From 8b1380b63d62a1e1858a102fb36c2078697581ec Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 4 Mar 2020 03:01:03 -0500 Subject: eliminate nonstd/io.h --- src/nonstd/__libc.c | 8 +- src/nonstd/__libc_start.c | 3 +- src/nonstd/__printf.c | 4 - src/nonstd/__scanf.c | 4 - src/nonstd/_io.h | 24 ---- src/nonstd/_printf.h | 313 ----------------------------------------- src/nonstd/flockfile.c | 5 - src/nonstd/ftrylockfile.c | 5 - src/nonstd/funlockfile.c | 5 - src/nonstd/getc_unlocked.c | 5 - src/nonstd/io-internal.ref | 2 - src/nonstd/pid_t.ref | 2 - src/nonstd/stdio.ref | 2 - src/nonstd/struct_FILE.c | 36 ----- src/nonstd/struct_io_options.c | 10 -- src/stdio/__printf.c | 313 +++++++++++++++++++++++++++++++++++++++++ src/stdio/_stdio.h | 57 ++++++++ src/stdio/clearerr.c | 2 +- src/stdio/fclose.c | 2 +- src/stdio/feof.c | 2 +- src/stdio/ferror.c | 2 +- src/stdio/fflush.c | 2 +- src/stdio/fgetc.c | 6 +- src/stdio/fgetpos.c | 2 +- src/stdio/fgets.c | 2 +- src/stdio/fileno.c | 2 +- src/stdio/fopen.c | 2 +- src/stdio/fprintf.c | 2 +- src/stdio/fputc.c | 2 +- src/stdio/fputs.c | 2 +- src/stdio/fread.c | 2 +- src/stdio/freopen.c | 2 +- src/stdio/fscanf.c | 2 +- src/stdio/pclose.c | 2 +- src/stdio/popen.c | 2 +- src/stdio/printf.c | 2 +- src/stdio/scanf.c | 2 +- src/stdio/setvbuf.c | 2 +- src/stdio/snprintf.c | 2 +- src/stdio/sprintf.c | 2 +- src/stdio/sscanf.c | 2 +- src/stdio/ungetc.c | 2 +- src/stdio/vfprintf.c | 2 +- src/stdio/vfscanf.c | 2 +- src/stdio/vprintf.c | 2 +- src/stdio/vsnprintf.c | 2 +- src/stdio/vsprintf.c | 2 +- src/stdio/vsscanf.c | 2 +- src/wchar/fgetwc.c | 2 +- src/wchar/fputwc.c | 2 +- src/wchar/fwide.c | 2 +- src/wchar/vfwprintf.c | 2 +- src/wchar/vfwscanf.c | 2 +- src/wchar/vswscanf.c | 2 +- 54 files changed, 413 insertions(+), 463 deletions(-) delete mode 100644 src/nonstd/__printf.c delete mode 100644 src/nonstd/__scanf.c delete mode 100644 src/nonstd/_io.h delete mode 100644 src/nonstd/_printf.h delete mode 100644 src/nonstd/flockfile.c delete mode 100644 src/nonstd/ftrylockfile.c delete mode 100644 src/nonstd/funlockfile.c delete mode 100644 src/nonstd/getc_unlocked.c delete mode 100644 src/nonstd/io-internal.ref delete mode 100644 src/nonstd/pid_t.ref delete mode 100644 src/nonstd/stdio.ref delete mode 100644 src/nonstd/struct_FILE.c delete mode 100644 src/nonstd/struct_io_options.c create mode 100644 src/stdio/__printf.c create mode 100644 src/stdio/_stdio.h (limited to 'src') diff --git a/src/nonstd/__libc.c b/src/nonstd/__libc.c index f596a3c8..9471558d 100644 --- a/src/nonstd/__libc.c +++ b/src/nonstd/__libc.c @@ -1,10 +1,8 @@ #include "sys/types.h" #include #include "nonstd/locale.h" -#include "nonstd/io.h" +#include "../stdio/_stdio.h" -#include "_printf.h" -#include "_scanf.h" #include "_locale.h" #include "_syscall.h" @@ -67,10 +65,6 @@ void *__libc(LIBC_INTERNAL variable) r = (void*)__syscall_lookup; break; - case PRINTF: - r = (void*)(__printf); - break; - case FILE_STREAMS: r = file_streams; break; diff --git a/src/nonstd/__libc_start.c b/src/nonstd/__libc_start.c index ca3e8245..4b0b773e 100644 --- a/src/nonstd/__libc_start.c +++ b/src/nonstd/__libc_start.c @@ -1,8 +1,7 @@ #include "stdlib.h" #include "stdio.h" #include "locale.h" - -#include "nonstd/io.h" +#include "../stdio/_stdio.h" #ifdef _POSIX_SOURCE #define DEFAULT_LOCALE "POSIX" diff --git a/src/nonstd/__printf.c b/src/nonstd/__printf.c deleted file mode 100644 index 5868a15d..00000000 --- a/src/nonstd/__printf.c +++ /dev/null @@ -1,4 +0,0 @@ -#include - -#define __printf(_opts, _fmt, _ap) \ - ((int (*)(struct io_options*, const char*, va_list))__libc(PRINTF))(_opts, _fmt, _ap) diff --git a/src/nonstd/__scanf.c b/src/nonstd/__scanf.c deleted file mode 100644 index 7508880d..00000000 --- a/src/nonstd/__scanf.c +++ /dev/null @@ -1,4 +0,0 @@ -#include - -#define __scanf(_opts, _fmt, _ap) \ - ((int (*)(struct io_options*, const char*, va_list))__libc(SCANF))(_opts, _fmt, _ap) diff --git a/src/nonstd/_io.h b/src/nonstd/_io.h deleted file mode 100644 index d117cb8c..00000000 --- a/src/nonstd/_io.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __INTERNAL_IO_H__ -#define __INTERNAL_IO_H__ - -#include "nonstd/io.h" -#include "stdarg.h" - -#if defined __STDC_VERSION__ && 199909L <= __STDC_VERSION__ -#include "inttypes.h" -#else -#include "stdlib.h" -typedef unsigned long int uintmax_t; -typedef long int intmax_t; -typedef long int intptr_t; -typedef long int ptrdiff_t; -#define strtoumax(_s, _endptr, _base) strtoul(_s, _endptr, _base) -#endif - -#if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE -#include "unistd.h" -#else -#define write(fd, b, s) -#endif - -#endif diff --git a/src/nonstd/_printf.h b/src/nonstd/_printf.h deleted file mode 100644 index 31b2d82e..00000000 --- a/src/nonstd/_printf.h +++ /dev/null @@ -1,313 +0,0 @@ -#include "_io.h" - -#define NUMBUFLEN 64 - -#define LEFT (1 << 0) -#define SIGN (1 << 1) -#define SPACE (1 << 2) -#define ALT (1 << 3) -#define ZERO (1 << 4) -#define UPPER (1 << 5) -#define UNSIGNED (1 << 6) - -static int __append(char *s, char *argstring, int nout, size_t n) -{ - s += nout; - while (*argstring) { - if (nout < (int)n) { - *s++ = *argstring; - } - nout++; - argstring++; - } - return nout; -} - -static void __itos(char *s, intmax_t n, int flags, int precision, int base) -{ - extern int toupper(int); - char digits[] = "0123456789abcdef"; - char sign = n < 0 ? '-' : '+'; - char buf[NUMBUFLEN]; - char *out = buf + NUMBUFLEN; - if (flags & UPPER && base > 10) { - size_t i; - for (i = 0; i < sizeof(digits); i++) { - digits[i] = (char)toupper(digits[i]); - } - } - *out = '\0'; - out--; - if (n == 0) { - *out = '0'; - out--; - } - while (n > 0) { - precision--; - *out = digits[n % base]; - n /= base; - out--; - } - if (flags & SIGN || sign == '-') { - *out = sign; - out--; - } - out++; - while ((*s++ = *out++) != 0) { - continue; - } -} - -int (__printf)(struct io_options *opt, const char * format, va_list arg) -{ - extern int isdigit(int); - extern int isupper(int); - - char buf[BUFSIZ]; - int nout = 0; - int fd = -1; - struct __FILE *f = NULL; - - intmax_t argint = 0; - void *argptr = NULL; - char numbuf[NUMBUFLEN]; - - size_t i; - size_t n = 0; - char *s = NULL; - - if (opt->stream) { - /* file based */ - f = opt->stream; - s = buf; - n = BUFSIZ; - flockfile(f); - } else if (opt->string) { - /* memory buffer */ - s = opt->string; - n = opt->maxlen; - } else { - /* file descriptor */ - s = buf; - n = BUFSIZ; - fd = opt->fd; - } - - for (i = 0; format[i] != 0; i++) { - /* - // zero of more flags "-+ #0" - // optional width "*" or decimal integer - // optional precision ".*" or ".[decimal]" - // optional length modifier "hh", "h", "l", "ll", "j", - // "z", "t", "L" - // conversion specifier "diouxXfFeEgGaAcspn%" - */ - int flags = 0; - /* uintmax_t width = 0; */ - int step = 0; - int precision = 0; - int base = 10; - enum { def, hh, h, l, ll, j, z, t, L } length = def; - - if (format[i] != '%') { - if (nout < (int)n) { - s[nout] = format[i]; - } - nout++; - continue; - } - - while (step == 0) { - i++; - switch (format[i]) { - case '-': flags |= LEFT; break; - case '+': flags |= SIGN; break; - case ' ': flags |= SPACE; break; - case '#': flags |= ALT; break; - case '0': flags |= ZERO; break; - default: step = 1; break; - } - } - - if (format[i] == '*') { - i++; - } else if (isdigit(format[i])) { - /* - char *end; - width = strtoumax(format + i, &end, 10); - i = end - format; - */ - } - - if (format[i] == '.') { - i++; - if (format[i] == '*') { - i++; - } else if (isdigit(format[i])) { - char *end; - precision = (int)strtoumax(format + i, &end, 10); - i = end - format; - } else { - /* invalid precision */ - nout = -nout; - goto end; - } - } - - if (format[i] == 'h') { - i++; - if (format[i] == 'h') { - i++; - length = hh; - } else { - length = h; - } - } else if (format[i] == 'l') { - i++; - if (format[i] == 'l') { - i++; - length = ll; - } else { - length = l; - } - } else if (format[i] == 'j') { - i++; - length = j; - } else if (format[i] == 'z') { - i++; - length = z; - } else if (format[i] == 't') { - i++; - length = t; - } else if (format[i] == 'L') { - i++; - length = L; - } - - if (isupper(format[i])) { - flags |= UPPER; - } - - switch (format[i]) { - case 'o': /* unsigned int */ - case 'u': - case 'x': - case 'X': - flags |= UNSIGNED; - - case 'd': /* int */ - case 'i': - switch (length) { - case hh: argint = (signed char)va_arg(arg, int); break; - case h: argint = (short int)va_arg(arg, int); break; - case l: argint = va_arg(arg, long int); break; - - #if defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ - case ll: argint = va_arg(arg, long long int); break; - #endif - - case j: argint = va_arg(arg, intmax_t); break; - case z: argint = va_arg(arg, size_t); break; - case t: argint = va_arg(arg, ptrdiff_t); break; - case L: nout = -nout; goto end; - default: argint = va_arg(arg, int); break; - } - if (format[i] == 'o') { - base = 8; - } else if (format[i] == 'x') { - base = 16; - } else if (format[i] == 'X') { - base = 16; - flags |= UPPER; - } else { - base = 10; - } - __itos(numbuf, (long int)argint, flags, precision, base); - nout = __append(s, numbuf, nout, n); - break; - - - case 'f': /* double [-]ddd.ddd */ - case 'F': - break; - - case 'e': /* double [-]d.ddde+/-dd */ - case 'E': - break; - - case 'g': /* double f or e see docs */ - case 'G': - break; - - case 'a': /* double as hex */ - case 'A': - break; - - case 'c': /* char */ - if (length == def) { - char c = va_arg(arg, int); - if (nout < (int)n) { - s[nout] = c; - } - nout++; - } else if (length == l) { - /* wint_t wc = va_arg(arg, wint_t); */ - /* char mb[MB_CUR_MAX + 1] = "WC"; */ - /* wctomb(mb, wc); */ - /* nout = __append(s, mb, nout, n); */ - } else { - nout = -nout; - goto end; - } - break; - - case 's': /* string */ - if (length == def) { - char *string = va_arg(arg, char *); - nout = __append(s, string, nout, n); - } else if (length == l) { - /*wchar_t *ws = va_arg(arg, wchar_t *); */ - /*char *mbs = malloc(wcslen(ws) * MB_CUR_MAX + 1); */ - /*wcstombs(mbs, ws, wcslen(ws) * MB_CUR_MAX + 1); */ - /*nout = __append(s, mbs, nout, n); */ - /*free(mbs); */ - nout = __append(s, "WIDE STRING", nout, n); - } else { - nout = -nout; - goto end; - } - - break; - - case 'p': /* pointer */ - argptr = va_arg(arg, void *); - nout = __append(s, "0x", nout, n); - __itos(numbuf, (intptr_t)argptr, ZERO, sizeof(argptr) * 2, 16); - nout = __append(s, numbuf, nout, n); - break; - - case 'n': /* write-back */ - break; - - case '%': /* literal '%' */ - if (nout < (int)n) { - s[nout] = '%'; - } - nout++; - break; - - default: /* undefined */ - return -nout; - } - } - - end: - if (f) { - fwrite(buf, 1, nout % BUFSIZ, f); - funlockfile(f); - } else if (fd != -1) { - write(fd, buf, nout % BUFSIZ); - } - - return nout; -} diff --git a/src/nonstd/flockfile.c b/src/nonstd/flockfile.c deleted file mode 100644 index 021c73c3..00000000 --- a/src/nonstd/flockfile.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#if !(defined _POSIX_C_SOURCE && 199506L <= _POSIX_C_SOURCE) && !(defined _XOPEN_SOURCE && 500 <= _XOPEN_SOURCE) -#define flockfile(__f) (void)(__f) -#endif diff --git a/src/nonstd/ftrylockfile.c b/src/nonstd/ftrylockfile.c deleted file mode 100644 index b805a6e5..00000000 --- a/src/nonstd/ftrylockfile.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#if !(defined _POSIX_C_SOURCE && 199506L <= _POSIX_C_SOURCE) && !(defined _XOPEN_SOURCE && 500 <= _XOPEN_SOURCE) -#define ftrylockfile(__f) (void)(__f), 0 -#endif diff --git a/src/nonstd/funlockfile.c b/src/nonstd/funlockfile.c deleted file mode 100644 index ab67117f..00000000 --- a/src/nonstd/funlockfile.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#if !(defined _POSIX_C_SOURCE && 199506L <= _POSIX_C_SOURCE) && !(defined _XOPEN_SOURCE && 500 <= _XOPEN_SOURCE) -#define funlockfile(__f) (void)(__f) -#endif diff --git a/src/nonstd/getc_unlocked.c b/src/nonstd/getc_unlocked.c deleted file mode 100644 index fb700587..00000000 --- a/src/nonstd/getc_unlocked.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#if !(defined _POSIX_C_SOURCE && 199506L <= _POSIX_C_SOURCE) && !(defined _XOPEN_SOURCE && 500 <= _XOPEN_SOURCE) -#define getc_unlocked(__f) getc(__f) -#endif diff --git a/src/nonstd/io-internal.ref b/src/nonstd/io-internal.ref deleted file mode 100644 index 4c06d2f9..00000000 --- a/src/nonstd/io-internal.ref +++ /dev/null @@ -1,2 +0,0 @@ -#include -REFERENCE() diff --git a/src/nonstd/pid_t.ref b/src/nonstd/pid_t.ref deleted file mode 100644 index 23be346f..00000000 --- a/src/nonstd/pid_t.ref +++ /dev/null @@ -1,2 +0,0 @@ -#include -REFERENCE(sys/types/pid_t.c) diff --git a/src/nonstd/stdio.ref b/src/nonstd/stdio.ref deleted file mode 100644 index d6cbdf21..00000000 --- a/src/nonstd/stdio.ref +++ /dev/null @@ -1,2 +0,0 @@ -#include -REFERENCE() diff --git a/src/nonstd/struct_FILE.c b/src/nonstd/struct_FILE.c deleted file mode 100644 index a3b8610e..00000000 --- a/src/nonstd/struct_FILE.c +++ /dev/null @@ -1,36 +0,0 @@ -#include - -struct __FILE { - fpos_t pos; - char *buf; - enum { SUPPLIED, ALLOCED, UNSET } buftype; - int buffering; - int bsize; - int isopen; - int flags; - int lastop; - - /* verified necessary */ - int fd; - int oflag; - int orientation; - int eof; - int err; - int nlocks; - int thread; - - #ifdef _POSIX_SOURCE - pid_t pipe_pid; - #else - long int pipe_pid; - #endif - - struct { - char *buf; - size_t size; - int allocated; - } mem; - - struct __FILE *prev; - struct __FILE *next; -}; diff --git a/src/nonstd/struct_io_options.c b/src/nonstd/struct_io_options.c deleted file mode 100644 index db125f6d..00000000 --- a/src/nonstd/struct_io_options.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -struct io_options { - const char *fnname; - char *string; - struct __FILE *stream; - size_t maxlen; - int fd; - int flags; -}; diff --git a/src/stdio/__printf.c b/src/stdio/__printf.c new file mode 100644 index 00000000..26197b4a --- /dev/null +++ b/src/stdio/__printf.c @@ -0,0 +1,313 @@ +#include "_stdio.h" + +#define NUMBUFLEN 64 + +#define LEFT (1 << 0) +#define SIGN (1 << 1) +#define SPACE (1 << 2) +#define ALT (1 << 3) +#define ZERO (1 << 4) +#define UPPER (1 << 5) +#define UNSIGNED (1 << 6) + +static int __append(char *s, char *argstring, int nout, size_t n) +{ + s += nout; + while (*argstring) { + if (nout < (int)n) { + *s++ = *argstring; + } + nout++; + argstring++; + } + return nout; +} + +static void __itos(char *s, intmax_t n, int flags, int precision, int base) +{ + extern int toupper(int); + char digits[] = "0123456789abcdef"; + char sign = n < 0 ? '-' : '+'; + char buf[NUMBUFLEN]; + char *out = buf + NUMBUFLEN; + if (flags & UPPER && base > 10) { + size_t i; + for (i = 0; i < sizeof(digits); i++) { + digits[i] = (char)toupper(digits[i]); + } + } + *out = '\0'; + out--; + if (n == 0) { + *out = '0'; + out--; + } + while (n > 0) { + precision--; + *out = digits[n % base]; + n /= base; + out--; + } + if (flags & SIGN || sign == '-') { + *out = sign; + out--; + } + out++; + while ((*s++ = *out++) != 0) { + continue; + } +} + +int (__printf)(struct io_options *opt, const char * format, va_list arg) +{ + extern int isdigit(int); + extern int isupper(int); + + char buf[BUFSIZ]; + int nout = 0; + int fd = -1; + struct __FILE *f = NULL; + + intmax_t argint = 0; + void *argptr = NULL; + char numbuf[NUMBUFLEN]; + + size_t i; + size_t n = 0; + char *s = NULL; + + if (opt->stream) { + /* file based */ + f = opt->stream; + s = buf; + n = BUFSIZ; + flockfile(f); + } else if (opt->string) { + /* memory buffer */ + s = opt->string; + n = opt->maxlen; + } else { + /* file descriptor */ + s = buf; + n = BUFSIZ; + fd = opt->fd; + } + + for (i = 0; format[i] != 0; i++) { + /* + // zero of more flags "-+ #0" + // optional width "*" or decimal integer + // optional precision ".*" or ".[decimal]" + // optional length modifier "hh", "h", "l", "ll", "j", + // "z", "t", "L" + // conversion specifier "diouxXfFeEgGaAcspn%" + */ + int flags = 0; + /* uintmax_t width = 0; */ + int step = 0; + int precision = 0; + int base = 10; + enum { def, hh, h, l, ll, j, z, t, L } length = def; + + if (format[i] != '%') { + if (nout < (int)n) { + s[nout] = format[i]; + } + nout++; + continue; + } + + while (step == 0) { + i++; + switch (format[i]) { + case '-': flags |= LEFT; break; + case '+': flags |= SIGN; break; + case ' ': flags |= SPACE; break; + case '#': flags |= ALT; break; + case '0': flags |= ZERO; break; + default: step = 1; break; + } + } + + if (format[i] == '*') { + i++; + } else if (isdigit(format[i])) { + /* + char *end; + width = strtoumax(format + i, &end, 10); + i = end - format; + */ + } + + if (format[i] == '.') { + i++; + if (format[i] == '*') { + i++; + } else if (isdigit(format[i])) { + char *end; + precision = (int)strtoumax(format + i, &end, 10); + i = end - format; + } else { + /* invalid precision */ + nout = -nout; + goto end; + } + } + + if (format[i] == 'h') { + i++; + if (format[i] == 'h') { + i++; + length = hh; + } else { + length = h; + } + } else if (format[i] == 'l') { + i++; + if (format[i] == 'l') { + i++; + length = ll; + } else { + length = l; + } + } else if (format[i] == 'j') { + i++; + length = j; + } else if (format[i] == 'z') { + i++; + length = z; + } else if (format[i] == 't') { + i++; + length = t; + } else if (format[i] == 'L') { + i++; + length = L; + } + + if (isupper(format[i])) { + flags |= UPPER; + } + + switch (format[i]) { + case 'o': /* unsigned int */ + case 'u': + case 'x': + case 'X': + flags |= UNSIGNED; + + case 'd': /* int */ + case 'i': + switch (length) { + case hh: argint = (signed char)va_arg(arg, int); break; + case h: argint = (short int)va_arg(arg, int); break; + case l: argint = va_arg(arg, long int); break; + + #if defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ + case ll: argint = va_arg(arg, long long int); break; + #endif + + case j: argint = va_arg(arg, intmax_t); break; + case z: argint = va_arg(arg, size_t); break; + case t: argint = va_arg(arg, ptrdiff_t); break; + case L: nout = -nout; goto end; + default: argint = va_arg(arg, int); break; + } + if (format[i] == 'o') { + base = 8; + } else if (format[i] == 'x') { + base = 16; + } else if (format[i] == 'X') { + base = 16; + flags |= UPPER; + } else { + base = 10; + } + __itos(numbuf, (long int)argint, flags, precision, base); + nout = __append(s, numbuf, nout, n); + break; + + + case 'f': /* double [-]ddd.ddd */ + case 'F': + break; + + case 'e': /* double [-]d.ddde+/-dd */ + case 'E': + break; + + case 'g': /* double f or e see docs */ + case 'G': + break; + + case 'a': /* double as hex */ + case 'A': + break; + + case 'c': /* char */ + if (length == def) { + char c = va_arg(arg, int); + if (nout < (int)n) { + s[nout] = c; + } + nout++; + } else if (length == l) { + /* wint_t wc = va_arg(arg, wint_t); */ + /* char mb[MB_CUR_MAX + 1] = "WC"; */ + /* wctomb(mb, wc); */ + /* nout = __append(s, mb, nout, n); */ + } else { + nout = -nout; + goto end; + } + break; + + case 's': /* string */ + if (length == def) { + char *string = va_arg(arg, char *); + nout = __append(s, string, nout, n); + } else if (length == l) { + /*wchar_t *ws = va_arg(arg, wchar_t *); */ + /*char *mbs = malloc(wcslen(ws) * MB_CUR_MAX + 1); */ + /*wcstombs(mbs, ws, wcslen(ws) * MB_CUR_MAX + 1); */ + /*nout = __append(s, mbs, nout, n); */ + /*free(mbs); */ + nout = __append(s, "WIDE STRING", nout, n); + } else { + nout = -nout; + goto end; + } + + break; + + case 'p': /* pointer */ + argptr = va_arg(arg, void *); + nout = __append(s, "0x", nout, n); + __itos(numbuf, (intptr_t)argptr, ZERO, sizeof(argptr) * 2, 16); + nout = __append(s, numbuf, nout, n); + break; + + case 'n': /* write-back */ + break; + + case '%': /* literal '%' */ + if (nout < (int)n) { + s[nout] = '%'; + } + nout++; + break; + + default: /* undefined */ + return -nout; + } + } + + end: + if (f) { + fwrite(buf, 1, nout % BUFSIZ, f); + funlockfile(f); + } else if (fd != -1) { + write(fd, buf, nout % BUFSIZ); + } + + return nout; +} diff --git a/src/stdio/_stdio.h b/src/stdio/_stdio.h new file mode 100644 index 00000000..3335fa7e --- /dev/null +++ b/src/stdio/_stdio.h @@ -0,0 +1,57 @@ +#ifndef ___STDIO_H__ +#define ___STDIO_H__ + +#include +#include +#include "../sys/types/pid_t.c" +#include "nonstd/internal.h" + +struct __FILE { + fpos_t pos; + char *buf; + enum { SUPPLIED, ALLOCED, UNSET } buftype; + int buffering; + int bsize; + int isopen; + int flags; + int lastop; + + /* verified necessary */ + int fd; + int oflag; + int orientation; + int eof; + int err; + int nlocks; + int thread; + + pid_t pipe_pid; + + struct { + char *buf; + size_t size; + int allocated; + } mem; + + struct __FILE *prev; + struct __FILE *next; +}; + +struct io_options { + const char *fnname; + char *string; + struct __FILE *stream; + size_t maxlen; + int fd; + int flags; +}; + +int __printf(struct io_options * restrict, const char * restrict, va_list); +int __scanf(struct io_options * restrict, const char * restrict, va_list); + +#ifndef _POSIX_C_SOURCE +#define flockfile(...) +#define funlockfile(...) +#endif + +#endif diff --git a/src/stdio/clearerr.c b/src/stdio/clearerr.c index 9b817a97..b15ba362 100644 --- a/src/stdio/clearerr.c +++ b/src/stdio/clearerr.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** reset file stream error indicator **/ void clearerr(FILE * stream) diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c index f07cf55e..cd5b4417 100644 --- a/src/stdio/fclose.c +++ b/src/stdio/fclose.c @@ -1,6 +1,6 @@ #include #include "stdlib.h" -#include "nonstd/io.h" +#include "_stdio.h" #if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE #include "sys/types.h" diff --git a/src/stdio/feof.c b/src/stdio/feof.c index 26c9a5a8..9524d8c6 100644 --- a/src/stdio/feof.c +++ b/src/stdio/feof.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" #include "nonstd/assert.h" /** test for end-of-file **/ diff --git a/src/stdio/ferror.c b/src/stdio/ferror.c index 609f7640..9d427e1f 100644 --- a/src/stdio/ferror.c +++ b/src/stdio/ferror.c @@ -1,6 +1,6 @@ #include #include "nonstd/assert.h" -#include "nonstd/io.h" +#include "_stdio.h" /** tests the file stream error indicator **/ int ferror(FILE *stream) diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c index c4b5ad58..9030a39f 100644 --- a/src/stdio/fflush.c +++ b/src/stdio/fflush.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** flush buffered writes **/ int fflush(FILE *stream) diff --git a/src/stdio/fgetc.c b/src/stdio/fgetc.c index 8c682c39..ae1f529a 100644 --- a/src/stdio/fgetc.c +++ b/src/stdio/fgetc.c @@ -1,5 +1,9 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" + +#ifndef _POSIX_C_SOURCE +#define getc_unlocked(...) 0 +#endif /** read a character from a file stream **/ int fgetc(FILE *stream) diff --git a/src/stdio/fgetpos.c b/src/stdio/fgetpos.c index 177519d1..aee57181 100644 --- a/src/stdio/fgetpos.c +++ b/src/stdio/fgetpos.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** get the file stream position indicator **/ int fgetpos(FILE * restrict stream, fpos_t * restrict pos) diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index df110de2..b562cb31 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** read a string of characters from a file stream **/ char * fgets(char * restrict s, int n, FILE * restrict stream) diff --git a/src/stdio/fileno.c b/src/stdio/fileno.c index 56ba86aa..8292001d 100644 --- a/src/stdio/fileno.c +++ b/src/stdio/fileno.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" #include "nonstd/assert.h" int fileno(FILE * stream) diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c index 1b415969..8abf7709 100644 --- a/src/stdio/fopen.c +++ b/src/stdio/fopen.c @@ -1,6 +1,6 @@ #include #include "stdlib.h" -#include "nonstd/io.h" +#include "_stdio.h" /** open a file stream **/ FILE * fopen(const char * restrict filename, const char * restrict mode) diff --git a/src/stdio/fprintf.c b/src/stdio/fprintf.c index e6651076..40a2d42d 100644 --- a/src/stdio/fprintf.c +++ b/src/stdio/fprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** write formatted output to a file stream **/ int fprintf(FILE * restrict stream, const char * restrict format, ...) diff --git a/src/stdio/fputc.c b/src/stdio/fputc.c index 2d748751..c392a19c 100644 --- a/src/stdio/fputc.c +++ b/src/stdio/fputc.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" #if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE #include "sys/types.h" diff --git a/src/stdio/fputs.c b/src/stdio/fputs.c index b33d3b59..22ee7d28 100644 --- a/src/stdio/fputs.c +++ b/src/stdio/fputs.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** write a string to a file stream **/ int fputs(const char * restrict s, FILE * restrict stream) diff --git a/src/stdio/fread.c b/src/stdio/fread.c index 692e3e18..7929d495 100644 --- a/src/stdio/fread.c +++ b/src/stdio/fread.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** read directly from a file stream **/ size_t fread(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream) diff --git a/src/stdio/freopen.c b/src/stdio/freopen.c index 69732a48..e26f353f 100644 --- a/src/stdio/freopen.c +++ b/src/stdio/freopen.c @@ -1,6 +1,6 @@ #include #include "errno.h" -#include "nonstd/io.h" +#include "_stdio.h" #if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE #include "sys/types.h" diff --git a/src/stdio/fscanf.c b/src/stdio/fscanf.c index 9e027cf5..05ec5ed8 100644 --- a/src/stdio/fscanf.c +++ b/src/stdio/fscanf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** read formatted input from a file stream **/ int fscanf(FILE * restrict stream, const char * restrict format, ...) diff --git a/src/stdio/pclose.c b/src/stdio/pclose.c index aff03b73..258fc902 100644 --- a/src/stdio/pclose.c +++ b/src/stdio/pclose.c @@ -1,7 +1,7 @@ #include #include "sys/types.h" #include "sys/wait.h" -#include "nonstd/io.h" +#include "_stdio.h" int pclose(FILE * stream) { diff --git a/src/stdio/popen.c b/src/stdio/popen.c index ab9b1663..201800a7 100644 --- a/src/stdio/popen.c +++ b/src/stdio/popen.c @@ -6,7 +6,7 @@ #include "unistd.h" #include "nonstd/assert.h" -#include "nonstd/io.h" +#include "_stdio.h" FILE * popen(const char * command, const char * mode) { diff --git a/src/stdio/printf.c b/src/stdio/printf.c index 0a95fbeb..c7bb7a91 100644 --- a/src/stdio/printf.c +++ b/src/stdio/printf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** write formatted output **/ int printf(const char *format, ...) diff --git a/src/stdio/scanf.c b/src/stdio/scanf.c index cc392373..efec61d8 100644 --- a/src/stdio/scanf.c +++ b/src/stdio/scanf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** read formatted input **/ int scanf(const char * restrict format, ...) diff --git a/src/stdio/setvbuf.c b/src/stdio/setvbuf.c index 0db67cda..e5800288 100644 --- a/src/stdio/setvbuf.c +++ b/src/stdio/setvbuf.c @@ -1,6 +1,6 @@ #include #include "stdlib.h" -#include "nonstd/io.h" +#include "_stdio.h" /** specify file stream buffering options **/ int setvbuf(FILE *stream, char *buf, int mode, size_t size) diff --git a/src/stdio/snprintf.c b/src/stdio/snprintf.c index acf234b1..966883eb 100644 --- a/src/stdio/snprintf.c +++ b/src/stdio/snprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" int snprintf(char * restrict s, size_t n, const char * restrict format, ...) { diff --git a/src/stdio/sprintf.c b/src/stdio/sprintf.c index bf5f5b6c..9e651adb 100644 --- a/src/stdio/sprintf.c +++ b/src/stdio/sprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" #if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ #include "stdint.h" diff --git a/src/stdio/sscanf.c b/src/stdio/sscanf.c index 02939338..10a08447 100644 --- a/src/stdio/sscanf.c +++ b/src/stdio/sscanf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** read formatted input from a string **/ int sscanf(const char * restrict s, const char * restrict format, ...) diff --git a/src/stdio/ungetc.c b/src/stdio/ungetc.c index 1c32ac1f..5629679a 100644 --- a/src/stdio/ungetc.c +++ b/src/stdio/ungetc.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" /** push a character back into an input stream **/ int ungetc(int c, FILE *stream) diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index 450d1383..55a9a140 100644 --- a/src/stdio/vfprintf.c +++ b/src/stdio/vfprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** write formatted output to a file stream **/ int vfprintf(FILE * restrict stream, const char * restrict format, va_list arg) diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c index 6c0f97ee..ad9f5614 100644 --- a/src/stdio/vfscanf.c +++ b/src/stdio/vfscanf.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "_stdio.h" int vfscanf(FILE * restrict stream, const char * restrict format, va_list arg) { diff --git a/src/stdio/vprintf.c b/src/stdio/vprintf.c index 29c1b062..7650c066 100644 --- a/src/stdio/vprintf.c +++ b/src/stdio/vprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** write formatted output **/ int vprintf(const char * restrict format, va_list arg) diff --git a/src/stdio/vsnprintf.c b/src/stdio/vsnprintf.c index f54a15a1..b7785f89 100644 --- a/src/stdio/vsnprintf.c +++ b/src/stdio/vsnprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" int vsnprintf(char * restrict s, size_t n, const char *format, va_list arg) { diff --git a/src/stdio/vsprintf.c b/src/stdio/vsprintf.c index 43caff52..14c7f7fb 100644 --- a/src/stdio/vsprintf.c +++ b/src/stdio/vsprintf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" /** write formatted output to a string **/ int vsprintf(char *s, const char *format, va_list arg) diff --git a/src/stdio/vsscanf.c b/src/stdio/vsscanf.c index d5322078..a1248beb 100644 --- a/src/stdio/vsscanf.c +++ b/src/stdio/vsscanf.c @@ -1,6 +1,6 @@ #include #include "stdarg.h" -#include "nonstd/io.h" +#include "_stdio.h" int vsscanf(const char * restrict s, const char * restrict format, va_list arg) { diff --git a/src/wchar/fgetwc.c b/src/wchar/fgetwc.c index 30d98083..1ae00f59 100644 --- a/src/wchar/fgetwc.c +++ b/src/wchar/fgetwc.c @@ -1,6 +1,6 @@ #include #include "stdio.h" -#include "nonstd/io.h" +#include "../stdio/_stdio.h" #include "limits.h" wint_t fgetwc(FILE * stream) diff --git a/src/wchar/fputwc.c b/src/wchar/fputwc.c index e7b9c503..803ccfdb 100644 --- a/src/wchar/fputwc.c +++ b/src/wchar/fputwc.c @@ -1,6 +1,6 @@ #include #include "stdio.h" -#include "nonstd/io.h" +#include "../stdio/_stdio.h" #include "limits.h" #include "errno.h" diff --git a/src/wchar/fwide.c b/src/wchar/fwide.c index a1401a42..13c4d5f0 100644 --- a/src/wchar/fwide.c +++ b/src/wchar/fwide.c @@ -1,6 +1,6 @@ #include #include "stdio.h" -#include "nonstd/io.h" +#include "../stdio/_stdio.h" #include "nonstd/assert.h" int fwide(FILE * stream, int mode) diff --git a/src/wchar/vfwprintf.c b/src/wchar/vfwprintf.c index df9ab381..c122a964 100644 --- a/src/wchar/vfwprintf.c +++ b/src/wchar/vfwprintf.c @@ -2,7 +2,7 @@ #include "stdio.h" #include "stdarg.h" #include "stdlib.h" -#include "nonstd/io.h" +#include "../stdio/_stdio.h" int vfwprintf(FILE * restrict stream, const wchar_t * restrict format, va_list arg) { diff --git a/src/wchar/vfwscanf.c b/src/wchar/vfwscanf.c index d2e90c5f..9abbda0a 100644 --- a/src/wchar/vfwscanf.c +++ b/src/wchar/vfwscanf.c @@ -1,6 +1,6 @@ #include "stdio.h" #include -#include "nonstd/io.h" +#include "../stdio/_stdio.h" int vfwscanf(FILE * restrict stream, const wchar_t * restrict format, va_list arg) { diff --git a/src/wchar/vswscanf.c b/src/wchar/vswscanf.c index b0f1271a..93db847a 100644 --- a/src/wchar/vswscanf.c +++ b/src/wchar/vswscanf.c @@ -1,5 +1,5 @@ #include -#include "nonstd/io.h" +#include "../stdio/_stdio.h" int vswscanf(const wchar_t * restrict s, const wchar_t * restrict format, va_list arg) { -- cgit v1.2.1