diff options
Diffstat (limited to 'src/stdio')
47 files changed, 236 insertions, 203 deletions
diff --git a/src/stdio/__lockfile.c b/src/stdio/__lockfile.c index 0dcb2a42..0f60a149 100644 --- a/src/stdio/__lockfile.c +++ b/src/stdio/__lockfile.c @@ -1,8 +1,6 @@ #include "stdio_impl.h" #include "pthread_impl.h" -#define MAYBE_WAITERS 0x40000000 - int __lockfile(FILE *f) { int owner = f->lock, tid = __pthread_self()->tid; diff --git a/src/stdio/__stdio_close.c b/src/stdio/__stdio_close.c index 79452bdb..30291328 100644 --- a/src/stdio/__stdio_close.c +++ b/src/stdio/__stdio_close.c @@ -1,4 +1,5 @@ #include "stdio_impl.h" +#include "aio_impl.h" static int dummy(int fd) { diff --git a/src/stdio/__stdio_seek.c b/src/stdio/__stdio_seek.c index 13e06a66..326ab9bc 100644 --- a/src/stdio/__stdio_seek.c +++ b/src/stdio/__stdio_seek.c @@ -1,13 +1,7 @@ #include "stdio_impl.h" +#include <unistd.h> off_t __stdio_seek(FILE *f, off_t off, int whence) { - off_t ret; -#ifdef SYS__llseek - if (syscall(SYS__llseek, f->fd, off>>32, off, &ret, whence)<0) - ret = -1; -#else - ret = syscall(SYS_lseek, f->fd, off, whence); -#endif - return ret; + return __lseek(f->fd, off, whence); } diff --git a/src/stdio/__string_read.c b/src/stdio/__string_read.c deleted file mode 100644 index 7b50a7e1..00000000 --- a/src/stdio/__string_read.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "stdio_impl.h" -#include <string.h> - -size_t __string_read(FILE *f, unsigned char *buf, size_t len) -{ - char *src = f->cookie; - size_t k = len+256; - char *end = memchr(src, 0, k); - if (end) k = end-src; - if (k < len) len = k; - memcpy(buf, src, len); - f->rpos = (void *)(src+len); - f->rend = (void *)(src+k); - f->cookie = src+k; - return len; -} diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c index 889b96d2..d594532b 100644 --- a/src/stdio/fclose.c +++ b/src/stdio/fclose.c @@ -7,26 +7,32 @@ weak_alias(dummy, __unlist_locked_file); int fclose(FILE *f) { int r; - int perm; FLOCK(f); + r = fflush(f); + r |= f->close(f); + FUNLOCK(f); - __unlist_locked_file(f); + /* Past this point, f is closed and any further explict access + * to it is undefined. However, it still exists as an entry in + * the open file list and possibly in the thread's locked files + * list, if it was closed while explicitly locked. Functions + * which process these lists must tolerate dead FILE objects + * (which necessarily have inactive buffer pointers) without + * producing any side effects. */ - if (!(perm = f->flags & F_PERM)) { - FILE **head = __ofl_lock(); - if (f->prev) f->prev->next = f->next; - if (f->next) f->next->prev = f->prev; - if (*head == f) *head = f->next; - __ofl_unlock(); - } + if (f->flags & F_PERM) return r; - r = fflush(f); - r |= f->close(f); + __unlist_locked_file(f); + + FILE **head = __ofl_lock(); + if (f->prev) f->prev->next = f->next; + if (f->next) f->next->prev = f->prev; + if (*head == f) *head = f->next; + __ofl_unlock(); free(f->getln_buf); - if (!perm) free(f); - else FUNLOCK(f); + free(f); return r; } diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c index 02dae27a..b0094376 100644 --- a/src/stdio/fflush.c +++ b/src/stdio/fflush.c @@ -3,11 +3,14 @@ /* stdout.c will override this if linked */ static FILE *volatile dummy = 0; weak_alias(dummy, __stdout_used); +weak_alias(dummy, __stderr_used); int fflush(FILE *f) { if (!f) { - int r = __stdout_used ? fflush(__stdout_used) : 0; + int r = 0; + if (__stdout_used) r |= fflush(__stdout_used); + if (__stderr_used) r |= fflush(__stderr_used); for (f=*__ofl_lock(); f; f=f->next) { FLOCK(f); diff --git a/src/stdio/fgetc.c b/src/stdio/fgetc.c index e1224164..2578afcc 100644 --- a/src/stdio/fgetc.c +++ b/src/stdio/fgetc.c @@ -1,11 +1,7 @@ -#include "stdio_impl.h" +#include <stdio.h> +#include "getc.h" int fgetc(FILE *f) { - int c; - if (f->lock < 0 || !__lockfile(f)) - return getc_unlocked(f); - c = getc_unlocked(f); - __unlockfile(f); - return c; + return do_getc(f); } diff --git a/src/stdio/fgetpos.c b/src/stdio/fgetpos.c index 50813d2c..392f7323 100644 --- a/src/stdio/fgetpos.c +++ b/src/stdio/fgetpos.c @@ -7,5 +7,3 @@ int fgetpos(FILE *restrict f, fpos_t *restrict pos) *(long long *)pos = off; return 0; } - -weak_alias(fgetpos, fgetpos64); diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index 6171f398..4a100b39 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -12,13 +12,14 @@ char *fgets(char *restrict s, int n, FILE *restrict f) FLOCK(f); - if (n--<=1) { + if (n<=1) { f->mode |= f->mode-1; FUNLOCK(f); - if (n) return 0; + if (n<1) return 0; *s = 0; return s; } + n--; while (n) { if (f->rpos != f->rend) { diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c index 0801e28f..aa10b818 100644 --- a/src/stdio/fgetwc.c +++ b/src/stdio/fgetwc.c @@ -25,12 +25,18 @@ static wint_t __fgetwc_unlocked_internal(FILE *f) do { b = c = getc_unlocked(f); if (c < 0) { - if (!first) errno = EILSEQ; + if (!first) { + f->flags |= F_ERR; + errno = EILSEQ; + } return WEOF; } l = mbrtowc(&wc, (void *)&b, 1, &st); if (l == -1) { - if (!first) ungetc(b, f); + if (!first) { + f->flags |= F_ERR; + ungetc(b, f); + } return WEOF; } first = 0; diff --git a/src/stdio/fgetws.c b/src/stdio/fgetws.c index b08b3049..195cb435 100644 --- a/src/stdio/fgetws.c +++ b/src/stdio/fgetws.c @@ -1,6 +1,5 @@ #include "stdio_impl.h" #include <wchar.h> -#include <errno.h> wint_t __fgetwc_unlocked(FILE *); @@ -12,10 +11,6 @@ wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f) FLOCK(f); - /* Setup a dummy errno so we can detect EILSEQ. This is - * the only way to catch encoding errors in the form of a - * partial character just before EOF. */ - errno = EAGAIN; for (; n; n--) { wint_t c = __fgetwc_unlocked(f); if (c == WEOF) break; @@ -23,7 +18,7 @@ wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f) if (c == '\n') break; } *p = 0; - if (ferror(f) || errno==EILSEQ) p = s; + if (ferror(f)) p = s; FUNLOCK(f); diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c index 82413b2d..343e3e3f 100644 --- a/src/stdio/fmemopen.c +++ b/src/stdio/fmemopen.c @@ -2,6 +2,7 @@ #include <errno.h> #include <string.h> #include <stdlib.h> +#include <stddef.h> #include <inttypes.h> #include "libc.h" @@ -83,7 +84,7 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode) struct mem_FILE *f; int plus = !!strchr(mode, '+'); - if (!size || !strchr("rwa", *mode)) { + if (!strchr("rwa", *mode)) { errno = EINVAL; return 0; } @@ -95,18 +96,17 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode) f = malloc(sizeof *f + (buf?0:size)); if (!f) return 0; - memset(&f->f, 0, sizeof f->f); + memset(f, 0, offsetof(struct mem_FILE, buf)); f->f.cookie = &f->c; f->f.fd = -1; f->f.lbf = EOF; f->f.buf = f->buf + UNGET; f->f.buf_size = sizeof f->buf - UNGET; if (!buf) { - buf = f->buf2;; + buf = f->buf2; memset(buf, 0, size); } - memset(&f->c, 0, sizeof f->c); f->c.buf = buf; f->c.size = size; f->c.mode = *mode; diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c index e1b91e12..80bc341e 100644 --- a/src/stdio/fopen.c +++ b/src/stdio/fopen.c @@ -29,5 +29,3 @@ FILE *fopen(const char *restrict filename, const char *restrict mode) __syscall(SYS_close, fd); return 0; } - -weak_alias(fopen, fopen64); diff --git a/src/stdio/fputc.c b/src/stdio/fputc.c index 92762c98..f364ed38 100644 --- a/src/stdio/fputc.c +++ b/src/stdio/fputc.c @@ -1,10 +1,7 @@ -#include "stdio_impl.h" +#include <stdio.h> +#include "putc.h" int fputc(int c, FILE *f) { - if (f->lock < 0 || !__lockfile(f)) - return putc_unlocked(c, f); - c = putc_unlocked(c, f); - __unlockfile(f); - return c; + return do_putc(c, f); } diff --git a/src/stdio/freopen.c b/src/stdio/freopen.c index 615d4b47..1641a4c5 100644 --- a/src/stdio/freopen.c +++ b/src/stdio/freopen.c @@ -40,6 +40,8 @@ FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *re fclose(f2); } + f->mode = 0; + f->locale = 0; FUNLOCK(f); return f; @@ -49,5 +51,3 @@ fail: fclose(f); return NULL; } - -weak_alias(freopen, freopen64); diff --git a/src/stdio/fseek.c b/src/stdio/fseek.c index 439308f7..c7425802 100644 --- a/src/stdio/fseek.c +++ b/src/stdio/fseek.c @@ -1,7 +1,14 @@ #include "stdio_impl.h" +#include <errno.h> int __fseeko_unlocked(FILE *f, off_t off, int whence) { + /* Fail immediately for invalid whence argument. */ + if (whence != SEEK_CUR && whence != SEEK_SET && whence != SEEK_END) { + errno = EINVAL; + return -1; + } + /* Adjust relative offset for unread data in buffer, if any. */ if (whence == SEEK_CUR && f->rend) off -= f->rend - f->rpos; @@ -39,5 +46,3 @@ int fseek(FILE *f, long off, int whence) } weak_alias(__fseeko, fseeko); - -weak_alias(fseeko, fseeko64); diff --git a/src/stdio/fsetpos.c b/src/stdio/fsetpos.c index 77ab8d82..779cb3cc 100644 --- a/src/stdio/fsetpos.c +++ b/src/stdio/fsetpos.c @@ -4,5 +4,3 @@ int fsetpos(FILE *f, const fpos_t *pos) { return __fseeko(f, *(const long long *)pos, SEEK_SET); } - -weak_alias(fsetpos, fsetpos64); diff --git a/src/stdio/ftell.c b/src/stdio/ftell.c index 1a2afbbc..1e1a08d8 100644 --- a/src/stdio/ftell.c +++ b/src/stdio/ftell.c @@ -37,5 +37,3 @@ long ftell(FILE *f) } weak_alias(__ftello, ftello); - -weak_alias(ftello, ftello64); diff --git a/src/stdio/ftrylockfile.c b/src/stdio/ftrylockfile.c index 3b97807a..50650585 100644 --- a/src/stdio/ftrylockfile.c +++ b/src/stdio/ftrylockfile.c @@ -2,8 +2,6 @@ #include "pthread_impl.h" #include <limits.h> -#define MAYBE_WAITERS 0x40000000 - void __do_orphaned_stdio_locks() { FILE *f; diff --git a/src/stdio/getc.c b/src/stdio/getc.c index b3f351d1..8409fc23 100644 --- a/src/stdio/getc.c +++ b/src/stdio/getc.c @@ -1,13 +1,9 @@ -#include "stdio_impl.h" +#include <stdio.h> +#include "getc.h" int getc(FILE *f) { - int c; - if (f->lock < 0 || !__lockfile(f)) - return getc_unlocked(f); - c = getc_unlocked(f); - __unlockfile(f); - return c; + return do_getc(f); } weak_alias(getc, _IO_getc); diff --git a/src/stdio/getc.h b/src/stdio/getc.h new file mode 100644 index 00000000..e24f9905 --- /dev/null +++ b/src/stdio/getc.h @@ -0,0 +1,22 @@ +#include "stdio_impl.h" +#include "pthread_impl.h" + +#ifdef __GNUC__ +__attribute__((__noinline__)) +#endif +static int locking_getc(FILE *f) +{ + if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f); + int c = getc_unlocked(f); + if (a_swap(&f->lock, 0) & MAYBE_WAITERS) + __wake(&f->lock, 1, 1); + return c; +} + +static inline int do_getc(FILE *f) +{ + int l = f->lock; + if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid) + return getc_unlocked(f); + return locking_getc(f); +} diff --git a/src/stdio/getchar.c b/src/stdio/getchar.c index c1012658..df395ca9 100644 --- a/src/stdio/getchar.c +++ b/src/stdio/getchar.c @@ -1,6 +1,7 @@ #include <stdio.h> +#include "getc.h" int getchar(void) { - return fgetc(stdin); + return do_getc(stdin); } diff --git a/src/stdio/getdelim.c b/src/stdio/getdelim.c index d2f5b15a..df114441 100644 --- a/src/stdio/getdelim.c +++ b/src/stdio/getdelim.c @@ -55,9 +55,11 @@ ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric *s = tmp; *n = m; } - memcpy(*s+i, f->rpos, k); - f->rpos += k; - i += k; + if (k) { + memcpy(*s+i, f->rpos, k); + f->rpos += k; + i += k; + } if (z) break; if ((c = getc_unlocked(f)) == EOF) { if (!i || !feof(f)) { diff --git a/src/stdio/gets.c b/src/stdio/gets.c index 6c4645e5..17963b93 100644 --- a/src/stdio/gets.c +++ b/src/stdio/gets.c @@ -4,7 +4,12 @@ char *gets(char *s) { - char *ret = fgets(s, INT_MAX, stdin); - if (ret && s[strlen(s)-1] == '\n') s[strlen(s)-1] = 0; - return ret; + size_t i=0; + int c; + FLOCK(stdin); + while ((c=getc_unlocked(stdin)) != EOF && c != '\n') s[i++] = c; + s[i] = 0; + if (c != '\n' && (!feof(stdin) || !i)) s = 0; + FUNLOCK(stdin); + return s; } diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c index f2d3215a..aad3d171 100644 --- a/src/stdio/ofl.c +++ b/src/stdio/ofl.c @@ -1,8 +1,10 @@ #include "stdio_impl.h" #include "lock.h" +#include "fork_impl.h" static FILE *ofl_head; static volatile int ofl_lock[1]; +volatile int *const __stdio_ofl_lockptr = ofl_lock; FILE **__ofl_lock() { diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c index ed1b561d..b8ae4a79 100644 --- a/src/stdio/open_wmemstream.c +++ b/src/stdio/open_wmemstream.c @@ -40,8 +40,12 @@ fail: static size_t wms_write(FILE *f, const unsigned char *buf, size_t len) { struct cookie *c = f->cookie; - size_t len2; + size_t len2 = f->wpos - f->wbase; wchar_t *newbuf; + if (len2) { + f->wpos = f->wbase; + if (wms_write(f, f->wbase, len2) < len2) return 0; + } if (len + c->pos >= c->space) { len2 = 2*c->space+1 | c->pos+len+1; if (len2 > SSIZE_MAX/4) return 0; diff --git a/src/stdio/pclose.c b/src/stdio/pclose.c index 080a4262..c64da405 100644 --- a/src/stdio/pclose.c +++ b/src/stdio/pclose.c @@ -7,7 +7,7 @@ int pclose(FILE *f) int status, r; pid_t pid = f->pipe_pid; fclose(f); - while ((r=__syscall(SYS_wait4, pid, &status, 0, 0)) == -EINTR); + while ((r=__sys_wait4(pid, &status, 0, 0)) == -EINTR); if (r<0) return __syscall_ret(r); return status; } diff --git a/src/stdio/popen.c b/src/stdio/popen.c index 92cb57ee..3ec83394 100644 --- a/src/stdio/popen.c +++ b/src/stdio/popen.c @@ -31,25 +31,12 @@ FILE *popen(const char *cmd, const char *mode) __syscall(SYS_close, p[1]); return NULL; } - FLOCK(f); - - /* If the child's end of the pipe happens to already be on the final - * fd number to which it will be assigned (either 0 or 1), it must - * be moved to a different fd. Otherwise, there is no safe way to - * remove the close-on-exec flag in the child without also creating - * a file descriptor leak race condition in the parent. */ - if (p[1-op] == 1-op) { - int tmp = fcntl(1-op, F_DUPFD_CLOEXEC, 0); - if (tmp < 0) { - e = errno; - goto fail; - } - __syscall(SYS_close, p[1-op]); - p[1-op] = tmp; - } e = ENOMEM; if (!posix_spawn_file_actions_init(&fa)) { + for (FILE *l = *__ofl_lock(); l; l=l->next) + if (l->pipe_pid && posix_spawn_file_actions_addclose(&fa, l->fd)) + goto fail; if (!posix_spawn_file_actions_adddup2(&fa, p[1-op], 1-op)) { if (!(e = posix_spawn(&pid, "/bin/sh", &fa, 0, (char *[]){ "sh", "-c", (char *)cmd, 0 }, __environ))) { @@ -58,13 +45,14 @@ FILE *popen(const char *cmd, const char *mode) if (!strchr(mode, 'e')) fcntl(p[op], F_SETFD, 0); __syscall(SYS_close, p[1-op]); - FUNLOCK(f); + __ofl_unlock(); return f; } } +fail: + __ofl_unlock(); posix_spawn_file_actions_destroy(&fa); } -fail: fclose(f); __syscall(SYS_close, p[1-op]); diff --git a/src/stdio/putc.c b/src/stdio/putc.c index fa893496..4744d978 100644 --- a/src/stdio/putc.c +++ b/src/stdio/putc.c @@ -1,12 +1,9 @@ -#include "stdio_impl.h" +#include <stdio.h> +#include "putc.h" int putc(int c, FILE *f) { - if (f->lock < 0 || !__lockfile(f)) - return putc_unlocked(c, f); - c = putc_unlocked(c, f); - __unlockfile(f); - return c; + return do_putc(c, f); } weak_alias(putc, _IO_putc); diff --git a/src/stdio/putc.h b/src/stdio/putc.h new file mode 100644 index 00000000..2014c4ec --- /dev/null +++ b/src/stdio/putc.h @@ -0,0 +1,22 @@ +#include "stdio_impl.h" +#include "pthread_impl.h" + +#ifdef __GNUC__ +__attribute__((__noinline__)) +#endif +static int locking_putc(int c, FILE *f) +{ + if (a_cas(&f->lock, 0, MAYBE_WAITERS-1)) __lockfile(f); + c = putc_unlocked(c, f); + if (a_swap(&f->lock, 0) & MAYBE_WAITERS) + __wake(&f->lock, 1, 1); + return c; +} + +static inline int do_putc(int c, FILE *f) +{ + int l = f->lock; + if (l < 0 || l && (l & ~MAYBE_WAITERS) == __pthread_self()->tid) + return putc_unlocked(c, f); + return locking_putc(c, f); +} diff --git a/src/stdio/putchar.c b/src/stdio/putchar.c index 945636d5..f044f169 100644 --- a/src/stdio/putchar.c +++ b/src/stdio/putchar.c @@ -1,6 +1,7 @@ #include <stdio.h> +#include "putc.h" int putchar(int c) { - return fputc(c, stdout); + return do_putc(c, stdout); } diff --git a/src/stdio/rename.c b/src/stdio/rename.c index 04c90c01..f540adb6 100644 --- a/src/stdio/rename.c +++ b/src/stdio/rename.c @@ -4,9 +4,11 @@ int rename(const char *old, const char *new) { -#ifdef SYS_rename +#if defined(SYS_rename) return syscall(SYS_rename, old, new); -#else +#elif defined(SYS_renameat) return syscall(SYS_renameat, AT_FDCWD, old, AT_FDCWD, new); +#else + return syscall(SYS_renameat2, AT_FDCWD, old, AT_FDCWD, new, 0); #endif } diff --git a/src/stdio/setvbuf.c b/src/stdio/setvbuf.c index 06ea296c..523dddc8 100644 --- a/src/stdio/setvbuf.c +++ b/src/stdio/setvbuf.c @@ -12,13 +12,15 @@ int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) if (type == _IONBF) { f->buf_size = 0; - } else { + } else if (type == _IOLBF || type == _IOFBF) { if (buf && size >= UNGET) { f->buf = (void *)(buf + UNGET); f->buf_size = size - UNGET; } if (type == _IOLBF && f->buf_size) f->lbf = '\n'; + } else { + return -1; } f->flags |= F_SVB; diff --git a/src/stdio/stderr.c b/src/stdio/stderr.c index 229c8651..f2bc4648 100644 --- a/src/stdio/stderr.c +++ b/src/stdio/stderr.c @@ -1,7 +1,9 @@ #include "stdio_impl.h" +#undef stderr + static unsigned char buf[UNGET]; -static FILE f = { +hidden FILE __stderr_FILE = { .buf = buf+UNGET, .buf_size = 0, .fd = 2, @@ -12,5 +14,5 @@ static FILE f = { .close = __stdio_close, .lock = -1, }; -FILE *const stderr = &f; -FILE *volatile __stderr_used = &f; +FILE *const stderr = &__stderr_FILE; +FILE *volatile __stderr_used = &__stderr_FILE; diff --git a/src/stdio/stdin.c b/src/stdio/stdin.c index 171ff22a..5aa5262c 100644 --- a/src/stdio/stdin.c +++ b/src/stdio/stdin.c @@ -1,7 +1,9 @@ #include "stdio_impl.h" +#undef stdin + static unsigned char buf[BUFSIZ+UNGET]; -static FILE f = { +hidden FILE __stdin_FILE = { .buf = buf+UNGET, .buf_size = sizeof buf-UNGET, .fd = 0, @@ -11,5 +13,5 @@ static FILE f = { .close = __stdio_close, .lock = -1, }; -FILE *const stdin = &f; -FILE *volatile __stdin_used = &f; +FILE *const stdin = &__stdin_FILE; +FILE *volatile __stdin_used = &__stdin_FILE; diff --git a/src/stdio/stdout.c b/src/stdio/stdout.c index 6b188942..4985a417 100644 --- a/src/stdio/stdout.c +++ b/src/stdio/stdout.c @@ -1,7 +1,9 @@ #include "stdio_impl.h" +#undef stdout + static unsigned char buf[BUFSIZ+UNGET]; -static FILE f = { +hidden FILE __stdout_FILE = { .buf = buf+UNGET, .buf_size = sizeof buf-UNGET, .fd = 1, @@ -12,5 +14,5 @@ static FILE f = { .close = __stdio_close, .lock = -1, }; -FILE *const stdout = &f; -FILE *volatile __stdout_used = &f; +FILE *const stdout = &__stdout_FILE; +FILE *volatile __stdout_used = &__stdout_FILE; diff --git a/src/stdio/tempnam.c b/src/stdio/tempnam.c index 84f91978..0c65b1f0 100644 --- a/src/stdio/tempnam.c +++ b/src/stdio/tempnam.c @@ -36,11 +36,10 @@ char *tempnam(const char *dir, const char *pfx) for (try=0; try<MAXTRIES; try++) { __randname(s+l-6); -#ifdef SYS_lstat - r = __syscall(SYS_lstat, s, &(struct stat){0}); +#ifdef SYS_readlink + r = __syscall(SYS_readlink, s, (char[1]){0}, 1); #else - r = __syscall(SYS_fstatat, AT_FDCWD, s, - &(struct stat){0}, AT_SYMLINK_NOFOLLOW); + r = __syscall(SYS_readlinkat, AT_FDCWD, s, (char[1]){0}, 1); #endif if (r == -ENOENT) return strdup(s); } diff --git a/src/stdio/tmpfile.c b/src/stdio/tmpfile.c index ae493987..2fa8803f 100644 --- a/src/stdio/tmpfile.c +++ b/src/stdio/tmpfile.c @@ -27,5 +27,3 @@ FILE *tmpfile(void) } return 0; } - -weak_alias(tmpfile, tmpfile64); diff --git a/src/stdio/tmpnam.c b/src/stdio/tmpnam.c index 6c7c253a..71dc8bb1 100644 --- a/src/stdio/tmpnam.c +++ b/src/stdio/tmpnam.c @@ -16,11 +16,10 @@ char *tmpnam(char *buf) int r; for (try=0; try<MAXTRIES; try++) { __randname(s+12); -#ifdef SYS_lstat - r = __syscall(SYS_lstat, s, &(struct stat){0}); +#ifdef SYS_readlink + r = __syscall(SYS_readlink, s, (char[1]){0}, 1); #else - r = __syscall(SYS_fstatat, AT_FDCWD, s, - &(struct stat){0}, AT_SYMLINK_NOFOLLOW); + r = __syscall(SYS_readlinkat, AT_FDCWD, s, (char[1]){0}, 1); #endif if (r == -ENOENT) return strcpy(buf ? buf : internal, s); } diff --git a/src/stdio/ungetc.c b/src/stdio/ungetc.c index 180673a4..bc629d4c 100644 --- a/src/stdio/ungetc.c +++ b/src/stdio/ungetc.c @@ -16,5 +16,5 @@ int ungetc(int c, FILE *f) f->flags &= ~F_EOF; FUNLOCK(f); - return c; + return (unsigned char)c; } diff --git a/src/stdio/vdprintf.c b/src/stdio/vdprintf.c index c35d9b4f..3b9c093b 100644 --- a/src/stdio/vdprintf.c +++ b/src/stdio/vdprintf.c @@ -1,14 +1,9 @@ #include "stdio_impl.h" -static size_t wrap_write(FILE *f, const unsigned char *buf, size_t len) -{ - return __stdio_write(f, buf, len); -} - int vdprintf(int fd, const char *restrict fmt, va_list ap) { FILE f = { - .fd = fd, .lbf = EOF, .write = wrap_write, + .fd = fd, .lbf = EOF, .write = __stdio_write, .buf = (void *)fmt, .buf_size = 0, .lock = -1 }; diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index 9b961e7f..360d723a 100644 --- a/src/stdio/vfprintf.c +++ b/src/stdio/vfprintf.c @@ -52,7 +52,7 @@ static const unsigned char states[]['z'-'A'+1] = { S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, - S('c') = CHAR, S('C') = INT, + S('c') = INT, S('C') = UINT, S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR, S('m') = NOARG, S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE, @@ -62,7 +62,7 @@ static const unsigned char states[]['z'-'A'+1] = { S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, - S('c') = INT, S('s') = PTR, S('n') = PTR, + S('c') = UINT, S('s') = PTR, S('n') = PTR, S('l') = LLPRE, }, { /* 2: ll-prefixed */ S('d') = LLONG, S('i') = LLONG, @@ -132,7 +132,7 @@ static void pop_arg(union arg *arg, int type, va_list *ap) static void out(FILE *f, const char *s, size_t l) { - if (!(f->flags & F_ERR)) __fwritex((void *)s, l, f); + if (!ferror(f)) __fwritex((void *)s, l, f); } static void pad(FILE *f, char c, int w, int l, int fl) @@ -166,7 +166,8 @@ static char *fmt_u(uintmax_t x, char *s) { unsigned long y; for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10; - for (y=x; y; y/=10) *--s = '0' + y%10; + for (y=x; y>=10; y/=10) *--s = '0' + y%10; + if (y) *--s = '0' + y; return s; } @@ -211,18 +212,11 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t) if (y) e2--; if ((t|32)=='a') { - long double round = 8.0; - int re; - if (t&32) prefix += 9; pl += 2; - if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0; - else re=LDBL_MANT_DIG/4-1-p; - - if (re) { - round *= 1<<(LDBL_MANT_DIG%4); - while (re--) round*=16; + if (p>=0 && p<(LDBL_MANT_DIG-1+3)/4) { + double round = scalbn(1, LDBL_MANT_DIG-1-(p*4)); if (*prefix=='-') { y=-y; y-=round; @@ -437,7 +431,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, unsigned st, ps; int cnt=0, l=0; size_t i; - char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; + char buf[sizeof(uintmax_t)*3]; const char *prefix; int t, pl; wchar_t wc[2], *ws; @@ -478,8 +472,8 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (*s=='*') { if (isdigit(s[1]) && s[2]=='$') { l10n=1; - nl_type[s[1]-'0'] = INT; - w = nl_arg[s[1]-'0'].i; + if (!f) nl_type[s[1]-'0'] = INT, w = 0; + else w = nl_arg[s[1]-'0'].i; s+=3; } else if (!l10n) { w = f ? va_arg(*ap, int) : 0; @@ -491,8 +485,8 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, /* Read precision */ if (*s=='.' && s[1]=='*') { if (isdigit(s[2]) && s[3]=='$') { - nl_type[s[2]-'0'] = INT; - p = nl_arg[s[2]-'0'].i; + if (!f) nl_type[s[2]-'0'] = INT, p = 0; + else p = nl_arg[s[2]-'0'].i; s+=4; } else if (!l10n) { p = f ? va_arg(*ap, int) : 0; @@ -521,13 +515,18 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (st==NOARG) { if (argpos>=0) goto inval; } else { - if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos]; - else if (f) pop_arg(&arg, st, ap); + if (argpos>=0) { + if (!f) nl_type[argpos]=st; + else arg=nl_arg[argpos]; + } else if (f) pop_arg(&arg, st, ap); else return 0; } if (!f) continue; + /* Do not process any new directives once in error state. */ + if (ferror(f)) return -1; + z = buf + sizeof(buf); prefix = "-+ 0X0x"; pl = 0; @@ -583,6 +582,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, } p = MAX(p, z-a + !arg.i); break; + narrow_c: case 'c': *(a=z-(p=1))=arg.i; fl &= ~ZERO_PAD; @@ -597,6 +597,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, fl &= ~ZERO_PAD; break; case 'C': + if (!arg.i) goto narrow_c; wc[0] = arg.i; wc[1] = 0; arg.p = wc; @@ -672,7 +673,7 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) FLOCK(f); olderr = f->flags & F_ERR; - if (f->mode < 1) f->flags &= ~F_ERR; + f->flags &= ~F_ERR; if (!f->buf_size) { saved_buf = f->buf; f->buf = internal_buf; @@ -688,7 +689,7 @@ int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) f->buf_size = 0; f->wpos = f->wbase = f->wend = 0; } - if (f->flags & F_ERR) ret = -1; + if (ferror(f)) ret = -1; f->flags |= olderr; FUNLOCK(f); va_end(ap2); diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c index 9e030fc4..b78a374d 100644 --- a/src/stdio/vfscanf.c +++ b/src/stdio/vfscanf.c @@ -57,7 +57,7 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap) { int width; int size; - int alloc; + int alloc = 0; int base; const unsigned char *p; int c, t; @@ -76,6 +76,9 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap) FLOCK(f); + if (!f->rpos) __toread(f); + if (!f->rpos) goto input_fail; + for (p=(const unsigned char *)fmt; *p; p++) { alloc = 0; diff --git a/src/stdio/vfwprintf.c b/src/stdio/vfwprintf.c index 0adf0b7a..59d5471b 100644 --- a/src/stdio/vfwprintf.c +++ b/src/stdio/vfwprintf.c @@ -45,7 +45,7 @@ static const unsigned char states[]['z'-'A'+1] = { S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, - S('c') = CHAR, S('C') = INT, + S('c') = INT, S('C') = UINT, S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR, S('m') = NOARG, S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE, @@ -53,7 +53,9 @@ static const unsigned char states[]['z'-'A'+1] = { }, { /* 1: l-prefixed */ S('d') = LONG, S('i') = LONG, S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, - S('c') = INT, S('s') = PTR, S('n') = PTR, + S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, + S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, + S('c') = UINT, S('s') = PTR, S('n') = PTR, S('l') = LLPRE, }, { /* 2: ll-prefixed */ S('d') = LLONG, S('i') = LLONG, @@ -123,7 +125,13 @@ static void pop_arg(union arg *arg, int type, va_list *ap) static void out(FILE *f, const wchar_t *s, size_t l) { - while (l-- && !(f->flags & F_ERR)) fputwc(*s++, f); + while (l-- && !ferror(f)) fputwc(*s++, f); +} + +static void pad(FILE *f, int n, int fl) +{ + if ((fl & LEFT_ADJ) || !n || ferror(f)) return; + fprintf(f, "%*s", n, ""); } static int getint(wchar_t **s) { @@ -240,6 +248,10 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_ } if (!f) continue; + + /* Do not process any new directives once in error state. */ + if (ferror(f)) return -1; + t = s[-1]; if (ps && (t&15)==3) t&=~32; @@ -256,25 +268,22 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_ } continue; case 'c': + case 'C': if (w<1) w=1; - if (w>1 && !(fl&LEFT_ADJ)) fprintf(f, "%*s", w-1, ""); - fputwc(btowc(arg.i), f); - if (w>1 && (fl&LEFT_ADJ)) fprintf(f, "%*s", w-1, ""); + pad(f, w-1, fl); + out(f, &(wchar_t){t=='C' ? arg.i : btowc(arg.i)}, 1); + pad(f, w-1, fl^LEFT_ADJ); l = w; continue; - case 'C': - fputwc(arg.i, f); - l = 1; - continue; case 'S': a = arg.p; z = a + wcsnlen(a, p<0 ? INT_MAX : p); if (p<0 && *z) goto overflow; p = z-a; if (w<p) w=p; - if (!(fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, ""); + pad(f, w-p, fl); out(f, a, p); - if ((fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, ""); + pad(f, w-p, fl^LEFT_ADJ); l=w; continue; case 'm': @@ -287,14 +296,14 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_ if (p<0 && *bs) goto overflow; p=l; if (w<p) w=p; - if (!(fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, ""); + pad(f, w-p, fl); bs = arg.p; while (l--) { i=mbtowc(&wc, bs, MB_LEN_MAX); bs+=i; - fputwc(wc, f); + out(f, &wc, 1); } - if ((fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, ""); + pad(f, w-p, fl^LEFT_ADJ); l=w; continue; } @@ -338,8 +347,8 @@ overflow: int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap) { va_list ap2; - int nl_type[NL_ARGMAX] = {0}; - union arg nl_arg[NL_ARGMAX]; + int nl_type[NL_ARGMAX+1] = {0}; + union arg nl_arg[NL_ARGMAX+1]; int olderr; int ret; @@ -355,7 +364,7 @@ int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap) olderr = f->flags & F_ERR; f->flags &= ~F_ERR; ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type); - if (f->flags & F_ERR) ret = -1; + if (ferror(f)) ret = -1; f->flags |= olderr; FUNLOCK(f); va_end(ap2); diff --git a/src/stdio/vsnprintf.c b/src/stdio/vsnprintf.c index b3510a63..409b9c85 100644 --- a/src/stdio/vsnprintf.c +++ b/src/stdio/vsnprintf.c @@ -45,11 +45,6 @@ int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list ap) .cookie = &c, }; - if (n > INT_MAX) { - errno = EOVERFLOW; - return -1; - } - *c.s = 0; return vfprintf(&f, fmt, ap); } diff --git a/src/stdio/vsscanf.c b/src/stdio/vsscanf.c index 98500225..4d6d259b 100644 --- a/src/stdio/vsscanf.c +++ b/src/stdio/vsscanf.c @@ -1,15 +1,25 @@ #include "stdio_impl.h" +#include <string.h> -static size_t do_read(FILE *f, unsigned char *buf, size_t len) +static size_t string_read(FILE *f, unsigned char *buf, size_t len) { - return __string_read(f, buf, len); + char *src = f->cookie; + size_t k = len+256; + char *end = memchr(src, 0, k); + if (end) k = end-src; + if (k < len) len = k; + memcpy(buf, src, len); + f->rpos = (void *)(src+len); + f->rend = (void *)(src+k); + f->cookie = src+k; + return len; } int vsscanf(const char *restrict s, const char *restrict fmt, va_list ap) { FILE f = { .buf = (void *)s, .cookie = (void *)s, - .read = do_read, .lock = -1 + .read = string_read, .lock = -1 }; return vfscanf(&f, fmt, ap); } diff --git a/src/stdio/vswprintf.c b/src/stdio/vswprintf.c index 7f98c5c9..5e9a4dad 100644 --- a/src/stdio/vswprintf.c +++ b/src/stdio/vswprintf.c @@ -18,6 +18,7 @@ static size_t sw_write(FILE *f, const unsigned char *s, size_t l) if (s!=f->wbase && sw_write(f, f->wbase, f->wpos-f->wbase)==-1) return -1; while (c->l && l && (i=mbtowc(c->ws, (void *)s, l))>=0) { + if (!i) i=1; s+=i; l-=i; c->l--; @@ -50,9 +51,6 @@ int vswprintf(wchar_t *restrict s, size_t n, const wchar_t *restrict fmt, va_lis if (!n) { return -1; - } else if (n > INT_MAX) { - errno = EOVERFLOW; - return -1; } r = vfwprintf(&f, fmt, ap); sw_write(&f, 0, 0); |