diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2002-03-12 01:18:50 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2002-03-12 01:18:50 +0000 |
commit | 03e039820dc5092e27e81f3671652f25da7f25f1 (patch) | |
tree | 37bddad6951b8a6aa5d75184353705f672217812 /libc/stdio | |
parent | ff3e48d94097ed02480bb0df538620b221ccd72f (diff) |
Swap in the new stdio code.
Diffstat (limited to 'libc/stdio')
-rw-r--r-- | libc/stdio/Makefile | 75 | ||||
-rw-r--r-- | libc/stdio/getw.c | 36 | ||||
-rw-r--r-- | libc/stdio/old_vfprintf.c (renamed from libc/stdio/printf.c) | 376 | ||||
-rw-r--r-- | libc/stdio/perror.c | 27 | ||||
-rw-r--r-- | libc/stdio/putw.c | 34 | ||||
-rw-r--r-- | libc/stdio/remove.c | 23 | ||||
-rw-r--r-- | libc/stdio/scanf.c | 83 | ||||
-rw-r--r-- | libc/stdio/stdio.c | 1117 |
8 files changed, 199 insertions, 1572 deletions
diff --git a/libc/stdio/Makefile b/libc/stdio/Makefile index bbbc46664..c2a557a45 100644 --- a/libc/stdio/Makefile +++ b/libc/stdio/Makefile @@ -24,26 +24,73 @@ TOPDIR=../../ include $(TOPDIR)Rules.mak -MSRC=stdio.c -MOBJ=_stdio_init.o _alloc_stdio_buffer.o _free_stdio_buffer_of_file.o \ - _free_stdio_stream.o clearerr.o feof.o ferror.o fileno.o setbuffer.o \ - setvbuf.o setbuf.o setlinebuf.o fclose.o _fopen.o fopen.o freopen.o \ - fdopen.o fflush.o fsfopen.o fseek.o rewind.o ftell.o fgetpos.o fsetpos.o \ - fputc.o fgetc.o fgets.o gets.o fputs.o puts.o ungetc.o fread.o fwrite.o \ - getchar.o putchar.o _uClibc_fwrite.o _uClibc_fread.o fopen64.o getc.o putc.o - -MSRC2=printf.c -MOBJ2=printf.o sprintf.o fprintf.o vprintf.o vsprintf.o vfprintf.o snprintf.o \ - vsnprintf.o asprintf.o vfnprintf.o fnprintf.o vdprintf.o +# Set to true to use the old vfprintf instead of the new. The old is roughly +# C89 compliant, but doesn't deal with qualifiers on %n and doesn't deal with +# %h correctly or %hh at all on the interger conversions. But on i386 it is +# over 1.5k smaller than the new code. Of course, the new code fixes the +# above mentioned deficiencies and adds custom specifier support similar to +# glibc, as well as handling positional args. This option is here temporarily +# until the configuration system gets rewritten. Note also that the old +# vfprintf code will be rewritten at some point to bring it into at least C89 +# standards compliance. + +USE_OLD_VFPRINTF = false + +# Note: The *64.o objects are empty when compiled without large file support. +# To not build them at all, remove the appropriate line from the MOBJ +# definition and uncomment the DOLFS test below. + +# Note: Use the libpthreads of: flockfile.o ftrylockfile.o funlockfile.o +# Also, maybe move __fsetlocking.o as well? + +MSRC = stdio.c +MOBJ = fclose.o fflush.o fopen.o freopen.o perror.o remove.o \ + setbuf.o setvbuf.o fgetc.o fgets.o fputc.o fputs.o \ + getc.o getchar.o gets.o putc.o putchar.o puts.o \ + ungetc.o fread.o fwrite.o fgetpos.o fseek.o fsetpos.o ftell.o \ + rewind.o clearerr.o feof.o ferror.o \ + fileno.o fdopen.o getw.o putw.o setbuffer.o setlinebuf.o fcloseall.o \ + fopen64.o freopen64.o ftello64.o fseeko64.o fsetpos64.o fgetpos64.o \ + __fbufsize.o __freading.o __fwriting.o __freadable.o __fwritable.o \ + __flbf.o __fpurge.o __fpending.o _flushlbf.o \ + fopencookie.o fmemopen.o open_memstream.o \ + __fsetlocking.o \ + _stdio_fopen.o _stdio_fread.o _stdio_fwrite.o _stdio_adjpos.o \ + _stdio_lseek.o _stdio_init.o \ + _stdio_fsfopen.o _stdio_fdout.o _uintmaxtostr.o + +# ifeq ($(DOLFS),true) +# MOBJ += fopen64.o freopen64.o ftello64.o fseeko64.o fsetpos64.o fgetpos64.o +# endif + +MSRC2= printf.c +MOBJ2= vsnprintf.o vdprintf.o vasprintf.o vprintf.o vsprintf.o \ + fprintf.o snprintf.o dprintf.o asprintf.o printf.o sprintf.o + +ifneq ($(USE_OLD_VFPRINTF),true) + MOBJ2 += _ppfs_init.o _ppfs_prepargs.o _ppfs_setargs.o \ + _ppfs_parsespec.o _do_one_spec.o vfprintf.o \ + _store_inttype.o _load_inttype.o \ + register_printf_function.o parse_printf_format.o +endif + + + +ifeq ($(HAS_FLOATING_POINT),true) + MOBJ2 += _dtostr.o +endif MSRC3=scanf.c MOBJ3=scanf.o sscanf.o fscanf.o vscanf.o vsscanf.o vfscanf.o -CSRC=popen.c perror.c remove.c getdelim.c getline.c tmpfile.c tmpnam.c \ - tmpnam_r.c tempnam.c ctermid.c getw.c putw.c +CSRC=popen.c getdelim.c getline.c tmpfile.c tmpnam.c \ + tmpnam_r.c tempnam.c ctermid.c +ifeq ($(USE_OLD_VFPRINTF),true) + CSRC += old_vfprintf.c +endif COBJS=$(patsubst %.c,%.o, $(CSRC)) -OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) +OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) all: $(OBJS) $(LIBC) diff --git a/libc/stdio/getw.c b/libc/stdio/getw.c deleted file mode 100644 index 75e73667f..000000000 --- a/libc/stdio/getw.c +++ /dev/null @@ -1,36 +0,0 @@ -/* Copyright (C) 1991, 1997, 1998 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#include <stdio.h> - -#ifdef USE_IN_LIBIO -# include <libio/iolibio.h> -# define fread(p, m, n, s) _IO_fread (p, m, n, s) -#endif - -/* Read a word (int) from STREAM. */ -int -getw (FILE *stream) -{ - int w; - - /* Is there a better way? */ - if (fread ((void *) &w, sizeof (w), 1, stream) != 1) - return EOF; - return w; -} diff --git a/libc/stdio/printf.c b/libc/stdio/old_vfprintf.c index 2940e44be..c7c07f34a 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/old_vfprintf.c @@ -97,7 +97,6 @@ /* These are now set in uClibc_config.h based on Config. */ /* -#define __UCLIBC_HAS_LONG_LONG__ 1 #define __UCLIBC_HAS_FLOATS__ 1 */ @@ -109,7 +108,6 @@ * to the base code size of 1163 on i386. */ -#define WANT_LONG_LONG_ERROR 0 #define WANT_FLOAT_ERROR 0 /* @@ -123,200 +121,29 @@ /**************************************************************************/ -#include <sys/types.h> -#include <fcntl.h> -#include <string.h> -#include <stdlib.h> +#define _GNU_SOURCE /* for strnlen */ +#define _STDIO_UTILITY +#include <stdio.h> +#include <stdarg.h> #include <limits.h> -#include <assert.h> - -#if WANT_GNU_ERRNO +#include <string.h> #include <errno.h> -#endif -#ifdef __STDC__ -#include <stdarg.h> -#define va_strt va_start -#else -#include <varargs.h> -#define va_strt(p,i) va_start(p) -#endif +#define __PRINTF_INFO_NO_BITFIELD +#include <printf.h> -#include "stdio.h" +#ifdef __STDIO_THREADSAFE +#include <pthread.h> +#endif /* __STDIO_THREADSAFE */ -extern int vfnprintf(FILE * op, size_t max_size, - register __const char *fmt, register va_list ap); +/* #undef __UCLIBC_HAS_FLOATS__ */ +/* #undef WANT_FLOAT_ERROR */ +/* #define WANT_FLOAT_ERROR 1 */ -#ifdef L_printf -int printf(const char *fmt, ...) -{ - va_list ptr; - int rv; +#define __isdigit(c) (((unsigned int)(c - '0')) < 10) - va_strt(ptr, fmt); - rv = vfnprintf(stdout, -1, fmt, ptr); - va_end(ptr); - return rv; -} -#endif +extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info); -#ifdef L_asprintf -int asprintf(char **app, const char *fmt, ...) -{ - va_list ptr; - int rv; - char *p; - - /* - * First iteration - find out size of buffer required and allocate it. - */ - va_strt(ptr, fmt); - rv = vsnprintf(NULL, 0, fmt, ptr); - va_end(ptr); - - p = malloc(++rv); /* allocate the buffer */ - *app = p; - if (!p) { - return -1; - } - - /* - * Second iteration - actually produce output. - */ - va_strt(ptr, fmt); - rv = vsnprintf(p, rv, fmt, ptr); - va_end(ptr); - - return rv; -} -#endif - -#ifdef L_sprintf -int sprintf(char *sp, const char *fmt, ...) -{ - va_list ptr; - int rv; - - va_strt(ptr, fmt); - rv = vsnprintf(sp, -1, fmt, ptr); - va_end(ptr); - return rv; -} -#endif - - -#ifdef L_snprintf -int snprintf(char *sp, size_t size, const char *fmt, ...) -{ - va_list ptr; - int rv; - - va_strt(ptr, fmt); - rv = vsnprintf(sp, size, fmt, ptr); - va_end(ptr); - return rv; -} -#endif - -#ifdef L_fprintf -int fprintf(FILE * fp, const char *fmt, ...) -{ - va_list ptr; - int rv; - - va_strt(ptr, fmt); - rv = vfnprintf(fp, -1, fmt, ptr); - va_end(ptr); - return rv; -} -#endif - -#ifdef L_fnprintf -int fnprintf(FILE * fp, size_t size, const char *fmt, ...) -{ - va_list ptr; - int rv; - - va_strt(ptr, fmt); - rv = vfnprintf(fp, size, fmt, ptr); - va_end(ptr); - return rv; -} -#endif - -#ifdef L_vprintf -int vprintf(const char *fmt, va_list ap) -{ - return vfprintf(stdout, fmt, ap); -} -#endif - -#ifdef L_vfprintf - -int vfprintf(FILE * op, register __const char *fmt, register va_list ap) -{ - return vfnprintf(op, -1, fmt, ap); -} - -#endif - -#ifdef L_vsprintf -int vsprintf(char *sp, __const char *fmt, va_list ap) -{ - return vsnprintf(sp, -1, fmt, ap); -} -#endif - -#ifdef L_vsnprintf -int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap) -{ - int rv; - FILE f; - - /* - * As we're only using the putc macro in vfnprintf, we don't need to - * initialize all FILE f's fields. - */ - f.bufwrite = (char *) ((unsigned) -1); - f.bufpos = sp; - f.mode = _IOFBF; - - rv = vfnprintf(&f, size, fmt, ap); - if (size) { /* If this is going to a buffer, */ - *(f.bufpos) = 0; /* don't forget to nul-terminate. */ - } - return rv; -} -#endif - -#ifdef L_vdprintf -/* - * Note: If fd has an associated buffered FILE, bad things happen. - */ -extern int vdprintf(int fd, const char *fmt, va_list ap) -{ - char buf[BUFSIZ]; - FILE f = {buf, 0, buf+sizeof(buf), buf, buf+sizeof(buf), 0, fd, _IOFBF}; - int rv; - - rv = vfnprintf(&f, -1, fmt, ap); - - if (fflush(&f)) { - return -1; - } - - return rv; -} -#endif - -#ifdef L_vfnprintf - -extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase); -extern char *__ltostr(char *buf, long val, int base, int uppercase); -extern char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase); -extern char *__lltostr(char *buf, long long val, int base, int uppercase); -extern int __dtostr(FILE * fp, size_t size, long double x, - char flag[], int width, int preci, char mode); enum { FLAG_PLUS = 0, @@ -329,15 +156,7 @@ enum { /* layout 01234 */ static const char spec[] = "+-#0 "; -#if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR static const char qual[] = "hlLq"; -#else -static const char qual[] = "hl"; -#endif - -#if !defined(__UCLIBC_HAS_LONG_LONG__) && WANT_LONG_LONG_ERROR -static const char ll_err[] = "<LONG-LONG>"; -#endif #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR static const char dbl_err[] = "<DOUBLE>"; @@ -355,27 +174,21 @@ static const char u_spec[] = "%nbopxXudics"; /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */ static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a"; -int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) +int vfprintf(FILE * __restrict op, register const char * __restrict fmt, + va_list ap) { int i, cnt, lval, len; char *p; const char *fmt0; - int buffer_mode; int preci, width; #define upcase i int radix, dpoint /*, upcase*/; -#if defined(__UCLIBC_HAS_LONG_LONG__) - char tmp[65]; -#else - char tmp[33]; -#endif + char tmp[65]; /* TODO - determing needed size from headers */ char flag[sizeof(spec)]; - cnt = 0; + __STDIO_THREADLOCK(op); - /* This speeds things up a bit for line unbuffered */ - buffer_mode = (op->mode & __MODE_BUF); - op->mode &= (~__MODE_BUF); + cnt = 0; while (*fmt) { if (*fmt == '%') { @@ -391,8 +204,6 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) lval = 1; /* sizeof(int) == sizeof(long) */ #endif - tmp[1] = 0; /* set things up for %c -- better done here */ - /* init flags */ for (p =(char *) spec ; *p ; p++) { flag[p-spec] = '\0'; @@ -446,13 +257,11 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) for (p = (char *) qual ; *p ; p++) { if (*p == *fmt) { lval = p - qual; - ++fmt; -#if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR + ++fmt; /* TODO - hh */ if ((*p == 'l') && (*fmt == *p)) { ++lval; ++fmt; } -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */ } } @@ -477,40 +286,20 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) } if (p-u_spec < 8) { /* unsigned conversion */ radix = u_radix[p-u_spec-2]; - upcase = ((int)'x') - *p; + upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER); if (*p == 'p') { lval = (sizeof(char *) == sizeof(long)); - upcase = 0; + upcase = __UIM_LOWER; flag[FLAG_HASH] = 'p'; } -#if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR - if (lval >= 2) { -#if defined(__UCLIBC_HAS_LONG_LONG__) - p = __ulltostr(tmp + sizeof(tmp) - 1, - va_arg(ap, unsigned long long), - radix, upcase); -#else - (void) va_arg(ap, unsigned long long); /* cary on */ - p = (char *) ll_err; -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) */ - } else { -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */ -#if UINT_MAX != ULONG_MAX - /* sizeof(unsigned int) != sizeof(unsigned long) */ - p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long) - ((lval) - ? va_arg(ap, unsigned long) - : va_arg(ap, unsigned int)), - radix, upcase); -#else - /* sizeof(unsigned int) == sizeof(unsigned long) */ - p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long) - va_arg(ap, unsigned long), + + p = _uintmaxtostr((tmp + sizeof(tmp) - 1), + ((lval>1) /* TODO -- longlong/long/int/short/char */ + ? va_arg(ap, uintmax_t) + : (uintmax_t) + va_arg(ap, unsigned long)), radix, upcase); -#endif -#if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR - } -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */ + flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */ if (*p != '0') { /* non-zero */ if (flag[FLAG_HASH]) { @@ -531,31 +320,14 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) p = "(nil)"; } } else if (p-u_spec < 10) { /* signed conversion */ -#if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR - if (lval >= 2) { -#if defined(__UCLIBC_HAS_LONG_LONG__) - p = __lltostr(tmp + sizeof(tmp) - 1, - va_arg(ap, long long), 10, 0); -#else - (void) va_arg(ap, long long); /* carry on */ - p = (char *) ll_err; -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) */ - } else { -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */ -#if INT_MAX != LONG_MAX - /* sizeof(int) != sizeof(long) */ - p = __ltostr(tmp + sizeof(tmp) - 1, (long) - ((lval) - ? va_arg(ap, long) - : va_arg(ap, int)), 10, 0); -#else - /* sizeof(int) == sizeof(long) */ - p = __ltostr(tmp + sizeof(tmp) - 1, (long) - va_arg(ap, long), 10, 0); -#endif -#if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR - } -#endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */ + + p = _uintmaxtostr((tmp + sizeof(tmp) - 1), + ((lval>1) /* TODO -- longlong/long/int/short/char */ + ? va_arg(ap, uintmax_t) + : (uintmax_t) ((long long) /* sign-extend! */ + va_arg(ap, long))), + -radix, upcase); + } else if (p-u_spec < 12) { /* character or string */ flag[FLAG_PLUS] = '\0'; flag[FLAG_0_PAD] = ' '; @@ -569,21 +341,44 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) p = va_arg(ap, char *); if (!p) { p = "(null)"; + preci = 6; + } else { + if (preci < 0) { + preci = INT_MAX; + } } + len = strnlen(p, preci); + goto print_len_set; } #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR } else if (p-u_spec < 27) { /* floating point */ #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */ #if defined(__UCLIBC_HAS_FLOATS__) + struct printf_info info; if (preci < 0) { preci = 6; } - cnt += __dtostr(op, - (max_size > cnt ? max_size - cnt : 0), - (long double) ((lval > 1) - ? va_arg(ap, long double) - : va_arg(ap, double)), - flag, width, preci, *fmt); + info.width = width; + info.prec = preci; + info.spec = *fmt; + info.pad = flag[FLAG_0_PAD]; + info._flags = 0; + if (flag[FLAG_PLUS] == '+') { + PRINT_INFO_SET_FLAG(&info,showsign); + } else if (flag[FLAG_PLUS] == ' ') { + PRINT_INFO_SET_FLAG(&info,space); + } + if (flag[FLAG_HASH]) { + PRINT_INFO_SET_FLAG(&info,alt); + } + if (flag[FLAG_MINUS_LJUSTIFY]) { + PRINT_INFO_SET_FLAG(&info,left); + } + cnt += _dtostr(op, + ((lval > 1) + ? va_arg(ap, long double) + : (long double) va_arg(ap, double)), + &info); goto nextfmt; #elif WANT_FLOAT_ERROR (void) ((lval > 1) ? va_arg(ap, long double) @@ -597,7 +392,8 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) #endif { /* this used to be printfield */ /* cheaper than strlen call */ - for ( len = 0 ; p[len] ; len++ ) { } +/* for ( len = 0 ; p[len] ; len++ ) { } */ + len = strnlen(p, SIZE_MAX); print_len_set: if ((*p == '-') #if WANT_GNU_ERRNO @@ -671,13 +467,8 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) ch = *p++; /* main field */ --len; } - - if (++cnt < max_size) { - putc(ch, op); - } - if ((ch == '\n') && (buffer_mode == _IOLBF)) { - fflush(op); - } + ++cnt; + putc(ch, op); } } goto nextfmt; @@ -686,28 +477,17 @@ int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap) fmt = fmt0; /* this was an illegal format */ } - charout: - if (++cnt < max_size) { - putc(*fmt, op); /* normal char out */ - } - if ((*fmt == '\n') && (buffer_mode == _IOLBF)) { - fflush(op); - } + charout: + ++cnt; + putc(*fmt, op); /* normal char out */ nextfmt: ++fmt; } - op->mode |= buffer_mode; - if (buffer_mode == _IOLBF) { - op->bufwrite = op->bufpos; - } + i = (__FERROR(op)) ? -1 : cnt; - if (ferror(op)) { - cnt = -1; - } - return (cnt); -} - -#endif + __STDIO_THREADLOCK(op); + return i; +} diff --git a/libc/stdio/perror.c b/libc/stdio/perror.c deleted file mode 100644 index 33b5e08b0..000000000 --- a/libc/stdio/perror.c +++ /dev/null @@ -1,27 +0,0 @@ -#include <string.h> -#include <stdio.h> -#include <errno.h> - -/* - * Manuel Novoa III Feb 2001 - * - * Replaced old version that did write(2,...)'s with a version using - * stream functions. If the program is calling perror, it's a safe - * bet that printf and friends are used as well. It is also possible - * that the calling program could buffer stderr, or reassign it. - * Also, the old version did not conform the standards when the - * passed char * was either NULL or pointed to an empty string. - */ - -void perror(__const char *str) -{ - static const char perror_str[] = ": "; - const char *sep; - - sep = perror_str; - if (!(str && *str)) { /* Caller did not supply a prefix message */ - sep += 2; /* or passed an empty string. */ - str = sep; - } - fprintf(stderr, "%s%s%s\n", str, sep, strerror(errno)); -} diff --git a/libc/stdio/putw.c b/libc/stdio/putw.c deleted file mode 100644 index d1b68ab27..000000000 --- a/libc/stdio/putw.c +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright (C) 1991, 1997, 1998 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#include <stdio.h> - -#ifdef USE_IN_LIBIO -# include <libio/iolibio.h> -# define fwrite(p, n, m, s) _IO_fwrite (p, n, m, s) -#endif - -/* Write the word (int) W to STREAM. */ -int -putw (int w, FILE *stream) -{ - /* Is there a better way? */ - if (fwrite ((const void *) &w, sizeof (w), 1, stream) < 1) - return EOF; - return 0; -} diff --git a/libc/stdio/remove.c b/libc/stdio/remove.c deleted file mode 100644 index af256e4aa..000000000 --- a/libc/stdio/remove.c +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk> - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - */ -#include <unistd.h> -#include <string.h> -#include <sys/types.h> -#include <errno.h> - -int remove(src) -__const char *src; -{ - extern int errno; - int er = errno; - int rv = unlink(src); - - if (rv < 0 && errno == EISDIR) - rv = rmdir(src); - if (rv >= 0) - __set_errno(er); - return rv; -} - diff --git a/libc/stdio/scanf.c b/libc/stdio/scanf.c index c8b487ed1..fc70b9244 100644 --- a/libc/stdio/scanf.c +++ b/libc/stdio/scanf.c @@ -31,14 +31,24 @@ * implementation doesn't for the "100ergs" case mentioned above. */ -#include <stdlib.h> -#include <unistd.h> +#define _GNU_SOURCE +#include <features.h> +#if defined(__UCLIBC__) && !defined(__USE_ISOC99) #define __USE_ISOC99 +#endif + +#define _STDIO_UTILITY #include <stdio.h> +#include <stdlib.h> +#include <unistd.h> #include <ctype.h> #include <string.h> #include <stdarg.h> +#ifdef __STDIO_THREADSAFE +#include <pthread.h> +#endif /* __STDIO_THREADSAFE */ + #ifdef L_scanf #ifdef __STDC__ int scanf(const char *fmt, ...) @@ -59,29 +69,22 @@ va_dcl #endif #ifdef L_sscanf -#ifdef __STDC__ -int sscanf(const char *sp, const char *fmt, ...) +#if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS) +#warning skipping sscanf since no buffering and no custom streams! #else -int sscanf(sp, fmt, va_alist) -__const char *sp; -__const char *fmt; -va_dcl -#endif -{ - FILE string[1] = { - {0, (unsigned char *) ((unsigned) -1), 0, 0, (char *) ((unsigned) -1), - 0, -1, _IOFBF} - }; +int sscanf(const char *sp, const char *fmt, ...) +{ va_list ptr; int rv; - string->bufpos = (unsigned char *) ((void *) sp); va_start(ptr, fmt); - rv = vfscanf(string, fmt, ptr); + rv = vsscanf(sp, fmt, ptr); va_end(ptr); return rv; } + +#endif #endif #ifdef L_fscanf @@ -114,16 +117,37 @@ va_list ap; #endif #ifdef L_vsscanf +#ifdef __STDIO_BUFFERS int vsscanf(__const char *sp, __const char *fmt, va_list ap) { - FILE string[1] = { - {0, (unsigned char *) ((unsigned) -1), 0, 0, (char *) ((unsigned) -1), - 0, -1, _IOFBF} - }; + FILE string[1]; + + string->filedes = -2; /* for debugging */ + string->modeflags = (__FLAG_NARROW|__FLAG_READONLY); + string->bufstart = string->bufrpos = (unsigned char *) ((void *) sp); + string->bufgetc = (char *) ((unsigned) -1); - string->bufpos = (unsigned char *) sp; return vfscanf(string, fmt, ap); } +#else /* __STDIO_BUFFERS */ +#ifdef __STDIO_GLIBC_CUSTOM_STREAMS +int vsscanf(__const char *sp, __const char *fmt, va_list ap) +{ + FILE *f; + int rv; + + if ((f = fmemopen((char *)sp, strlen(sp), "r")) == NULL) { + return -1; + } + rv = vfscanf(f, fmt, ap); + fclose(f); + + return rv; +} +#else /* __STDIO_GLIBC_CUSTOM_STREAMS */ +#warning skipping vsscanf since no buffering and no custom streams! +#endif /* __STDIO_GLIBC_CUSTOM_STREAMS */ +#endif /* __STDIO_BUFFERS */ #endif #ifdef L_vfscanf @@ -154,6 +178,7 @@ struct scan_cookie { int width_flag; int ungot_char; int ungot_flag; + int app_ungot; }; #ifdef __UCLIBC_HAS_LONG_LONG__ @@ -181,8 +206,11 @@ static void init_scan_cookie(struct scan_cookie *sc, FILE *fp) sc->nread = 0; sc->width_flag = 0; sc->ungot_flag = 0; + sc->app_ungot = ((fp->modeflags & __MASK_UNGOT) ? fp->ungot[1] : 0); } +/* TODO -- what about literal '\0' chars in a file??? */ + static int scan_getc_nw(struct scan_cookie *sc) { if (sc->ungot_flag == 0) { @@ -233,6 +261,10 @@ static void kill_scan_cookie(struct scan_cookie *sc) { if (sc->ungot_flag) { ungetc(sc->ungot_char,sc->fp); + /* Deal with distiction between user and scanf ungots. */ + if (sc->nread == 0) { /* Only one char was read... app ungot? */ + sc->fp->ungot[1] = sc->app_ungot; /* restore ungot state. */ + } } } @@ -267,6 +299,8 @@ va_list ap; unsigned char buf[MAX_DIGITS+2]; unsigned char scanset[UCHAR_MAX + 1]; + __STDIO_THREADLOCK(fp); + init_scan_cookie(&sc,fp); fmt = (unsigned const char *) format; @@ -547,7 +581,7 @@ va_list ap; goto done; } /* Unrecognized specifier! */ - goto done; + goto RETURN_cnt; } if (isspace(*fmt)) { /* Consume all whitespace. */ while (isspace(scan_getc_nw(&sc))) {} @@ -567,9 +601,12 @@ va_list ap; kill_scan_cookie(&sc); if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) { - return (EOF); + cnt = EOF; } + RETURN_cnt: + __STDIO_THREADUNLOCK(fp); + return (cnt); } diff --git a/libc/stdio/stdio.c b/libc/stdio/stdio.c deleted file mode 100644 index 4a0dc8188..000000000 --- a/libc/stdio/stdio.c +++ /dev/null @@ -1,1117 +0,0 @@ -/* Copyright (C) 1996 Robert de Bath <rdebath@cix.compulink.co.uk> - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - */ - -/* This is an implementation of the C standard IO package. - * - * Updates: - * 29-Sep-2000 W. Greathouse 1. fgetc copying beyond end of buffer - * 2. stdout needs flushed when input requested on - * stdin. - * 3. bufend was set incorrectly to 4 bytes beyond - * bufin (sizeof a pointer) instead of BUFSIZ. - * This resulted in 4 byte buffers for full - * buffered stdin and stdout and an 8 byte - * buffer for the unbuffered stderr! - */ - -/* - * Feb 27, 2001 Manuel Novoa III - * - * Most of the core functionality has been completely rewritten. - * A number of functions have been added as well, as mandated by C89. - * - * An extension function "fsfopen" has been added: - * Open a file using an automatically (stack) or statically allocated FILE. - * The FILE * returned behaves just as any other FILE * with respect to the - * stdio functions, but be aware of the following: - * NOTE: The buffer used for the file is FILE's builtin 2-byte buffer, so - * setting a new buffer is probably advisable. - * NOTE: This function is primarily intended to be used for stack-allocated - * FILEs when uClibc stdio has no dynamic memory support. - * For the statically allocated case, it is probably better to increase - * the value of FIXED_STREAMS in stdio.c. - * WARNING: If allocated on the stack, make sure you call fclose before the - * stack memory is reclaimed! - */ - - -#include <stdlib.h> -#include <stdio.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/types.h> -#include <malloc.h> -#include <errno.h> -#include <string.h> -#include <assert.h> -#include <limits.h> - -extern off_t _uClibc_fwrite(const unsigned char *buf, off_t bytes, FILE *fp); -extern off_t _uClibc_fread(unsigned char *buf, off_t bytes, FILE *fp); - -/* Used internally to actually open files */ -extern FILE *__fopen __P((__const char *__restrict __filename, int __fd, - FILE *__restrict __stream, __const char *__restrict __mode, - int extra_modes)); - -/* Note: This def of READING is ok since 1st ungetc puts in buf. */ -#define READING(fp) (fp->bufstart < fp->bufread) -#define WRITING(fp) (fp->bufwrite > fp->bufstart) - -#define READABLE(fp) (fp->bufread != 0) -#define WRITEABLE(fp) (fp->bufwrite != 0) -#define EOF_OR_ERROR(fp) (fp->mode & (__MODE_EOF | __MODE_ERR)) - -/***********************************************************************/ -/* BUILD TIME OPTIONS */ -/***********************************************************************/ -/* - * FIXED_STREAMS must be >= 3 and FIXED_BUFFERS must be >= 2. - * As a feature, these can be increased, although this is probably - * only useful if DISABLE_DYNAMIC is set to 1 below. - */ - -#define FIXED_STREAMS 3 -#define FIXED_BUFFERS 2 - -/* - * As a feature, you can build uClibc with no dynamic allocation done - * by the stdio package. Just set DISABLE_DYNAMIC to nonzero. Note that - * use of asprintf, getdelim, or getline will pull malloc into the link. - * - * Note: You can't trust FOPEN_MAX if DISABLE_DYNAMIC != 0. - */ -#define DISABLE_DYNAMIC 0 - -/* - * As a feature, you can try to allow setvbuf calls after file operations. - * Setting FLEXIBLE_SETVBUF to nonzero will cause setvbuf to try to fflush - * any buffered writes or sync the file position for buffered reads. If it - * is successful, the buffer change can then take place. - */ -#define FLEXIBLE_SETVBUF 0 -/***********************************************************************/ - -#if DISABLE_DYNAMIC != 0 -#undef malloc -#undef free -#define malloc(x) 0 -#define free(x) -#endif - -extern FILE *__IO_list; /* For fflush. */ -extern FILE *_free_file_list; -extern char _free_buffer_index; -extern FILE _stdio_streams[FIXED_STREAMS]; -extern unsigned char _fixed_buffers[FIXED_BUFFERS * BUFSIZ]; - -extern unsigned char *_alloc_stdio_buffer(size_t size); -extern void _free_stdio_buffer_of_file(FILE *fp); -extern void _free_stdio_stream(FILE *fp); - -#ifdef L__alloc_stdio_buffer -unsigned char *_alloc_stdio_buffer(size_t size) -{ - unsigned char *buf; - - if ((size == BUFSIZ) && (_free_buffer_index < FIXED_BUFFERS)) { - buf = _fixed_buffers + ((unsigned int)_free_buffer_index) * BUFSIZ; - _free_buffer_index = *buf; - return buf; - } - return malloc(size); -} -#endif - -#ifdef L__free_stdio_buffer_of_file -void _free_stdio_buffer_of_file(FILE *fp) -{ - unsigned char *buf; - - if (!(fp->mode & __MODE_FREEBUF)) { - return; - } - fp->mode &= ~(__MODE_FREEBUF); - buf = fp->bufstart; - - if ((buf >= _fixed_buffers) - && (buf < _fixed_buffers + (FIXED_BUFFERS * BUFSIZ))) { - *buf = _free_buffer_index; - _free_buffer_index = (buf - _fixed_buffers)/BUFSIZ; - return; - } - free(buf); -} |