LibC: Add ungetc() and automatically flush streams on fclose().

This commit is contained in:
Andreas Kling 2019-03-27 01:40:55 +01:00
parent 0c2face7b0
commit d1e55fb4d9
Notes: sideshowbarker 2024-07-19 14:56:05 +09:00
7 changed files with 39 additions and 3 deletions

View file

@ -47,6 +47,8 @@ __BEGIN_DECLS
#define S_IWOTH 0002
#define S_IXOTH 0001
#define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
#define S_IRWXG (S_IRWXU >> 3)
#define S_IRWXO (S_IRWXG >> 3)

View file

@ -6,6 +6,10 @@
#define PRId16 "d"
#define PRId32 "d"
#define PRId64 "lld"
#define PRIi8 "d"
#define PRIi16 "d"
#define PRIi32 "d"
#define PRIi64 "lld"
#define PRIu8 "u"
#define PRIu16 "u"
#define PRIu32 "u"

View file

@ -5,7 +5,6 @@
#define PAGE_SIZE 4096
#define PATH_MAX 4096
#define BUFSIZ 1024
#define INT_MAX INT32_MAX
#define INT_MIN INT32_MIN

View file

@ -10,6 +10,7 @@ enum {
LC_CTYPE,
LC_COLLATE,
LC_TIME,
LC_MONETARY,
};
struct lconv {

View file

@ -24,6 +24,16 @@ typedef __INT_FAST16_TYPE__ int_fast16_t;
typedef __INT_FAST32_TYPE__ int_fast32_t;
typedef __INT_FAST64_TYPE__ int_fast64_t;
typedef __UINT_LEAST8_TYPE__ uint_least8_t;
typedef __UINT_LEAST16_TYPE__ uint_least16_t;
typedef __UINT_LEAST32_TYPE__ uint_least32_t;
typedef __UINT_LEAST64_TYPE__ uint_least64_t;
typedef __INT_LEAST8_TYPE__ int_least8_t;
typedef __INT_LEAST16_TYPE__ int_least16_t;
typedef __INT_LEAST32_TYPE__ int_least32_t;
typedef __INT_LEAST64_TYPE__ int_least64_t;
#define __int8_t_defined 1
#define __uint8_t_defined 1
#define __int16_t_defined 1

View file

@ -141,9 +141,12 @@ int getchar()
return getc(stdin);
}
int ungetc(int, FILE*)
int ungetc(int c, FILE* stream)
{
ASSERT_NOT_REACHED();
ASSERT(stream);
stream->have_ungotten = true;
stream->ungotten = c;
return c;
}
int fputc(int ch, FILE* stream)
@ -203,6 +206,18 @@ int ferror(FILE* stream)
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
{
assert(stream);
if (!size)
return 0;
if (stream->have_ungotten) {
// FIXME: Support ungotten character even if size != 1.
ASSERT(size == 1);
((char*)ptr)[0] = stream->ungotten;
stream->have_ungotten = false;
--nmemb;
ptr = &((char*)ptr)[1];
}
ssize_t nread = read(stream->fd, ptr, nmemb * size);
if (nread < 0)
return 0;
@ -388,6 +403,7 @@ FILE* fdopen(int fd, const char* mode)
int fclose(FILE* stream)
{
fflush(stream);
int rc = close(stream->fd);
free(stream);
return rc;

View file

@ -7,6 +7,8 @@
#include <stdarg.h>
#include <limits.h>
#define BUFSIZ 1024
__BEGIN_DECLS
#ifndef EOF
#define EOF (-1)
@ -28,6 +30,8 @@ struct __STDIO_FILE {
char* buffer;
size_t buffer_size;
size_t buffer_index;
bool have_ungotten;
char ungotten;
char default_buffer[BUFSIZ];
};