diff options
Diffstat (limited to 'lib/libc/stdio/fdopen.c')
| -rw-r--r-- | lib/libc/stdio/fdopen.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index 23f94843..f762a83d 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -1,7 +1,12 @@ -#include <io.h> -#include <libc.h> -#include <unistd.h> -#include <stdio.h> +#include "__stdio.h" // for __libc_fadd +#include "features.h" // for __weak +#include "stdatomic.h" // for atomic_flag_clear +#include "stddef.h" // for NULL + +#include <libc.h> // for __IMPL +#include <stdio.h> // for FILE, _IONBF, SEEK_END, _IOLBF, fdopen +#include <stdlib.h> // for calloc, free +#include <unistd.h> // for lseek, off_t __weak void __stdio_cleanup(void) { @@ -9,31 +14,32 @@ __weak void __stdio_cleanup(void) FILE *fdopen(int fildes, const char *mode) { - FILE *fp; + FILE *stream; if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a')) { return NULL; } - if ((fp = calloc(1, sizeof(FILE))) == NULL) + if ((stream = calloc(1, sizeof(FILE))) == NULL) return NULL; - fp->fd = fildes; - atomic_flag_clear(&fp->lock); + __IMPL(stream)->fd = fildes; + atomic_flag_clear(&__IMPL(stream)->lock); if (mode[0] == 'r') { - fp->type = _IONBF; + __IMPL(stream)->type = _IONBF; } else if (mode[0] == 'w') { - fp->type = _IOLBF; + __IMPL(stream)->type = _IOLBF; } else if (mode[0] == 'a') { - fp->type = _IONBF; + __IMPL(stream)->type = _IONBF; + off_t offset = lseek(fildes, 0, SEEK_END); if (offset == (off_t)-1) { - free(fp); + free(stream); return NULL; } } - __libc_fadd(fp); - return fp; + __libc_fadd(stream); + return stream; } |
