stdio stuff

This commit is contained in:
K. Lange 2018-05-15 12:39:38 +09:00
parent 91ae45c518
commit 4a4b316de3
3 changed files with 24 additions and 9 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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);