Skip to content

Commit

Permalink
LibC: Add ungetc() and automatically flush streams on fclose().
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Mar 27, 2019
1 parent 0c2face commit d1e55fb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions LibC/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions LibC/inttypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion LibC/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions LibC/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum {
LC_CTYPE,
LC_COLLATE,
LC_TIME,
LC_MONETARY,
};

struct lconv {
Expand Down
10 changes: 10 additions & 0 deletions LibC/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions LibC/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions LibC/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <stdarg.h>
#include <limits.h>

#define BUFSIZ 1024

__BEGIN_DECLS
#ifndef EOF
#define EOF (-1)
Expand All @@ -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];
};

Expand Down

0 comments on commit d1e55fb

Please sign in to comment.