Fix a few bugs in stdio.h and implement fread.

parent a79db5d8
......@@ -11,14 +11,15 @@
#define EOF (-1)
int fputc(int c, FILE *s) {
unsigned char uc = (unsigned char) c;
if (s->fd == 0) {
return EOF;
} else if (s->fd == 1 || s->fd == 2) {
__handles->write(c);
__handles->write(uc);
} else {
__handles->vfs_write(c, s->fd);
__handles->vfs_write(uc, s->fd);
}
return c;
return uc;
}
#define putc fputc
......@@ -51,7 +52,7 @@ int getc(FILE *stream) {
stream->ungetted = 0;
return stream->ungetbuf;
}
char buf;
unsigned char buf;
ssize_t res = read(stream->fd, &buf, 1);
if (res) {
return buf;
......@@ -82,12 +83,35 @@ int fflush(FILE *stream) {
return 0;
}
size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream) {
const char *buf = buffer;
size *= count;
while (size--) {
fputc(*buf++, stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
const char *buf = ptr;
size_t written;
for (written = 0; written < nmemb; written++) {
size_t size2 = size;
while (size2--) {
if (fputc(*buf++, stream) == EOF) {
return written;
}
}
}
return written;
}
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
char *buf = ptr;
size_t read;
for (read = 0; read < nmemb; read++) {
size_t size2 = size;
while (size2--) {
int c = fgetc(stream);
if (c == EOF) {
return read;
} else {
*buf++ = (char) c;
}
}
}
return read;
}
int _open_mode(const char *mode) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment