diff --git a/base/usr/include/stdio.h b/base/usr/include/stdio.h index e4675ce0..fe85d286 100644 --- a/base/usr/include/stdio.h +++ b/base/usr/include/stdio.h @@ -29,7 +29,7 @@ extern size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE * stream); extern int fileno(FILE * stream); extern int fflush(FILE * stream); -extern size_t vasprintf(char * buf, const char *fmt, va_list args); +extern size_t vasprintf(char ** buf, const char *fmt, va_list args); extern int sprintf(char *buf, const char *fmt, ...); extern int fprintf(FILE *stream, char *fmt, ...); extern int printf(char *fmt, ...); @@ -50,4 +50,6 @@ extern int ungetc(int c, FILE * stream); extern int feof(FILE * stream); extern void clearerr(FILE * stream); +extern int _fwouldblock(FILE * stream); + #define getc(s) fgetc(s) diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c index 7bf36acd..940fa17c 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.c @@ -96,7 +96,7 @@ static void print_hex(unsigned int value, unsigned int width, char * buf, int * /* * vasprintf() */ -size_t vasprintf(char * buf, const char * fmt, va_list args) { +size_t xvasprintf(char * buf, const char * fmt, va_list args) { int i = 0; char * s; char * b = buf; @@ -178,33 +178,42 @@ size_t vasprintf(char * buf, const char * fmt, va_list args) { /* Ensure the buffer ends in a null */ *b = '\0'; return b - buf; +} +size_t vasprintf(char ** buf, const char * fmt, va_list args) { + char * b = malloc(1024); + *buf = b; + return xvasprintf(b, fmt, args); } int fprintf(FILE * device, char *fmt, ...) { va_list args; va_start(args, fmt); - char buffer[1024]; - vasprintf(buffer, fmt, args); + char * buffer; + vasprintf(&buffer, fmt, args); va_end(args); - return fwrite(buffer, 1, strlen(buffer), device); + int out = fwrite(buffer, 1, strlen(buffer), device); + free(buffer); + return out; } int printf(char *fmt, ...) { va_list args; va_start(args, fmt); - char buffer[1024]; - vasprintf(buffer, fmt, args); + char * buffer; + vasprintf(&buffer, fmt, args); va_end(args); - return fwrite(buffer, 1, strlen(buffer), stdout); + int out = fwrite(buffer, 1, strlen(buffer), stdout); + free(buffer); + return out; } int sprintf(char * buf, const char *fmt, ...) { va_list args; va_start(args, fmt); - int out = vasprintf(buf, fmt, args); + int out = xvasprintf(buf, fmt, args); va_end(args); return out; } diff --git a/libc/stdio/stdio.c b/libc/stdio/stdio.c index 6d9886a4..e73ae1fd 100644 --- a/libc/stdio/stdio.c +++ b/libc/stdio/stdio.c @@ -190,6 +190,10 @@ FILE * fdopen(int fd, const char *mode){ return out; } +int _fwouldblock(FILE * stream) { + return !stream->available; +} + int fclose(FILE * stream) { int out = syscall_close(stream->fd); free(stream->read_buf);