From 03e039820dc5092e27e81f3671652f25da7f25f1 Mon Sep 17 00:00:00 2001 From: Manuel Novoa III Date: Tue, 12 Mar 2002 01:18:50 +0000 Subject: Swap in the new stdio code. --- include/printf.h | 247 ++++++ include/stdio.h | 92 +- include/stdio_ext.h | 87 ++ libc/inet/addr.c | 17 +- libc/misc/assert/__assert.c | 81 +- libc/misc/internals/Makefile | 8 +- libc/misc/internals/__uClibc_main.c | 18 +- libc/misc/internals/dtostr.c | 423 ---------- libc/misc/internals/lltostr.c | 39 - libc/misc/internals/ltostr.c | 39 - libc/misc/internals/tempname.c | 37 +- libc/misc/internals/ulltostr.c | 37 - libc/misc/internals/ultostr.c | 37 - libc/misc/internals/zoicheck.c | 16 - libc/stdio/Makefile | 75 +- libc/stdio/getw.c | 36 - libc/stdio/old_vfprintf.c | 493 +++++++++++ libc/stdio/perror.c | 27 - libc/stdio/printf.c | 713 ---------------- libc/stdio/putw.c | 34 - libc/stdio/remove.c | 23 - libc/stdio/scanf.c | 83 +- libc/stdio/stdio.c | 1117 ------------------------- libc/stdlib/atexit.c | 13 +- libc/stdlib/ptsname.c | 13 +- libc/string/strerror.c | 26 +- libc/string/strsignal.c | 28 +- libc/sysdeps/linux/common/bits/uClibc_stdio.h | 494 +++++++++++ libpthread/linuxthreads/lockfile.c | 47 +- 29 files changed, 1605 insertions(+), 2795 deletions(-) create mode 100644 include/printf.h create mode 100644 include/stdio_ext.h delete mode 100644 libc/misc/internals/dtostr.c delete mode 100644 libc/misc/internals/lltostr.c delete mode 100644 libc/misc/internals/ltostr.c delete mode 100644 libc/misc/internals/ulltostr.c delete mode 100644 libc/misc/internals/ultostr.c delete mode 100644 libc/misc/internals/zoicheck.c delete mode 100644 libc/stdio/getw.c create mode 100644 libc/stdio/old_vfprintf.c delete mode 100644 libc/stdio/perror.c delete mode 100644 libc/stdio/printf.c delete mode 100644 libc/stdio/putw.c delete mode 100644 libc/stdio/remove.c delete mode 100644 libc/stdio/stdio.c create mode 100644 libc/sysdeps/linux/common/bits/uClibc_stdio.h diff --git a/include/printf.h b/include/printf.h new file mode 100644 index 000000000..8b0a66c88 --- /dev/null +++ b/include/printf.h @@ -0,0 +1,247 @@ +/* Copyright (C) 1991-1993,1995-1999,2000,2001 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. */ + +/* March 11, 2001 Manuel Novoa III + * + * Modified as appropriate for my new stdio lib. + */ + +#ifndef _PRINTF_H + +#define _PRINTF_H 1 +#include + +__BEGIN_DECLS + +#define __need_FILE +#include +#define __need_size_t +#define __need_wchar_t +#include + +/* WARNING -- This is definitely nonportable... but it seems to work + * with gcc, which is currently the only "supported" compiler. + * The library code uses bitmasks for space-efficiency (you can't + * set/test multiple bitfields in one operation). Unfortunatly, we + * need to support bitfields since that's what glibc uses. So, we take + * advantage of how gcc lays out bitfields to create an appropriate + * mapping. By defining __PRINTF_INFO_NO_BITFIELD we access the + * bitfields using bitmasks in a single flag variable. + * + * WARNING -- This may very well fail if built with -fpack-struct!!! + * + * TODO -- Add a validation test. + * TODO -- Add an option to build in a shim translation function if + * the bitfield<->bitmask mapping fails. + */ +/* #define __PRINTF_INFO_NO_BITFIELD */ +#include + +struct printf_info +{ + int prec; /* Precision. */ + int width; /* Width. */ +#ifdef __STDIO_WIDE /* TODO: temporary fix for uClibc */ + wchar_t spec; /* Format letter. */ +#else + int spec; +#endif +#ifndef __PRINTF_INFO_NO_BITFIELD +#if __BYTE_ORDER == __LITTLE_ENDIAN + unsigned int space:1; /* Space flag. */ + unsigned int showsign:1; /* + flag. */ + unsigned int extra:1; /* For special use. */ + unsigned int left:1; /* - flag. */ + unsigned int alt:1; /* # flag. */ + unsigned int group:1; /* ' flag. */ + unsigned int i18n:1; /* I flag. */ + unsigned int wide:1; /* Nonzero for wide character streams. */ + unsigned int is_char:1; /* hh flag. */ + unsigned int is_short:1; /* h flag. */ + unsigned int is_long:1; /* l flag. */ + unsigned int is_long_double:1;/* L flag. */ + +#elif __BYTE_ORDER == __BIG_ENDIAN + + unsigned int __padding:20;/* non-gnu -- total of 32 bits on 32bit arch */ + unsigned int is_long_double:1;/* L flag. */ + unsigned int is_long:1; /* l flag. */ + unsigned int is_short:1; /* h flag. */ + unsigned int is_char:1; /* hh flag. */ + unsigned int wide:1; /* Nonzero for wide character streams. */ + unsigned int i18n:1; /* I flag. */ + unsigned int group:1; /* ' flag. */ + unsigned int alt:1; /* # flag. */ + unsigned int left:1; /* - flag. */ + unsigned int extra:1; /* For special use. */ + unsigned int showsign:1; /* + flag. */ + unsigned int space:1; /* Space flag. */ + +#else +#error unsupported byte order! +#endif + +#define PRINT_INFO_FLAG_VAL(INFO_PTR,BITFIELD) (INFO_PTR)->BITFIELD +#define PRINT_INFO_SET_FLAG(INFO_PTR,BITFIELD) (INFO_PTR)->BITFIELD = 1 +#define PRINT_INFO_CLR_FLAG(INFO_PTR,BITFIELD) (INFO_PTR)->BITFIELD = 0 +#define PRINT_INFO_SET_extra(INFO_PTR,VAL) (INFO_PTR)->extra = (VAL) + +#else /* __PRINTF_INFO_NO_BITFIELD */ + + unsigned int _flags; /* non-gnu */ +#define __PRINT_INFO_FLAG_space (1<<0) +#define __PRINT_INFO_FLAG_showsign (1<<1) +#define __PRINT_INFO_FLAG_extra (1<<2) +#define __PRINT_INFO_FLAG_left (1<<3) +#define __PRINT_INFO_FLAG_alt (1<<4) +#define __PRINT_INFO_FLAG_group (1<<5) +#define __PRINT_INFO_FLAG_i18n (1<<6) +#define __PRINT_INFO_FLAG_wide (1<<7) + +#define __PRINT_INFO_FLAG_is_char (1<<8) +#define __PRINT_INFO_FLAG_is_short (1<<9) +#define __PRINT_INFO_FLAG_is_long (1<<10) +#define __PRINT_INFO_FLAG_is_long_double (1<<11) + +#if defined(__STDC__) && __STDC__ +#define PRINT_INFO_FLAG_VAL(INFO_PTR,BITFIELD) \ + ((INFO_PTR)->_flags & __PRINT_INFO_FLAG_##BITFIELD) +#define PRINT_INFO_SET_FLAG(INFO_PTR,BITFIELD) \ + ((INFO_PTR)->_flags |= __PRINT_INFO_FLAG_##BITFIELD) +#define PRINT_INFO_CLR_FLAG(INFO_PTR,BITFIELD) \ + ((INFO_PTR)->_flags &= ~__PRINT_INFO_FLAG_##BITFIELD) +#else +#define PRINT_INFO_FLAG_VAL(INFO_PTR,BITFIELD) \ + ((INFO_PTR)->_flags & __PRINT_INFO_FLAG_/**/BITFIELD) +#define PRINT_INFO_SET_FLAG(INFO_PTR,BITFIELD) \ + ((INFO_PTR)->_flags |= __PRINT_INFO_FLAG_/**/BITFIELD) +#define PRINT_INFO_CLR_FLAG(INFO_PTR,BITFIELD) \ + ((INFO_PTR)->_flags &= ~__PRINT_INFO_FLAG_/**/BITFIELD) +#endif +#define PRINT_INFO_SET_extra(INFO_PTR,VAL) \ + ((INFO_PTR)->_flags |= (((INFO_PTR)->_flags & ~1) | ((VAL) & 1))) +#endif /* __PRINTF_INFO_NO_BITFIELD */ +#ifdef __STDIO_WIDE /* TODO: temporary fix for uClibc */ + wchar_t pad; /* Padding character. */ +#else + int pad; +#endif +}; + + +/* Type of a printf specifier-handler function. + STREAM is the FILE on which to write output. + INFO gives information about the format specification. + ARGS is a vector of pointers to the argument data; + the number of pointers will be the number returned + by the associated arginfo function for the same INFO. + + The function should return the number of characters written, + or -1 for errors. */ + +typedef int printf_function (FILE *__stream, + __const struct printf_info *__info, + __const void *__const *__args); + +/* Type of a printf specifier-arginfo function. + INFO gives information about the format specification. + N, ARGTYPES, and return value are as for parse_printf_format. */ + +typedef int printf_arginfo_function (__const struct printf_info *__info, + size_t __n, int *__argtypes); + + +/* Register FUNC to be called to format SPEC specifiers; ARGINFO must be + specified to determine how many arguments a SPEC conversion requires and + what their types are. */ + +extern int register_printf_function (int __spec, printf_function __func, + printf_arginfo_function __arginfo); + + +/* Parse FMT, and fill in N elements of ARGTYPES with the + types needed for the conversions FMT specifies. Returns + the number of arguments required by FMT. + + The ARGINFO function registered with a user-defined format is passed a + `struct printf_info' describing the format spec being parsed. A width + or precision of INT_MIN means a `*' was used to indicate that the + width/precision will come from an arg. The function should fill in the + array it is passed with the types of the arguments it wants, and return + the number of arguments it wants. */ + +extern size_t parse_printf_format (__const char *__restrict __fmt, size_t __n, + int *__restrict __argtypes) __THROW; + + +/* Codes returned by `parse_printf_format' for basic types. + + These values cover all the standard format specifications. + Users can add new values after PA_LAST for their own types. */ + +/* WARNING -- The above is not entirely true, even for glibc. + * As far as the library code is concerned, such args are treated + * as 'your type' pointers if qualified by PA_FLAG_PTR. If they + * aren't qualified as pointers, I _think_ glibc just ignores them + * and carries on. I think it should be treated as an error. */ + +enum +{ /* C type: */ + PA_INT, /* int */ + PA_CHAR, /* int, cast to char */ + PA_WCHAR, /* wide char */ + PA_STRING, /* const char *, a '\0'-terminated string */ + PA_WSTRING, /* const wchar_t *, wide character string */ + PA_POINTER, /* void * */ + PA_FLOAT, /* float */ + PA_DOUBLE, /* double */ + __PA_NOARG, /* non-glibc -- signals non-arg width or prec */ + PA_LAST +}; + +/* Flag bits that can be set in a type returned by `parse_printf_format'. */ +/* WARNING -- These differ in value from what glibc uses. */ +#define PA_FLAG_MASK (0xff00) +#define __PA_FLAG_CHAR (0x0100) /* non-gnu -- to deal with hh */ +#define PA_FLAG_SHORT (0x0200) +#define PA_FLAG_LONG (0x0400) +#define PA_FLAG_LONG_LONG (0x0800) +#define PA_FLAG_LONG_DOUBLE PA_FLAG_LONG_LONG +#define PA_FLAG_PTR (0x1000) /* TODO -- make dynamic??? */ + +#define __PA_INTMASK (0x0f00) /* non-gnu -- all int flags */ + +/* Function which can be registered as `printf'-handlers. */ + +/* Print floating point value using using abbreviations for the orders + of magnitude used for numbers ('k' for kilo, 'm' for mega etc). If + the format specifier is a uppercase character powers of 1000 are + used. Otherwise powers of 1024. */ +extern int printf_size (FILE *__restrict __fp, + __const struct printf_info *__info, + __const void *__const *__restrict __args) __THROW; + +/* This is the appropriate argument information function for `printf_size'. */ +extern int printf_size_info (__const struct printf_info *__restrict + __info, size_t __n, int *__restrict __argtypes) + __THROW; + + +__END_DECLS + +#endif /* printf.h */ diff --git a/include/stdio.h b/include/stdio.h index e418b2f0b..f39ab8481 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -1,4 +1,4 @@ -/* Define ISO C stdio on top of C++ iostreams. +/* Copyright (C) 1991, 1994-1999, 2000, 2001, 2002 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -37,32 +37,11 @@ __BEGIN_DECLS # define __need_FILE # define __need___FILE #endif /* Don't need FILE. */ -#include #if !defined __FILE_defined && defined __need_FILE /* The opaque type of streams. This is the definition used elsewhere. */ - -/* when you add or change fields here, be sure to change the initialization - * in stdio_init and fopen */ - -struct _UC_FILE { - unsigned char *bufpos; /* the next byte to write to or read from */ - unsigned char *bufread; /* the end of data returned by last read() */ - unsigned char *bufwrite; /* 1 + highest address writable by macro */ - unsigned char *bufstart; /* the start of the buffer */ - unsigned char *bufend; /* the end of the buffer; ie the byte after the last - malloc()ed byte */ - struct _UC_FILE * next; - - int fd; /* the file descriptor associated with the stream */ - - unsigned char mode; - unsigned char ungot; - unsigned char unbuf[2]; /* The buffer for 'unbuffered' streams */ -}; - typedef struct _UC_FILE FILE; # define __FILE_defined 1 @@ -83,39 +62,33 @@ typedef struct _UC_FILE __FILE; #ifdef _STDIO_H #undef _STDIO_USES_IOSTREAM +#include + +#include + /* This define avoids name pollution if we're using GNU stdarg.h */ # define __need___va_list #include - /* The type of the second argument to `fgetpos' and `fsetpos'. */ #ifndef __USE_FILE_OFFSET64 -typedef __off_t fpos_t; +typedef _UC_fpos_t fpos_t; #else -typedef __off64_t fpos_t; +typedef _UC_fpos64_t fpos_t; #endif #ifdef __USE_LARGEFILE64 -typedef __off64_t fpos64_t; +typedef _UC_fpos64_t fpos64_t; #endif /* The possibilities for the third argument to `setvbuf'. */ -#define _IOFBF 0 /* Fully buffered. */ -#define _IOLBF 1 /* Line buffered. */ -#define _IONBF 2 /* No buffering. */ - -/* Possible states for a file stream -- internal use only */ -#define __MODE_BUF 0x03 /* Modal buffering dependent on isatty */ -#define __MODE_FREEBUF 0x04 /* Buffer allocated by stdio code, can free */ -#define __MODE_FREEFIL 0x08 /* FILE allocated by stdio code, can free */ -#define __MODE_UNGOT 0x10 /* Buffer has been polluted by ungetc */ -#define __MODE_TIED 0x20 /* FILE is tied with stdin/stdout */ -#define __MODE_EOF 0x40 /* EOF status */ -#define __MODE_ERR 0x80 /* Error status */ +#define _IOFBF _UC_IOFBF /* Fully buffered. */ +#define _IOLBF _UC_IOLBF /* Line buffered. */ +#define _IONBF _UC_IONBF /* No buffering. */ /* Default buffer size. */ #ifndef BUFSIZ -# define BUFSIZ (512) +# define BUFSIZ _UC_BUFSIZ #endif @@ -155,10 +128,12 @@ typedef __off64_t fpos64_t; extern FILE *stdin; /* Standard input stream. */ extern FILE *stdout; /* Standard output stream. */ extern FILE *stderr; /* Standard error output stream. */ +#ifdef __STDC__ /* C89/C99 say they're macros. Make them happy. */ #define stdin stdin #define stdout stdout #define stderr stderr +#endif /* Remove file FILENAME. */ extern int remove (__const char *__filename) __THROW; @@ -212,8 +187,7 @@ extern int fflush (FILE *__stream) __THROW; extern int fflush_unlocked (FILE *__stream) __THROW; #endif -#if 0 -/*#ifdef __USE_GNU*/ +#ifdef __USE_GNU /* Close all streams. */ extern int fcloseall (void) __THROW; #endif @@ -254,8 +228,8 @@ extern FILE *freopen64 (__const char *__restrict __filename, extern FILE *fdopen (int __fd, __const char *__modes) __THROW; #endif -#if 0 -/*#ifdef __USE_GNU*/ +#ifdef __USE_GNU +#ifdef __STDIO_GLIBC_CUSTOM_STREAMS /* Create a new stream that refers to the given magic cookie, and uses the given functions for input and output. */ extern FILE *fopencookie (void *__restrict __magic_cookie, @@ -271,6 +245,7 @@ extern FILE *fmemopen (void *__s, size_t __len, __const char *__modes) __THROW; extern FILE *open_memstream (char **__restrict __bufloc, size_t *__restrict __sizeloc) __THROW; #endif +#endif /* If BUF is NULL, make STREAM unbuffered. @@ -380,9 +355,7 @@ extern int getchar (void) __THROW; /* The C standard explicitly says this is a macro, so we always do the optimization for it. */ -#define getc(stream) \ - (((stream)->bufpos >= (stream)->bufread) ? fgetc(stream): \ - (*(stream)->bufpos++)) +#define getc(_fp) __GETC(_fp) #if defined __USE_POSIX || defined __USE_MISC /* These are defined in POSIX.1:1996. */ @@ -405,9 +378,7 @@ extern int putchar (int __c) __THROW; /* The C standard explicitly says this can be a macro, so we always do the optimization for it. */ -#define putc(c, stream) \ - (((stream)->bufpos >= (stream)->bufwrite) ? fputc((c), (stream)) \ - : (unsigned char) (*(stream)->bufpos++ = (c)) ) +#define putc(_ch, _fp) __PUTC(_ch, _fp) #ifdef __USE_MISC /* Faster version when locking is not necessary. */ @@ -434,8 +405,7 @@ extern int putw (int __w, FILE *__stream) __THROW; extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) __THROW; -#if 0 -/*#ifdef __USE_GNU*/ +#ifdef __USE_GNU /* This function does the same as `fgets' but does not lock the stream. */ extern char *fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream) __THROW; @@ -452,15 +422,15 @@ extern char *gets (char *__s) __THROW; NULL), pointing to *N characters of space. It is realloc'd as necessary. Returns the number of characters read (not including the null terminator), or -1 on error or EOF. */ -extern ssize_t __getdelim (char **__restrict __lineptr, +extern __ssize_t __getdelim (char **__restrict __lineptr, size_t *__restrict __n, int __delimiter, FILE *__restrict __stream) __THROW; -extern ssize_t getdelim (char **__restrict __lineptr, +extern __ssize_t getdelim (char **__restrict __lineptr, size_t *__restrict __n, int __delimiter, FILE *__restrict __stream) __THROW; /* Like `getdelim', but reads up to a newline. */ -extern ssize_t getline (char **__restrict __lineptr, +extern __ssize_t getline (char **__restrict __lineptr, size_t *__restrict __n, FILE *__restrict __stream) __THROW; #endif @@ -470,8 +440,7 @@ extern ssize_t getline (char **__restrict __lineptr, extern int fputs (__const char *__restrict __s, FILE *__restrict __stream) __THROW; -#if 0 -/*#ifdef __USE_GNU*/ +#ifdef __USE_GNU /* This function does the same as `fputs' but does not lock the stream. */ extern int fputs_unlocked (__const char *__restrict __s, FILE *__restrict __stream) __THROW; @@ -582,7 +551,7 @@ extern int sys_nerr; extern __const char *__const sys_errlist[]; #endif #if 0 -/*#ifdef __USE_GNU*/ +/* #ifdef __USE_GNU */ extern int _sys_nerr; extern __const char *__const _sys_errlist[]; #endif @@ -591,8 +560,6 @@ extern __const char *__const _sys_errlist[]; #ifdef __USE_POSIX /* Return the system file descriptor for STREAM. */ extern int fileno (FILE *__stream) __THROW; -/* Only use the macro below if you know fp is a valid FILE for a valid fd. */ -#define __fileno(fp) ((fp)->fd) #endif /* Use POSIX. */ #ifdef __USE_MISC @@ -624,7 +591,7 @@ extern char *cuserid (char *__s) __THROW; #if 0 -/*#ifdef __USE_GNU*/ +/* #ifdef __USE_GNU */ struct obstack; /* See . */ /* Write formatted output to an obstack. */ @@ -636,7 +603,7 @@ extern int obstack_vprintf (struct obstack *__restrict __obstack, #endif /* Use GNU. */ -#if defined __USE_POSIX || defined __USE_MISC +#if (defined __USE_POSIX || defined __USE_MISC) && defined __UCLIBC_HAS_THREADS__ /* These are defined in POSIX.1:1996. */ /* Acquire ownership of STREAM. */ @@ -660,7 +627,8 @@ extern void funlockfile (FILE *__stream) __THROW; /* If we are compiling with optimizing read this file. It contains several optimizing inline functions and macros. */ -#ifdef __USE_EXTERN_INLINES +#if 0 +/* #ifdef __USE_EXTERN_INLINES */ # include #endif diff --git a/include/stdio_ext.h b/include/stdio_ext.h new file mode 100644 index 000000000..55586ea84 --- /dev/null +++ b/include/stdio_ext.h @@ -0,0 +1,87 @@ +/* Functions to access FILE structure internals. + Copyright (C) 2000, 2001 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. */ + +/* This header contains the same definitions as the header of the same name + on Sun's Solaris OS. */ + +#ifndef _STDIO_EXT_H +#define _STDIO_EXT_H 1 + +#include + +enum +{ + /* Query current state of the locking status. */ + FSETLOCKING_QUERY = 0, +#define FSETLOCKING_QUERY FSETLOCKING_QUERY + /* The library protects all uses of the stream functions, except for + uses of the *_unlocked functions, by calls equivalent to flockfile(). */ + FSETLOCKING_INTERNAL, +#define FSETLOCKING_INTERNAL FSETLOCKING_INTERNAL + /* The user will take care of locking. */ + FSETLOCKING_BYCALLER +#define FSETLOCKING_BYCALLER FSETLOCKING_BYCALLER +}; + + +__BEGIN_DECLS + +/* Return the size of the buffer of FP in bytes currently in use by + the given stream. */ +extern size_t __fbufsize (FILE *__fp); + + +/* Return non-zero value iff the stream FP is opened readonly, or if the + last operation on the stream was a read operation. */ +extern int __freading (FILE *__fp); + +/* Return non-zero value iff the stream FP is opened write-only or + append-only, or if the last operation on the stream was a write + operation. */ +extern int __fwriting (FILE *__fp); + + +/* Return non-zero value iff stream FP is not opened write-only or + append-only. */ +extern int __freadable (FILE *__fp); + +/* Return non-zero value iff stream FP is not opened read-only. */ +extern int __fwritable (FILE *__fp); + + +/* Return non-zero value iff the stream FP is line-buffered. */ +extern int __flbf (FILE *__fp); + + +/* Discard all pending buffered I/O on the stream FP. */ +extern void __fpurge (FILE *__fp); + +/* Return amount of output in bytes pending on a stream FP. */ +extern size_t __fpending (FILE *__fp); + +/* Flush all line-buffered files. */ +extern void _flushlbf (void); + + +/* Set locking status of stream FP to TYPE. */ +extern int __fsetlocking (FILE *__fp, int __type); + +__END_DECLS + +#endif /* stdio_ext.h */ diff --git a/libc/inet/addr.c b/libc/inet/addr.c index d1f9c04e6..6b143c6ba 100644 --- a/libc/inet/addr.c +++ b/libc/inet/addr.c @@ -10,10 +10,16 @@ * are smaller than the previous functions and don't require static buffers. * In the process, removed the reference to strcat and cut object size of * inet_ntoa in half (from 190 bytes down to 94). + * + * Manuel Novoa III Feb 2002 + * + * Changed to use _int10tostr. */ #define __FORCE_GLIBC #include +#define _STDIO_UTILITY /* For _int10tostr. */ +#include #include #include #include @@ -80,15 +86,6 @@ const char *cp; #ifdef L_inet_ntoa -#include - -#if (ULONG_MAX >> 32) -/* We're set up for 32 bit unsigned longs */ -#error need to check size allocation for static buffer 'buf' -#endif - -extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase); - char *inet_ntoa(in) struct in_addr in; { @@ -101,7 +98,7 @@ struct in_addr in; q = 0; p = buf + sizeof(buf) - 1; for (i=0 ; i < 4 ; i++ ) { - p = __ultostr(p, addr & 0xff, 10, 0 ) - 1; + p = _int10tostr(p, addr & 0xff) - 1; addr >>= 8; if (q) { *q = '.'; diff --git a/libc/misc/assert/__assert.c b/libc/misc/assert/__assert.c index b98958f43..fba9dc719 100644 --- a/libc/misc/assert/__assert.c +++ b/libc/misc/assert/__assert.c @@ -1,49 +1,52 @@ -/* Copyright (C) 1996 Robert de Bath - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - */ - -/* - * Manuel Novoa III Dec 2000 +/* Copyright (C) 2002 Manuel Novoa III + * An __assert() function compatible with the modified glibc assert.h + * that is used by uClibc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. * - * Converted to use my new (un)signed long (long) to string routines, which - * are smaller than the previous functions and don't require static buffers. + * This 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include -#include +#define _STDIO_UTILITY /* For _stdio_fdout and _int10tostr. */ +#include #include -#include - -#if (INT_MAX >> 31) -/* We're set up for 32 bit ints */ -#error need to check size allocation for buffer 'buf' -#endif - -extern char *__ltostr(char *buf, unsigned long uval, int base, int uppercase); +#include -static void errput(str) -const char *str; -{ - write(2, str, strlen(str)); -} +/* Get the prototype from assert.h as a double-check. */ +#undef NDEBUG +#include +#undef assert -void __assert(assertion, filename, linenumber, function) -const char *assertion; -const char *filename; -int linenumber; -const char *function; +void __assert(const char *assertion, const char * filename, + int linenumber, const char * function) { - char buf[12]; + char buf[__BUFLEN_INT10TOSTR]; - errput(filename); - errput(":"); - errput(__ltostr(buf + sizeof(buf) - 1, linenumber, 10, 0)); - errput(function ? ": " : ""); - errput(function ? function : ""); - errput(function ? "() " : ""); - errput(": Assertion \""); - errput(assertion); - errput("\" failed.\n"); + _stdio_fdout(STDERR_FILENO, +#if 0 + program_name, /* TODO: support program_name like glibc? */ + ": ", +#endif + filename, + ":", + _int10tostr(buf+sizeof(buf)-1, linenumber), + ": ", + /* Function name isn't available with some compilers. */ + ((function == NULL) ? "?function?" : function), + ": Assertion `", + assertion, + "' failed.\n", + NULL + ); abort(); } diff --git a/libc/misc/internals/Makefile b/libc/misc/internals/Makefile index 6e1c2c29d..c35c636ef 100644 --- a/libc/misc/internals/Makefile +++ b/libc/misc/internals/Makefile @@ -24,13 +24,7 @@ TOPDIR=../../../ include $(TOPDIR)Rules.mak -CSRC=ultostr.c ltostr.c __uClibc_main.c tempname.c errno.c __errno_location.c __h_errno_location.c -ifeq ($(HAS_FLOATING_POINT),true) - CSRC += dtostr.c zoicheck.c -endif -ifeq ($(HAS_LONG_LONG),true) - CSRC += ulltostr.c lltostr.c -endif +CSRC=__uClibc_main.c tempname.c errno.c __errno_location.c __h_errno_location.c COBJS=$(patsubst %.c,%.o, $(CSRC)) OBJS=$(COBJS) diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c index 7f5e6e805..7c2eaf35c 100644 --- a/libc/misc/internals/__uClibc_main.c +++ b/libc/misc/internals/__uClibc_main.c @@ -28,13 +28,13 @@ void __uClibc_main(int argc, char **argv, char **envp) #ifdef HAVE_ELF weak_alias(__environ, environ); -extern void weak_function __init_stdio(void); -extern void weak_function __stdio_flush_buffers(void); +extern void weak_function _stdio_init(void); +extern void weak_function _stdio_term(void); extern int *weak_const_function __errno_location (void); extern int *weak_const_function __h_errno_location (void); #else -extern void __init_stdio(void); -extern void __stdio_flush_buffers(void); +extern void _stdio_init(void); +extern void _stdio_term(void); extern int *__errno_location (void); extern int *__h_errno_location (void); #endif @@ -71,8 +71,8 @@ void __uClibc_main(int argc, char **argv, char **envp) * Initialize stdio here. In the static library case, this will * be bypassed if not needed because of the weak alias above. */ - if (__init_stdio) - __init_stdio(); + if (_stdio_init) + _stdio_init(); /* * Note: It is possible that any initialization done above could @@ -95,7 +95,7 @@ void __uClibc_main(int argc, char **argv, char **envp) /* * Define an empty function and use it as a weak alias for the stdio * initialization routine. That way we don't pull in all the stdio - * code unless we need to. Similarly, do the same for __stdio_flush_buffers + * code unless we need to. Similarly, do the same for _stdio_term * so as not to include atexit unnecessarily. * * NOTE!!! This is only true for the _static_ case!!! @@ -106,7 +106,7 @@ weak_alias(__environ, environ); void __uClibc_empty_func(void) { } -weak_alias(__uClibc_empty_func, __init_stdio); -weak_alias(__uClibc_empty_func, __stdio_flush_buffers); +weak_alias(__uClibc_empty_func, _stdio_init); +weak_alias(__uClibc_empty_func, _stdio_term); #endif #endif diff --git a/libc/misc/internals/dtostr.c b/libc/misc/internals/dtostr.c deleted file mode 100644 index 85ec69b12..000000000 --- a/libc/misc/internals/dtostr.c +++ /dev/null @@ -1,423 +0,0 @@ -/* - * Copyright (C) 2000, 2001 Manuel Novoa III - * - * Function: int __dtostr(FILE * fp, size_t size, long double x, - * char flag[], int width, int preci, char mode) - * - * This was written for uClibc to provide floating point support for - * the printf functions. It handles +/- infinity and nan on i386. - * - * Notes: - * - * At most MAX_DIGITS significant digits are kept. Any trailing digits - * are treated as 0 as they are really just the results of rounding noise - * anyway. If you want to do better, use an arbitary precision arithmetic - * package. ;-) - * - * It should also be fairly portable, as not assumptions are made about the - * bit-layout of doubles. - * - * It should be too difficult to convert this to handle long doubles on i386. - * For information, see the comments below. - * - * TODO: - * long double and/or float version? (note: for float can trim code some). - * - * Decrease the size. This is really much bigger than I'd like. - */ - -/*****************************************************************************/ -/* Don't change anything that follows unless you know what you're doing. */ -/*****************************************************************************/ - -/* - * Configuration for the scaling power table. Ignoring denormals, you - * should have 2**EXP_TABLE_SIZE >= LDBL_MAX_EXP >= 2**(EXP_TABLE_SIZE-1). - * The minimum for standard C is 6. For IEEE 8bit doubles, 9 suffices. - * For long doubles on i386, use 13. - */ -#define EXP_TABLE_SIZE 13 - -/* - * Set this to the maximum number of digits you want converted. - * Conversion is done in blocks of DIGITS_PER_BLOCK (9 by default) digits. - * (20) 17 digits suffices to uniquely determine a (long) double on i386. - */ -#define MAX_DIGITS 20 - -/* - * Set this to the smallest integer type capable of storing a pointer. - */ -#define INT_OR_PTR int - -/* - * This is really only used to check for infinities. The macro produces - * smaller code for i386 and, since this is tested before any floating point - * calculations, it doesn't appear to suffer from the excess precision problem - * caused by the FPU that strtod had. If it causes problems, call the function - * and compile zoicheck.c with -ffloat-store. - */ -#define _zero_or_inf_check(x) ( x == (x/4) ) - -/* - * Fairly portable nan check. Bitwise for i386 generated larger code. - * If you have a better version, comment this out. - */ -#define isnan(x) (x != x) - -/*****************************************************************************/ -/* Don't change anything that follows peroid!!! ;-) */ -/*****************************************************************************/ - -#include -#include -#include -#include -#include - -extern int fnprintf(FILE * fp, size_t size, const char *fmt, ...); - -/* from printf.c -- should really be in an internal header file */ -enum { - FLAG_PLUS = 0, - FLAG_MINUS_LJUSTIFY, - FLAG_HASH, - FLAG_0_PAD, - FLAG_SPACE, -}; - -/*****************************************************************************/ - -/* - * Set things up for the scaling power table. - */ - -#if EXP_TABLE_SIZE < 6 -#error EXP_TABLE_SIZE should be at least 6 to comply with standards -#endif - -#define EXP_TABLE_MAX (1U<<(EXP_TABLE_SIZE-1)) - -/* - * Only bother checking if this is too small. - */ - -#if LDBL_MAX_10_EXP/2 > EXP_TABLE_MAX -#error larger EXP_TABLE_SIZE needed -#endif - -/* - * With 32 bit ints, we can get 9 digits per block. - */ -#define DIGITS_PER_BLOCK 9 - -#if (INT_MAX >> 30) -#define DIGIT_BLOCK_TYPE int -#define DB_FMT "%.*d" -#elif (LONG_MAX >> 30) -#define DIGIT_BLOCK_TYPE long -#define DB_FMT "%.*ld" -#else -#error need at least 32 bit longs -#endif - -/* Are there actually any machines where this might fail? */ -#if 'A' > 'a' -#error ordering assumption violated : 'A' > 'a' -#endif - -/* Maximum number of calls to fnprintf to output double. */ -#define MAX_CALLS 8 - -/*****************************************************************************/ - -#define NUM_DIGIT_BLOCKS ((MAX_DIGITS+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK) - -/* extra space for '-', '.', 'e+###', and nul */ -#define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK ) -/*****************************************************************************/ - -static const char *fmts[] = { - "%0*d", "%.*s", ".", "inf", "INF", "nan", "NAN", "%*s" -}; - -/*****************************************************************************/ - -int __dtostr(FILE * fp, size_t size, long double x, - char flag[], int width, int preci, char mode) -{ - long double exp_table[EXP_TABLE_SIZE]; - long double p10; - DIGIT_BLOCK_TYPE digit_block; /* int of at least 32 bits */ - int i, j; - int round, o_exp; - int exp, exp_neg; - char *s; - char *e; - char buf[BUF_SIZE]; - INT_OR_PTR pc_fwi[2*MAX_CALLS]; - INT_OR_PTR *ppc; - char exp_buf[8]; - char drvr[8]; - char *pdrvr; - int npc; - int cnt; - char sign_str[2]; - char o_mode; - - /* check that INT_OR_PTR is sufficiently large */ - assert( sizeof(INT_OR_PTR) == sizeof(char *) ); - - *sign_str = flag[FLAG_PLUS]; - *(sign_str+1) = 0; - if (isnan(x)) { /* nan check */ - pdrvr = drvr + 1; - *pdrvr++ = 5 + (mode < 'a'); - pc_fwi[2] = 3; - flag[FLAG_0_PAD] = 0; - goto EXIT_SPECIAL; - } - - if (x == 0) { /* handle 0 now to avoid false positive */ - exp = -1; - goto GENERATE_DIGITS; - } - - if (x < 0) { /* convert negatives to positives */ - *sign_str = '-'; - x = -x; - } - - if (_zero_or_inf_check(x)) { /* must be inf since zero handled above */ - pdrvr = drvr + 1; - *pdrvr++ = 3 + + (mode < 'a'); - pc_fwi[2] = 3; - flag[FLAG_0_PAD] = 0; - goto EXIT_SPECIAL; - } - - /* need to build the scaling table */ - for (i = 0, p10 = 10 ; i < EXP_TABLE_SIZE ; i++) { - exp_table[i] = p10; - p10 *= p10; - } - - exp_neg = 0; - if (x < 1e8) { /* do we need to scale up or down? */ - exp_neg = 1; - } - - exp = DIGITS_PER_BLOCK - 1; - - i = EXP_TABLE_SIZE; - j = EXP_TABLE_MAX; - while ( i-- ) { /* scale x such that 1e8 <= x < 1e9 */ - if (exp_neg) { - if (x * exp_table[i] < 1e9) { - x *= exp_table[i]; - exp -= j; - } - } else { - if (x / exp_table[i] >= 1e8) { - x /= exp_table[i]; - exp += j; - } - } - j >>= 1; - } - if (x >= 1e9) { /* handle bad rounding case */ - x /= 10; - ++exp; - } - assert(x < 1e9); - - GENERATE_DIGITS: - s = buf + 2; /* leave space for '\0' and '0' */ - - for (i = 0 ; i < NUM_DIGIT_BLOCKS ; ++i ) { - digit_block = (DIGIT_BLOCK_TYPE) x; - x = (x - digit_block) * 1e9; - s += sprintf(s, DB_FMT, DIGITS_PER_BLOCK, digit_block); - } - - /*************************************************************************/ - - *exp_buf = 'e'; - if (mode < 'a') { - *exp_buf = 'E'; - mode += ('a' - 'A'); - } - - o_mode = mode; - - round = preci; - - if ((mode == 'g') && (round > 0)){ - --round; - } - - if (mode == 'f') { - round += exp; - } - - s = buf; - *s++ = 0; /* terminator for rounding and 0-triming */ - *s = '0'; /* space to round */ - - i = 0; - e = s + MAX_DIGITS + 1; - if (round < MAX_DIGITS) { - e = s + round + 2; - if (*e >= '5') { - i = 1; - } - } - - do { /* handle rounding and trim trailing 0s */ - *--e += i; /* add the carry */ - } while ((*e == '0') || (*e > '9')); - - o_exp = exp; - if (e <= s) { /* we carried into extra digit */ - ++o_exp; - e = s; /* needed if all 0s */ - } else { - ++s; - } - *++e = 0; /* ending nul char */ - - if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) { - mode = 'f'; - } - - exp = o_exp; - if (mode != 'f') { - o_exp = 0; - } - - if (o_exp < 0) { - *--s = '0'; /* fake the first digit */ - } - - pdrvr = drvr+1; - ppc = pc_fwi+2; - - *pdrvr++ = 0; - *ppc++ = 1; - *ppc++ = (INT_OR_PTR)(*s++ - '0'); - - i = e - s; /* total digits */ - if (o_exp >= 0) { - if (o_exp >= i) { /* all digit(s) left of decimal */ - *pdrvr++ = 1; - *ppc++ = i; - *ppc++ = (INT_OR_PTR)(s); - o_exp -= i; - i = 0; - if (o_exp>0) { /* have 0s left of decimal */ - *pdrvr++ = 0; - *ppc++ = o_exp; - *ppc++ = 0; - } - } else if (o_exp > 0) { /* decimal between digits */ - *pdrvr++ = 1; - *ppc++ = o_exp; - *ppc++ = (INT_OR_PTR)(s); - s += o_exp; - i -= o_exp; - } - o_exp = -1; - } - - if (flag[FLAG_HASH] || (i) || ((o_mode != 'g') && (preci > 0))) { - *pdrvr++ = 2; /* need decimal */ - *ppc++ = 1; /* needed for width calc */ - ppc++; - } - - if (++o_exp < 0) { /* have 0s right of decimal */ - *pdrvr++ = 0; - *ppc++ = -o_exp; - *ppc++ = 0; - } - if (i) { /* have digit(s) right of decimal */ - *pdrvr++ = 1; - *ppc++ = i; - *ppc++ = (INT_OR_PTR)(s); - } - - if (o_mode != 'g') { - i -= o_exp; - if (i < preci) { /* have 0s right of digits */ - i = preci - i; - *pdrvr++ = 0; - *ppc++ = i; - *ppc++ = 0; - } - } - - /* build exponent string */ - if (mode != 'f') { - *pdrvr++ = 1; - *ppc++ = sprintf(exp_buf,"%c%+.2d", *exp_buf, exp); - *ppc++ = (INT_OR_PTR) exp_buf; - } - - EXIT_SPECIAL: - npc = pdrvr - drvr; - ppc = pc_fwi + 2; - for (i=1 ; i< npc ; i++) { - width -= *(ppc++); - ppc++; - } - i = 0; - if (*sign_str) { - i = 1; - } - width -= i; - if (width <= 0) { - width = 0; - } else { - if (flag[FLAG_MINUS_LJUSTIFY]) { /* padding on right */ - ++npc; - *pdrvr++ = 7; - *ppc = width; - *++ppc = (INT_OR_PTR)(""); - width = 0; - } else if (flag[FLAG_0_PAD] == '0') { /* 0 padding */ - pc_fwi[2] += width; - width = 0; - } - } - *drvr = 7; - ppc = pc_fwi; - *ppc++ = width + i; - *ppc = (INT_OR_PTR) sign_str; - - pdrvr = drvr; - ppc = pc_fwi; - cnt = 0; - for (i=0 ; i *ppc) { - size -= *ppc; - } - cnt += *ppc; /* to avoid problems if j == -1 */ - ppc += 2; - } - - return cnt; -} - - - - - - diff --git a/libc/misc/internals/lltostr.c b/libc/misc/internals/lltostr.c deleted file mode 100644 index 2ce359615..000000000 --- a/libc/misc/internals/lltostr.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2000 Manuel Novoa III - * - * Note: buf is a pointer to the END of the buffer passed. - * Call like this: - * char buf[SIZE], *p; - * p = __lltostr(buf + sizeof(buf) - 1, ...) - * For long longs of 64 bits, appropriate buffer sizes are: - * base = 2 66 = 1 (possible -) sign + 64 digits + 1 nul - * base = 10 21 = 1 (possible -) sign + 19 digits + 1 nul - * base = 16 18 = 1 (possible -) sign + 16 hex digits + 1 nul - */ - -extern char *__ulltostr(char *buf, unsigned long long uval, int base, - int uppercase); - -char *__lltostr(char *buf, long long val, int base, int uppercase) -{ - unsigned long long uval; - char *pos; - int negative; - - negative = 0; - if (val < 0) { - negative = 1; - uval = ((unsigned long long)(-(1+val))) + 1; - } else { - uval = val; - } - - - pos = __ulltostr(buf, uval, base, uppercase); - - if (pos && negative) { - *--pos = '-'; - } - - return pos; -} diff --git a/libc/misc/internals/ltostr.c b/libc/misc/internals/ltostr.c deleted file mode 100644 index 7e45fec30..000000000 --- a/libc/misc/internals/ltostr.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2000 Manuel Novoa III - * - * Note: buf is a pointer to the END of the buffer passed. - * Call like this: - * char buf[SIZE], *p; - * p = __ltostr(buf + sizeof(buf) - 1, ...) - * - * For longs of 32 bits, appropriate buffer sizes are: - * base = 2 34 = 1 (possible -) sign + 32 digits + 1 nul - * base = 10 12 = 1 (possible -) sign + 10 digits + 1 nul - * base = 16 10 = 1 (possible -) sign + 8 hex digits + 1 nul - */ - -extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase); - -char *__ltostr(char *buf, long val, int base, int uppercase) -{ - unsigned long uval; - char *pos; - int negative; - - negative = 0; - if (val < 0) { - negative = 1; - uval = ((unsigned long)(-(1+val))) + 1; - } else { - uval = val; - } - - - pos = __ultostr(buf, uval, base, uppercase); - - if (pos && negative) { - *--pos = '-'; - } - - return pos; -} diff --git a/libc/misc/internals/tempname.c b/libc/misc/internals/tempname.c index 3c43f9b85..41325d998 100644 --- a/libc/misc/internals/tempname.c +++ b/libc/misc/internals/tempname.c @@ -16,6 +16,11 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/* March 11, 2002 Manuel Novoa III + * + * Modify code to remove dependency on libgcc long long arith support funcs. + */ + #include #include #include @@ -117,8 +122,11 @@ int __gen_tempname (char *tmpl, int openit) char *XXXXXX; static uint64_t value; struct timeval tv; + uint32_t high, low, rh; + unsigned int k; int count, fd; int save_errno = errno; + int i; len = strlen (tmpl); if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) @@ -136,20 +144,21 @@ int __gen_tempname (char *tmpl, int openit) for (count = 0; count < TMP_MAX; value += 7777, ++count) { - uint64_t v = value; - - /* Fill in the random bits. */ - XXXXXX[0] = letters[v % 62]; - v /= 62; - XXXXXX[1] = letters[v % 62]; - v /= 62; - XXXXXX[2] = letters[v % 62]; - v /= 62; - XXXXXX[3] = letters[v % 62]; - v /= 62; - XXXXXX[4] = letters[v % 62]; - v /= 62; - XXXXXX[5] = letters[v % 62]; + low = value & UINT32_MAX; + high = value >> 32; + + for (i = 0 ; i < 6 ; i++) { + rh = high % 62; + high /= 62; +#define L ((UINT32_MAX % 62 + 1) % 62) + k = (low % 62) + (L * rh); +#undef L +#define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62)) + low = (low / 62) + (H * rh) + (k / 62); +#undef H + k %= 62; + XXXXXX[i] = letters[k]; + } if (openit) { diff --git a/libc/misc/internals/ulltostr.c b/libc/misc/internals/ulltostr.c deleted file mode 100644 index 50246d3bc..000000000 --- a/libc/misc/internals/ulltostr.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2000 Manuel Novoa III - * - * Note: buf is a pointer to the END of the buffer passed. - * Call like this: - * char buf[SIZE], *p; - * p = __ulltostr(buf + sizeof(buf) - 1, ...) - * - * For long longs of 64 bits, appropriate buffer sizes are: - * base = 2 65 = 64 digits + 1 nul - * base = 10 20 = 19 digits + 1 nul - * base = 16 17 = 16 hex digits + 1 nul - */ - -char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase) -{ - int digit; - - if ((base < 2) || (base > 36)) { - return 0; - } - - *buf = '\0'; - - do { - digit = uval % base; - uval /= base; - - /* note: slightly slower but generates less code */ - *--buf = '0' + digit; - if (digit > 9) { - *buf = (uppercase ? 'A' : 'a') + digit - 10; - } - } while (uval); - - return buf; -} diff --git a/libc/misc/internals/ultostr.c b/libc/misc/internals/ultostr.c deleted file mode 100644 index d4663fb39..000000000 --- a/libc/misc/internals/ultostr.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2000 Manuel Novoa III - * - * Note: buf is a pointer to the END of the buffer passed. - * Call like this; - * char buf[SIZE], *p; - * p = __ultostr(buf + sizeof(buf) - 1, ...) - * - * For longs of 32 bits, appropriate buffer sizes are: - * base = 2 33 = 32 digits + 1 nul - * base = 10 11 = 10 digits + 1 nul - * base = 16 9 = 8 hex digits + 1 nul - */ - -char *__ultostr(char *buf, unsigned long uval, int base, int uppercase) -{ - int digit; - - if ((base < 2) || (base > 36)) { - return 0; - } - - *buf = '\0'; - - do { - digit = uval % base; - uval /= base; - - /* note: slightly slower but generates less code */ - *--buf = '0' + digit; - if (digit > 9) { - *buf = (uppercase ? 'A' : 'a') + digit - 10; - } - } while (uval); - - return buf; -} diff --git a/libc/misc/internals/zoicheck.c b/libc/misc/internals/zoicheck.c deleted file mode 100644 index 2113c6298..000000000 --- a/libc/misc/internals/zoicheck.c +++ /dev/null @@ -1,16 +0,0 @@ - -/* - * Copyright (C) 2000 Manuel Novoa III - * - * This is a utility routine for strtod errno support. - * As the name implies, it checks if a double is either 0 or +/-infinity. - * Doing this inline doesn't work on i386 because of excess precission - * stored in the FPU. - * - * TODO: Check bitmasks directly? - */ - -int _zero_or_inf_check(double x) -{ - return ( x == x/4 ); -} 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 - -#ifdef USE_IN_LIBIO -# include -# 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/old_vfprintf.c b/libc/stdio/old_vfprintf.c new file mode 100644 index 000000000..c7c07f34a --- /dev/null +++ b/libc/stdio/old_vfprintf.c @@ -0,0 +1,493 @@ +/* + * This file based on printf.c from 'Dlibs' on the atari ST (RdeBath) + * + * + * Dale Schumacher 399 Beacon Ave. + * (alias: Dalnefre') St. Paul, MN 55104 + * dal@syntel.UUCP United States of America + * "It's not reality that's important, but how you perceive things." + */ + +/* Altered to use stdarg, made the core function vfnprintf. + * Hooked into the stdio package using 'inside information' + * Altered sizeof() assumptions, now assumes all integers except chars + * will be either + * sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short) + * + * -RDB + */ + +/* + * Manuel Novoa III Dec 2000 + * + * The previous vfnprintf routine was almost completely rewritten with the + * goal of fixing some shortcomings and reducing object size. + * + * The summary of changes: + * + * Converted print conversion specification parsing from one big switch + * to a method using string tables. This new method verifies that the + * conversion flags, field width, precision, qualifier, and specifier + * appear in the correct order. Many questionable specifications were + * accepted by the previous code. This new method also resulted in a + * substantial reduction in object size of about 330 bytes (20%) from + * the old version (1627 bytes) on i386, even with the following + * improvements. + * + * Implemented %n specifier as required by the standards. + * Implemented proper handling of precision for int types. + * Implemented # for hex and pointer, fixed error for octal rep of 0. + * Implemented return of -1 on stream error. + * + * Added optional support for the GNU extension %m which prints the string + * corresponding the errno. + * + * Added optional support for long long ints and unsigned long long ints + * using the conversion qualifiers "ll", "L", or "q" (like glibc). + * + * Added optional support for doubles in a very limited form. None of + * the formating options are obeyed. The string returned by __dtostr + * is printed directly. + * + * Converted to use my (un)signed long (long) to string routines, which are + * smaller than the previous functions and don't require static buffers. + * + * Other Modifications: + * Modified sprintf, snprintf, vsprintf, vsnprintf to share on fake-file. + */ + +/* + * Manuel Novoa III Jan 2001 + * + * Removed fake file from *s*printf functions because of possible problems + * if called recursively. Instead, have sprintf, snprintf, and vsprintf + * call vsnprintf which allocates a fake file on the stack. + * Removed WANT_FPUTC option. Always use standard putc macro to avoid + * problems with the fake file used by the *s*printf functions. + * Fixed bug parsing flags -- did not restart scan. + * Added function asprintf. + * Fixed 0-pad prefixing bug. + * Converted sizeof(int) == sizeof(long) tests to compile time vs run time. + * This saves 112 bytes of code on i386. + * Fixed precision bug -- when negative set to default. + * Added function fnprintf to support __dtostr. + * Added floating point support for doubles. Yeah! + * + * + * May 2001 Fixes from Johan Adolfsson (johan.adolfsson@axis.com) + * 1) printf("%c",0) returned 0 instead of 1. + * 2) unrolled loop in asprintf to reduce size and remove compile warning. + * + * + * June 2001 + * 1) fix %p so that "0x" is prepended to outputed hex val + * 2) fix %p so that "(nil)" is output for (void *)0 to match glibc + * + */ + +/*****************************************************************************/ +/* OPTIONS */ +/*****************************************************************************/ +/* The optional support for long longs and doubles comes in two forms. + * + * 1) Normal (or partial for doubles) output support. Set to 1 to turn on. + * Adds about 130 bytes for doubles, about 220 bytes for long longs, + * and about 275 for both to the base code size of 1163 on i386. + */ + +/* These are now set in uClibc_config.h based on Config. */ +/* +#define __UCLIBC_HAS_FLOATS__ 1 +*/ + +/* 2) An error message is inserted into the stream, an arg of the + * appropriate size is removed from the arglist, and processing + * continues. This is adds less code and may be useful in some + * cases. Set to 1 to turn on. Adds about 50 bytes for doubles, + * about 140 bytes for long longs, and about 175 bytes for both + * to the base code size of 1163 on i386. + */ + +#define WANT_FLOAT_ERROR 0 + +/* + * Set to support GNU extension of %m to print string corresponding to errno. + * + * Warning: This adds about 50 bytes (i386) to the code but it also pulls in + * strerror and the corresponding string table which together are about 3.8k. + */ + +#define WANT_GNU_ERRNO 0 + +/**************************************************************************/ + +#define _GNU_SOURCE /* for strnlen */ +#define _STDIO_UTILITY +#include +#include +#include +#include +#include + +#define __PRINTF_INFO_NO_BITFIELD +#include + +#ifdef __STDIO_THREADSAFE +#include +#endif /* __STDIO_THREADSAFE */ + +/* #undef __UCLIBC_HAS_FLOATS__ */ +/* #undef WANT_FLOAT_ERROR */ +/* #define WANT_FLOAT_ERROR 1 */ + +#define __isdigit(c) (((unsigned int)(c - '0')) < 10) + +extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info); + + +enum { + FLAG_PLUS = 0, + FLAG_MINUS_LJUSTIFY, + FLAG_HASH, + FLAG_0_PAD, + FLAG_SPACE, +}; + +/* layout 01234 */ +static const char spec[] = "+-#0 "; + +static const char qual[] = "hlLq"; + +#if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR +static const char dbl_err[] = ""; +#endif + +#if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR +/* layout 012345678901234567 */ +static const char u_spec[] = "%nbopxXudicsfgGeEaA"; +#else +/* layout 0123456789012 */ +static const char u_spec[] = "%nbopxXudics"; +#endif + +/* WARNING: u_spec and u_radix need to stay in agreement!!! */ +/* u_radix[i] <-> u_spec[i+2] for unsigned entries only */ +static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a"; + +int vfprintf(FILE * __restrict op, register const char * __restrict fmt, + va_list ap) +{ + int i, cnt, lval, len; + char *p; + const char *fmt0; + int preci, width; +#define upcase i + int radix, dpoint /*, upcase*/; + char tmp[65]; /* TODO - determing needed size from headers */ + char flag[sizeof(spec)]; + + __STDIO_THREADLOCK(op); + + cnt = 0; + + while (*fmt) { + if (*fmt == '%') { + fmt0 = fmt; /* save our position in case of bad format */ + ++fmt; + width = -1; /* min field width */ + preci = -5; /* max string width or mininum digits */ + radix = 10; /* number base */ + dpoint = 0; /* found decimal point */ +#if INT_MAX != LONG_MAX + lval = 0; /* sizeof(int) != sizeof(long) */ +#else + lval = 1; /* sizeof(int) == sizeof(long) */ +#endif + + /* init flags */ + for (p =(char *) spec ; *p ; p++) { + flag[p-spec] = '\0'; + } + flag[FLAG_0_PAD] = ' '; + + /* process optional flags */ + for (p = (char *)spec ; *p ; ) { + if (*fmt == *p) { + flag[p-spec] = *fmt++; + p = (char *)spec; /* restart scan */ + } else { + p++; + } + } + + if (!flag[FLAG_PLUS]) { + flag[FLAG_PLUS] = flag[FLAG_SPACE]; + } + + /* process optional width and precision */ + do { + if (*fmt == '.') { + ++fmt; + dpoint = 1; + } + if (*fmt == '*') { /* parameter width value */ + ++fmt; + i = va_arg(ap, int); + } else { + for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) { + i = (i * 10) + (*fmt - '0'); + } + } + + if (dpoint) { + preci = i; + if (i<0) { + preci = -5; + } + } else { + width = i; + if (i<0) { + width = -i; + flag[FLAG_MINUS_LJUSTIFY] = 1; + } + } + } while ((*fmt == '.') && !dpoint ); + + /* process optional qualifier */ + for (p = (char *) qual ; *p ; p++) { + if (*p == *fmt) { + lval = p - qual; + ++fmt; /* TODO - hh */ + if ((*p == 'l') && (*fmt == *p)) { + ++lval; + ++fmt; + } + } + } + +#if WANT_GNU_ERRNO + if (*fmt == 'm') { + flag[FLAG_PLUS] = '\0'; + flag[FLAG_0_PAD] = ' '; + p = strerror(errno); + goto print; + } +#endif + + /* process format specifier */ + for (p = (char *) u_spec ; *p ; p++) { + if (*fmt != *p) continue; + if (p-u_spec < 1) { /* print a % */ + goto charout; + } + if (p-u_spec < 2) { /* store output count in int ptr */ + *(va_arg(ap, int *)) = cnt; + goto nextfmt; + } + if (p-u_spec < 8) { /* unsigned conversion */ + radix = u_radix[p-u_spec-2]; + upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER); + if (*p == 'p') { + lval = (sizeof(char *) == sizeof(long)); + upcase = __UIM_LOWER; + flag[FLAG_HASH] = 'p'; + } + + 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); + + flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */ + if (*p != '0') { /* non-zero */ + if (flag[FLAG_HASH]) { + if (radix == 8) { + *--p = '0'; /* add leadding zero */ + } else if (radix != 10) { /* either 2 or 16 */ + flag[FLAG_PLUS] = '0'; + *--p = 'b'; + if (radix == 16) { + *p = 'x'; + if (*fmt == 'X') { + *p = 'X'; + } + } + } + } + } else if (flag[FLAG_HASH] == 'p') { /* null pointer */ + p = "(nil)"; + } + } else if (p-u_spec < 10) { /* signed conversion */ + + 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] = ' '; + if (*p == 'c') { /* character */ + p = tmp; + *p = va_arg(ap, int); + /* This takes care of the "%c",0 case */ + len = 1; + goto print_len_set; + } else { /* string */ + 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; + } + 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) + : va_arg(ap, double)); /* carry on */ + p = (char *) dbl_err; +#endif /* defined(__UCLIBC_HAS_FLOATS__) */ + } + +#if WANT_GNU_ERRNO + print: +#endif + { /* this used to be printfield */ + /* cheaper than strlen call */ +/* for ( len = 0 ; p[len] ; len++ ) { } */ + len = strnlen(p, SIZE_MAX); + print_len_set: + if ((*p == '-') +#if WANT_GNU_ERRNO + && (*fmt != 'm') +#endif + && (*fmt != 's')) { + f