From: Joerg Sonnenberger Date: Sat, 23 Jul 2005 20:23:06 +0000 (+0000) Subject: First step to cleaning up stdio. This breaks the libc ABI, all programs X-Git-Tag: v2.0.1~6508 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/55b5af8438bae897e96d192972b15905aa65edb6 First step to cleaning up stdio. This breaks the libc ABI, all programs have to be recompiled. Make FILE an opaque type for normal operation (anything outside libc). This means programs have to use the exported interface, they can neither make static instances on the heap or access fields of their own. Introduce a new type __FILE_public, which contains the fields accessed by the various macros. It is placed first in the real FILE and the macros cast the given FILE * to __FILE_public for access. To allow better argument checks, all macros have been converted to inline functions instead. Merge the various stdio helper headers into a single priv_stdio.h. The license from the original files has been kept, the third clause is gone as part of the UCB copyright addendum. They haven't been changed in FreeBSD at all. Add two new helper functions, fcookie and __fpending to read parts of the hidden state. The former is handy for funopen users, the latter exists on other systems as well. Cleanup some minor warnings on the way and hide some local functions with static. Adept libftpio and CVS to the chanced API. --- diff --git a/gnu/usr.bin/cvs/lib/Makefile b/gnu/usr.bin/cvs/lib/Makefile index 1bf7182584..74a791830e 100644 --- a/gnu/usr.bin/cvs/lib/Makefile +++ b/gnu/usr.bin/cvs/lib/Makefile @@ -1,5 +1,5 @@ # $FreeBSD: src/gnu/usr.bin/cvs/lib/Makefile,v 1.19.2.4 2003/01/21 23:06:52 peter Exp $ -# $DragonFly: src/gnu/usr.bin/cvs/lib/Makefile,v 1.11 2005/06/13 22:35:54 corecode Exp $ +# $DragonFly: src/gnu/usr.bin/cvs/lib/Makefile,v 1.12 2005/07/23 20:23:06 joerg Exp $ .include "${.CURDIR}/../Makefile.inc" @@ -31,7 +31,7 @@ SRCS= config.h \ pagealign_alloc.c getline.c getndelim2.c mktime.c \ getpass.c rpmatch.c vasnprintf.c printf-args.c printf-parse.c \ asnprintf.c getcwd.c closeout.c chdir-long.c quotearg.c \ - __fpending.c memrchr.c openat.c + memrchr.c openat.c config.h: config.h.proto sed -e "s,@VERSION@,${VERSION}-DragonFly,g" \ diff --git a/gnu/usr.bin/cvs/lib/config.h.proto b/gnu/usr.bin/cvs/lib/config.h.proto index a144a37c37..5c4b74418f 100644 --- a/gnu/usr.bin/cvs/lib/config.h.proto +++ b/gnu/usr.bin/cvs/lib/config.h.proto @@ -1,4 +1,4 @@ -/* $DragonFly: src/gnu/usr.bin/cvs/lib/config.h.proto,v 1.7 2005/06/13 22:35:54 corecode Exp $ */ +/* $DragonFly: src/gnu/usr.bin/cvs/lib/config.h.proto,v 1.8 2005/07/23 20:23:06 joerg Exp $ */ /* config.h. Generated by configure. */ /* config.h.in. Generated from configure.in by autoheader. */ @@ -233,7 +233,7 @@ /* Define to 1 if you have the declaration of `__fpending', and to 0 if you don't. */ -#define HAVE_DECL___FPENDING 0 +#define HAVE_DECL___FPENDING 1 /* Define to 1 if you have the header file. */ /* #undef HAVE_DIRECT_H */ @@ -732,7 +732,7 @@ #define HAVE__BOOL 1 /* Define to 1 if you have the `__fpending' function. */ -/* #undef HAVE___FPENDING */ +#define HAVE___FPENDING /* Define to 1 if you have the `__secure_getenv' function. */ /* #undef HAVE___SECURE_GETENV */ diff --git a/include/stdio.h b/include/stdio.h index a806e1e9da..8a2a23a53d 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -35,7 +35,7 @@ * * @(#)stdio.h 8.5 (Berkeley) 4/29/95 * $FreeBSD: src/include/stdio.h,v 1.24.2.5 2002/11/09 08:07:20 imp Exp $ - * $DragonFly: src/include/stdio.h,v 1.7 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/include/stdio.h,v 1.8 2005/07/23 20:23:06 joerg Exp $ */ #ifndef _STDIO_H_ @@ -60,22 +60,6 @@ typedef __size_t size_t; typedef __off_t fpos_t; -#define _FSTDIO /* Define for new stdio with functions. */ - -/* - * NB: to fit things in six character monocase externals, the stdio - * code uses the prefix `__s' for stdio objects, typically followed - * by a three-character attempt at a mnemonic. - */ - -/* stdio buffers */ -struct __sbuf { - unsigned char *_base; - int _size; -}; - -struct __sFILEX; - /* * stdio state variables. * @@ -94,46 +78,18 @@ struct __sFILEX; * _lbfsize is used only to make the inline line-buffered output stream * code as compact as possible. * - * _ub, _up, and _ur are used when ungetc() pushes back more characters - * than fit in the current _bf, or when ungetc() pushes back a character - * that does not match the previous one in _bf. When this happens, - * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff - * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. - * - * NB: see WARNING above before changing the layout of this structure! + * WARNING: Do not change the order of the fields in __FILE_public! */ -typedef struct __sFILE { - unsigned char *_p; /* current position in (some) buffer */ - int _r; /* read space left for getc() */ - int _w; /* write space left for putc() */ - short _flags; /* flags, below; this FILE is free if 0 */ - short _file; /* fileno, if Unix descriptor, else -1 */ - struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ - int _lbfsize; /* 0 or -_bf._size, for inline putc */ - - /* operations */ - void *_cookie; /* cookie passed to io functions */ - int (*_close) (void *); - int (*_read) (void *, char *, int); - fpos_t (*_seek) (void *, fpos_t, int); - int (*_write) (void *, const char *, int); - - /* separate buffer for long sequences of ungetc() */ - struct __sbuf _ub; /* ungetc buffer */ - struct __sFILEX *_extra; - int _ur; /* saved _r when _r is counting ungetc data */ - - /* tricks to meet minimum requirements even when malloc() fails */ - unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ - unsigned char _nbuf[1]; /* guarantee a getc() buffer */ - - /* separate buffer for fgetln() when line crosses buffer boundary */ - struct __sbuf _lb; /* buffer for fgetln() */ - - /* Unix stdio files get aligned to block boundaries on fseek() */ - int _blksize; /* stat.st_blksize (may be != _bf._size) */ - fpos_t _offset; /* current lseek offset (see WARNING) */ -} FILE; +typedef struct __FILE FILE; + +struct __FILE_public { + unsigned char *_p; /* current position in (some) buffer */ + int _flags; /* flags, below; this FILE is free if 0 */ + int _fileno; /* fileno, if Unix descriptor, else -1 */ + __ssize_t _r; /* read space left for getc() */ + __ssize_t _w; /* write space left for putc() */ + __ssize_t _lbfsize; /* 0 or -_bf._size, for inline putc */ +}; __BEGIN_DECLS extern FILE *__stdinp, *__stdoutp, *__stderrp; @@ -303,6 +259,7 @@ __END_DECLS __BEGIN_DECLS int asprintf (char **, const char *, ...) __printflike(2, 3); char *ctermid_r (char *); +void *fcookie(FILE *); char *fgetln (FILE *, size_t *); #if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ >= 3 #define __ATTR_FORMAT_ARG __attribute__((__format_arg__(2))) @@ -310,6 +267,7 @@ char *fgetln (FILE *, size_t *); #define __ATTR_FORMAT_ARG #endif __const char *fmtcheck (const char *, const char *) __ATTR_FORMAT_ARG; +__ssize_t __fpending(FILE *); int fpurge (FILE *); int fseeko (FILE *, __off_t, int); __off_t ftello (FILE *); @@ -362,35 +320,62 @@ int __swbuf (int, FILE *); __END_DECLS /* - * The __sfoo macros are here so that we can - * define function versions in the C library. + * The __sfoo functions are here so that we can + * define real function versions in the C library. */ -#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) -#if defined(__GNUC__) && defined(__STDC__) -static __inline int __sputc(int _c, FILE *_p) { - if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) +static __inline int +__sgetc(FILE *_fp) +{ + struct __FILE_public *_p = (struct __FILE_public *)_fp; + + if (--_p->_r < 0) + return (__srget(_fp)); + else + return (*_p->_p++); +} + +static __inline int +__sputc(int _c, FILE *_fp) +{ + struct __FILE_public *_p = (struct __FILE_public *)_fp; + + if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && _c != '\n')) return (*_p->_p++ = _c); else - return (__swbuf(_c, _p)); + return (__swbuf(_c, _fp)); } -#else -/* - * This has been tuned to generate reasonable code on the vax using pcc. - */ -#define __sputc(c, p) \ - (--(p)->_w < 0 ? \ - (p)->_w >= (p)->_lbfsize ? \ - (*(p)->_p = (c)), *(p)->_p != '\n' ? \ - (int)*(p)->_p++ : \ - __swbuf('\n', p) : \ - __swbuf((int)(c), p) : \ - (*(p)->_p = (c), (int)*(p)->_p++)) -#endif -#define __sfeof(p) (((p)->_flags & __SEOF) != 0) -#define __sferror(p) (((p)->_flags & __SERR) != 0) -#define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) -#define __sfileno(p) ((p)->_file) +static __inline int +__sfeof(FILE *_fp) +{ + struct __FILE_public *_p = (struct __FILE_public *)_fp; + + return ((_p->_flags & __SEOF) != 0); +} + +static __inline int +__sferror(FILE *_fp) +{ + struct __FILE_public *_p = (struct __FILE_public *)_fp; + + return ((_p->_flags & __SERR) != 0); +} + +static __inline void +__sclearerr(FILE *_fp) +{ + struct __FILE_public *_p = (struct __FILE_public *)_fp; + + _p->_flags &= ~(__SERR|__SEOF); +} + +static __inline int +__sfileno(FILE *_fp) +{ + struct __FILE_public *_p = (struct __FILE_public *)_fp; + + return (_p->_fileno); +} /* * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12 diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index a45d704cfa..8737aabb04 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -1,12 +1,12 @@ # @(#)Makefile.inc 8.3 (Berkeley) 4/17/94 # $FreeBSD: src/lib/libc/stdio/Makefile.inc,v 1.19.2.1 2001/04/25 10:04:11 ru Exp $ -# $DragonFly: src/lib/libc/stdio/Makefile.inc,v 1.2 2003/06/17 04:26:45 dillon Exp $ +# $DragonFly: src/lib/libc/stdio/Makefile.inc,v 1.3 2005/07/23 20:23:05 joerg Exp $ # stdio sources .PATH: ${.CURDIR}/../libc/stdio -SRCS+= _flock_stub.c \ - asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \ +SRCS+= __fpending.c _flock_stub.c \ + asprintf.c clrerr.c fcookie.c fclose.c fdopen.c feof.c ferror.c fflush.c \ fgetc.c fgetln.c fgetpos.c fgets.c fileno.c findfp.c flags.c fopen.c \ fprintf.c fpurge.c fputc.c fputs.c fread.c freopen.c fscanf.c \ fseek.c fsetpos.c ftell.c funopen.c fvwrite.c fwalk.c fwrite.c \ diff --git a/lib/libc/stdio/__fpending.c b/lib/libc/stdio/__fpending.c new file mode 100644 index 0000000000..fa2b68bace --- /dev/null +++ b/lib/libc/stdio/__fpending.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Joerg Sonnenberger . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/lib/libc/stdio/__fpending.c,v 1.1 2005/07/23 20:23:05 joerg Exp $ + */ + +#include + +#include "priv_stdio.h" + +__ssize_t +__fpending(FILE *fp) +{ + return(fp->pub._p - fp->_bf._base); +} diff --git a/lib/libc/stdio/_flock_stub.c b/lib/libc/stdio/_flock_stub.c index a70aea8c5b..02c4816b88 100644 --- a/lib/libc/stdio/_flock_stub.c +++ b/lib/libc/stdio/_flock_stub.c @@ -30,7 +30,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/_flock_stub.c,v 1.3 1999/08/28 00:00:55 peter Exp $ - * $DragonFly: src/lib/libc/stdio/_flock_stub.c,v 1.8 2005/05/11 12:45:57 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/_flock_stub.c,v 1.9 2005/07/23 20:23:05 joerg Exp $ * */ @@ -40,6 +40,7 @@ #include "un-namespace.h" #include "local.h" +#include "priv_stdio.h" void __flockfile(FILE *fp); void __flockfile_debug(FILE *fp, char *fname, int lineno); diff --git a/lib/libc/stdio/asprintf.c b/lib/libc/stdio/asprintf.c index a4c9f8587e..e700cf3360 100644 --- a/lib/libc/stdio/asprintf.c +++ b/lib/libc/stdio/asprintf.c @@ -27,7 +27,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/asprintf.c,v 1.6 1999/08/28 00:00:55 peter Exp $ - * $DragonFly: src/lib/libc/stdio/asprintf.c,v 1.5 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/asprintf.c,v 1.6 2005/07/23 20:23:05 joerg Exp $ */ #include @@ -36,6 +36,7 @@ #include #include "local.h" +#include "priv_stdio.h" int asprintf(char **str, char const *fmt, ...) @@ -45,15 +46,15 @@ asprintf(char **str, char const *fmt, ...) FILE f; struct __sFILEX ext; - f._file = -1; - f._flags = __SWR | __SSTR | __SALC; - f._bf._base = f._p = (unsigned char *)malloc(128); + f.pub._fileno = -1; + f.pub._flags = __SWR | __SSTR | __SALC; + f._bf._base = f.pub._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) { *str = NULL; errno = ENOMEM; return (-1); } - f._bf._size = f._w = 127; /* Leave room for the NUL */ + f._bf._size = f.pub._w = 127; /* Leave room for the NUL */ f._extra = &ext; INITEXTRA(&f); va_start(ap, fmt); @@ -65,7 +66,7 @@ asprintf(char **str, char const *fmt, ...) errno = ENOMEM; return (-1); } - *f._p = '\0'; + *f.pub._p = '\0'; *str = (char *)f._bf._base; return (ret); } diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c index e770b46366..1e1a56e9b1 100644 --- a/lib/libc/stdio/fclose.c +++ b/lib/libc/stdio/fclose.c @@ -35,7 +35,7 @@ * * @(#)fclose.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fclose.c,v 1.8 1999/11/21 22:34:57 dt Exp $ - * $DragonFly: src/lib/libc/stdio/fclose.c,v 1.8 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fclose.c,v 1.9 2005/07/23 20:23:05 joerg Exp $ */ #include "namespace.h" @@ -43,23 +43,25 @@ #include #include #include "un-namespace.h" + #include "libc_private.h" #include "local.h" +#include "priv_stdio.h" int fclose(FILE *fp) { int r; - if (fp->_flags == 0) { /* not open! */ + if (fp->pub._flags == 0) { /* not open! */ errno = EBADF; return (EOF); } FLOCKFILE(fp); - r = fp->_flags & __SWR ? __sflush(fp) : 0; + r = fp->pub._flags & __SWR ? __sflush(fp) : 0; if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) r = EOF; - if (fp->_flags & __SMBF) + if (fp->pub._flags & __SMBF) free((char *)fp->_bf._base); if (HASUB(fp)) FREEUB(fp); @@ -74,14 +76,14 @@ fclose(FILE *fp) * locking code. */ FUNLOCKFILE(fp); - fp->_file = -1; - fp->_r = fp->_w = 0; /* Mess up if reaccessed. */ + fp->pub._fileno = -1; + fp->pub._r = fp->pub._w = 0; /* Mess up if reaccessed. */ #if 0 if (fp->_lock != NULL) { _pthread_mutex_destroy((pthread_mutex_t *)&fp->_lock); fp->_lock = NULL; } #endif - fp->_flags = 0; /* Release this FILE for reuse. */ + fp->pub._flags = 0; /* Release this FILE for reuse. */ return (r); } diff --git a/lib/libc/stdio/fcookie.c b/lib/libc/stdio/fcookie.c new file mode 100644 index 0000000000..af581e2691 --- /dev/null +++ b/lib/libc/stdio/fcookie.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Joerg Sonnenberger . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/lib/libc/stdio/fcookie.c,v 1.1 2005/07/23 20:23:05 joerg Exp $ + */ + +#include + +#include "priv_stdio.h" + +void * +_fcookie(FILE *fp) +{ + return(fp->_cookie); +} + +__weak_reference(_fcookie, fcookie); diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index c72fc8d265..87d1284cc2 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/fdopen.c,v 1.3 2000/01/27 23:06:44 jasone Exp $ - * $DragonFly: src/lib/libc/stdio/fdopen.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fdopen.c,v 1.6 2005/07/23 20:23:05 joerg Exp $ * * @(#)fdopen.c 8.1 (Berkeley) 6/4/93 */ @@ -46,7 +46,9 @@ #include #include #include "un-namespace.h" + #include "local.h" +#include "priv_stdio.h" FILE * fdopen(int fd, const char *mode) @@ -72,15 +74,15 @@ fdopen(int fd, const char *mode) if ((fp = __sfp()) == NULL) return (NULL); - fp->_flags = flags; + fp->pub._flags = flags; /* * If opened for appending, but underlying descriptor does not have * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to * end before each write. */ if ((oflags & O_APPEND) && !(fdflags & O_APPEND)) - fp->_flags |= __SAPP; - fp->_file = fd; + fp->pub._flags |= __SAPP; + fp->pub._fileno = fd; fp->_cookie = fp; fp->_read = __sread; fp->_write = __swrite; diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c index f7356e6958..03024c2def 100644 --- a/lib/libc/stdio/fflush.c +++ b/lib/libc/stdio/fflush.c @@ -35,7 +35,7 @@ * * @(#)fflush.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fflush.c,v 1.7 1999/08/28 00:00:58 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fflush.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fflush.c,v 1.6 2005/07/23 20:23:05 joerg Exp $ */ #include "namespace.h" @@ -43,7 +43,9 @@ #include #include "un-namespace.h" #include "libc_private.h" + #include "local.h" +#include "priv_stdio.h" /* * Flush a single file, or (if fp is NULL) all files. @@ -57,7 +59,7 @@ fflush(FILE *fp) if (fp == NULL) return (_fwalk(__sflush)); FLOCKFILE(fp); - if ((fp->_flags & (__SWR | __SRW)) == 0) { + if ((fp->pub._flags & (__SWR | __SRW)) == 0) { errno = EBADF; retval = EOF; } else @@ -78,7 +80,7 @@ __fflush(FILE *fp) if (fp == NULL) return (_fwalk(__sflush)); - if ((fp->_flags & (__SWR | __SRW)) == 0) { + if ((fp->pub._flags & (__SWR | __SRW)) == 0) { errno = EBADF; retval = EOF; } else @@ -92,26 +94,26 @@ __sflush(FILE *fp) unsigned char *p; int n, t; - t = fp->_flags; + t = fp->pub._flags; if ((t & __SWR) == 0) return (0); if ((p = fp->_bf._base) == NULL) return (0); - n = fp->_p - p; /* write this much */ + n = fp->pub._p - p; /* write this much */ /* * Set these immediately to avoid problems with longjmp and to allow * exchange buffering (via setvbuf) in user write function. */ - fp->_p = p; - fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; + fp->pub._p = p; + fp->pub._w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; for (; n > 0; n -= t, p += t) { t = (*fp->_write)(fp->_cookie, (char *)p, n); if (t <= 0) { - fp->_flags |= __SERR; + fp->pub._flags |= __SERR; return (EOF); } } diff --git a/lib/libc/stdio/fgetc.c b/lib/libc/stdio/fgetc.c index 368982c804..bb4b1f1cd0 100644 --- a/lib/libc/stdio/fgetc.c +++ b/lib/libc/stdio/fgetc.c @@ -35,12 +35,13 @@ * * @(#)fgetc.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fgetc.c,v 1.7 1999/08/28 00:00:58 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fgetc.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fgetc.c,v 1.5 2005/07/23 20:23:05 joerg Exp $ */ #include "namespace.h" #include #include "un-namespace.h" + #include "libc_private.h" int diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c index c09505f294..8006cbb31f 100644 --- a/lib/libc/stdio/fgetln.c +++ b/lib/libc/stdio/fgetln.c @@ -35,13 +35,14 @@ * * @(#)fgetln.c 8.2 (Berkeley) 1/2/94 * $FreeBSD: src/lib/libc/stdio/fgetln.c,v 1.6 1999/08/28 00:00:59 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fgetln.c,v 1.4 2004/06/07 20:35:41 hmp Exp $ + * $DragonFly: src/lib/libc/stdio/fgetln.c,v 1.5 2005/07/23 20:23:05 joerg Exp $ */ #include #include #include #include "local.h" +#include "priv_stdio.h" /* * Expand the line buffer. Return -1 on error. @@ -50,7 +51,7 @@ * so we add 1 here. #endif */ -int +static int __slbexpand(FILE *fp, size_t newsize) { void *p; @@ -82,13 +83,13 @@ fgetln(FILE *fp, size_t *lenp) size_t off; /* make sure there is input */ - if (fp->_r <= 0 && __srefill(fp)) { + if (fp->pub._r <= 0 && __srefill(fp)) { *lenp = 0; return (NULL); } /* look for a newline in the input */ - if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) != NULL) { + if ((p = memchr((void *)fp->pub._p, '\n', (size_t)fp->pub._r)) != NULL) { char *ret; /* @@ -97,11 +98,11 @@ fgetln(FILE *fp, size_t *lenp) * the text. */ p++; /* advance over it */ - ret = (char *)fp->_p; - *lenp = len = p - fp->_p; - fp->_flags |= __SMOD; - fp->_r -= len; - fp->_p = p; + ret = (char *)fp->pub._p; + *lenp = len = p - fp->pub._p; + fp->pub._flags |= __SMOD; + fp->pub._r -= len; + fp->pub._p = p; return (ret); } @@ -115,7 +116,7 @@ fgetln(FILE *fp, size_t *lenp) */ #define OPTIMISTIC 80 - for (len = fp->_r, off = 0;; len += fp->_r) { + for (len = fp->pub._r, off = 0;; len += fp->pub._r) { size_t diff; /* @@ -125,24 +126,25 @@ fgetln(FILE *fp, size_t *lenp) */ if (__slbexpand(fp, len + OPTIMISTIC)) goto error; - (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, + (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->pub._p, len - off); off = len; if (__srefill(fp)) break; /* EOF or error: return partial line */ - if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) == NULL) + if ((p = memchr((void *)fp->pub._p, '\n', (size_t)fp->pub._r)) + == NULL) continue; /* got it: finish up the line (like code above) */ p++; - diff = p - fp->_p; + diff = p - fp->pub._p; len += diff; if (__slbexpand(fp, len)) goto error; - (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, + (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->pub._p, diff); - fp->_r -= diff; - fp->_p = p; + fp->pub._r -= diff; + fp->pub._p = p; break; } *lenp = len; diff --git a/lib/libc/stdio/fgets.c b/lib/libc/stdio/fgets.c index 539f726562..a8b785d14e 100644 --- a/lib/libc/stdio/fgets.c +++ b/lib/libc/stdio/fgets.c @@ -35,7 +35,7 @@ * * @(#)fgets.c 8.2 (Berkeley) 12/22/93 * $FreeBSD: src/lib/libc/stdio/fgets.c,v 1.9 1999/08/28 00:01:00 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fgets.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fgets.c,v 1.6 2005/07/23 20:23:05 joerg Exp $ */ #include "namespace.h" @@ -43,7 +43,9 @@ #include #include "un-namespace.h" #include "local.h" + #include "libc_private.h" +#include "priv_stdio.h" /* * Read at most n-1 characters from the given file. @@ -67,7 +69,7 @@ fgets(char *buf, int n, FILE *fp) /* * If the buffer is empty, refill it. */ - if ((len = fp->_r) <= 0) { + if ((len = fp->pub._r) <= 0) { if (__srefill(fp)) { /* EOF/error: stop with partial or no line */ if (s == buf) { @@ -76,9 +78,9 @@ fgets(char *buf, int n, FILE *fp) } break; } - len = fp->_r; + len = fp->pub._r; } - p = fp->_p; + p = fp->pub._p; /* * Scan through at most n bytes of the current buffer, @@ -91,15 +93,15 @@ fgets(char *buf, int n, FILE *fp) t = memchr((void *)p, '\n', len); if (t != NULL) { len = ++t - p; - fp->_r -= len; - fp->_p = t; + fp->pub._r -= len; + fp->pub._p = t; (void)memcpy((void *)s, (void *)p, len); s[len] = 0; FUNLOCKFILE(fp); return (buf); } - fp->_r -= len; - fp->_p += len; + fp->pub._r -= len; + fp->pub._p += len; (void)memcpy((void *)s, (void *)p, len); s += len; n -= len; diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c index bc17170d81..51a65a62ec 100644 --- a/lib/libc/stdio/fileno.c +++ b/lib/libc/stdio/fileno.c @@ -35,10 +35,13 @@ * * @(#)fileno.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fileno.c,v 1.6 1999/08/28 00:01:01 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fileno.c,v 1.4 2004/06/08 03:36:47 hmp Exp $ + * $DragonFly: src/lib/libc/stdio/fileno.c,v 1.5 2005/07/23 20:23:05 joerg Exp $ */ +#include "namespace.h" #include +#include "un-namespace.h" + #include "libc_private.h" /* diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c index 1d6f3e7170..ec6638db1d 100644 --- a/lib/libc/stdio/findfp.c +++ b/lib/libc/stdio/findfp.c @@ -35,7 +35,7 @@ * * @(#)findfp.c 8.2 (Berkeley) 1/4/94 * $FreeBSD: src/lib/libc/stdio/findfp.c,v 1.7.2.3 2001/08/17 02:56:31 peter Exp $ - * $DragonFly: src/lib/libc/stdio/findfp.c,v 1.7 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/findfp.c,v 1.8 2005/07/23 20:23:06 joerg Exp $ */ #include @@ -49,16 +49,16 @@ #include #include "local.h" -#include "glue.h" +#include "priv_stdio.h" int __sdidinit; #define NDYNAMIC 10 /* add ten more whenever necessary */ #define std(flags, file) \ - {0,0,0,flags,file,{0},0,__sF+file,__sclose,__sread,__sseek,__swrite, \ - {0}, __sFX + file} -/* p r w flags file _bf z cookie close read seek write */ + {{0,flags,file,0,0,0},{NULL, 0},__sF+file,__sclose,__sread,__sseek,__swrite, \ + {NULL,0}, __sFX + file, 0, {0,0,0}, {0}, {NULL,0}, 0,0 } +/* p flags file r w _bf cookie close read seek write */ /* _ub _extra */ /* the usual - (stdin + stdout + stderr) */ @@ -149,7 +149,7 @@ __sfp(void) THREAD_LOCK(); for (g = &__sglue; g != NULL; g = g->next) { for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) - if (fp->_flags == 0) + if (fp->pub._flags == 0) goto found; } THREAD_UNLOCK(); /* don't hold lock while malloc()ing. */ @@ -160,15 +160,15 @@ __sfp(void) lastglue = g; /* not atomic; only accessed when locked */ fp = g->iobs; found: - fp->_flags = 1; /* reserve this slot; caller sets real flags */ + fp->pub._flags = 1; /* reserve this slot; caller sets real flags */ THREAD_UNLOCK(); - fp->_p = NULL; /* no current pointer */ - fp->_w = 0; /* nothing to read or write */ - fp->_r = 0; + fp->pub._p = NULL; /* no current pointer */ + fp->pub._w = 0; /* nothing to read or write */ + fp->pub._r = 0; fp->_bf._base = NULL; /* no buffer */ fp->_bf._size = 0; - fp->_lbfsize = 0; /* not line buffered */ - fp->_file = -1; /* no file */ + fp->pub._lbfsize = 0; /* not line buffered */ + fp->pub._fileno = -1; /* no file */ /* fp->_cookie = ; */ /* caller sets cookie, _read/_write etc */ fp->_ub._base = NULL; /* no ungetc buffer */ fp->_ub._size = 0; @@ -182,6 +182,8 @@ found: * XXX. Force immediate allocation of internal memory. Not used by stdio, * but documented historically for certain applications. Bad applications. */ +void f_prealloc(void); + __warn_references(f_prealloc, "warning: this program uses f_prealloc(), which is not recommended."); diff --git a/lib/libc/stdio/fopen.c b/lib/libc/stdio/fopen.c index cb502b560f..ffa2b07f30 100644 --- a/lib/libc/stdio/fopen.c +++ b/lib/libc/stdio/fopen.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/fopen.c,v 1.3.2.1 2001/03/05 10:53:51 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/fopen.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fopen.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ * * @(#)fopen.c 8.1 (Berkeley) 6/4/93 */ @@ -48,6 +48,7 @@ #include "un-namespace.h" #include "local.h" +#include "priv_stdio.h" FILE * fopen(const char *file, const char *mode) @@ -61,11 +62,11 @@ fopen(const char *file, const char *mode) if ((fp = __sfp()) == NULL) return (NULL); if ((f = _open(file, oflags, DEFFILEMODE)) < 0) { - fp->_flags = 0; /* release */ + fp->pub._flags = 0; /* release */ return (NULL); } - fp->_file = f; - fp->_flags = flags; + fp->pub._fileno = f; + fp->pub._flags = flags; fp->_cookie = fp; fp->_read = __sread; fp->_write = __swrite; diff --git a/lib/libc/stdio/fpurge.c b/lib/libc/stdio/fpurge.c index 68467d9d42..54e3333f5e 100644 --- a/lib/libc/stdio/fpurge.c +++ b/lib/libc/stdio/fpurge.c @@ -35,7 +35,7 @@ * * @(#)fpurge.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fpurge.c,v 1.7 1999/08/28 00:01:02 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fpurge.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fpurge.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -43,8 +43,10 @@ #include #include #include "un-namespace.h" + #include "local.h" #include "libc_private.h" +#include "priv_stdio.h" /* * fpurge: like fflush, but without writing anything: leave the @@ -55,15 +57,15 @@ fpurge(FILE *fp) { int retval; FLOCKFILE(fp); - if (!fp->_flags) { + if (!fp->pub._flags) { errno = EBADF; retval = EOF; } else { if (HASUB(fp)) FREEUB(fp); - fp->_p = fp->_bf._base; - fp->_r = 0; - fp->_w = fp->_flags & (__SLBF|__SNBF) ? 0 : fp->_bf._size; + fp->pub._p = fp->_bf._base; + fp->pub._r = 0; + fp->pub._w = fp->pub._flags & (__SLBF|__SNBF) ? 0 : fp->_bf._size; retval = 0; } FUNLOCKFILE(fp); diff --git a/lib/libc/stdio/fputs.c b/lib/libc/stdio/fputs.c index 70b495db37..b8f410060a 100644 --- a/lib/libc/stdio/fputs.c +++ b/lib/libc/stdio/fputs.c @@ -35,14 +35,16 @@ * * @(#)fputs.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fputs.c,v 1.7 1999/08/28 00:01:03 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fputs.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fputs.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" +#include #include #include #include "un-namespace.h" -#include "fvwrite.h" +#include "priv_stdio.h" + #include "libc_private.h" /* @@ -55,7 +57,7 @@ fputs(const char *s, FILE *fp) struct __suio uio; struct __siov iov; - iov.iov_base = (void *)s; + iov.iov_base = __DECONST(char *, s); iov.iov_len = uio.uio_resid = strlen(s); uio.uio_iov = &iov; uio.uio_iovcnt = 1; diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 732b171c88..998355dcfd 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -35,15 +35,17 @@ * * @(#)fread.c 8.2 (Berkeley) 12/11/93 * $FreeBSD: src/lib/libc/stdio/fread.c,v 1.7 1999/08/28 00:01:04 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fread.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fread.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" #include #include #include "un-namespace.h" + #include "local.h" #include "libc_private.h" +#include "priv_stdio.h" size_t fread(void *buf, size_t size, size_t count, FILE *fp) @@ -61,13 +63,13 @@ fread(void *buf, size_t size, size_t count, FILE *fp) if ((resid = count * size) == 0) return (0); FLOCKFILE(fp); - if (fp->_r < 0) - fp->_r = 0; + if (fp->pub._r < 0) + fp->pub._r = 0; total = resid; p = buf; - while (resid > (r = fp->_r)) { - (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); - fp->_p += r; + while (resid > (r = fp->pub._r)) { + (void)memcpy((void *)p, (void *)fp->pub._p, (size_t)r); + fp->pub._p += r; /* fp->_r = 0 ... done in __srefill */ p += r; resid -= r; @@ -77,9 +79,9 @@ fread(void *buf, size_t size, size_t count, FILE *fp) return ((total - resid) / size); } } - (void)memcpy((void *)p, (void *)fp->_p, resid); - fp->_r -= resid; - fp->_p += resid; + (void)memcpy((void *)p, (void *)fp->pub._p, resid); + fp->pub._r -= resid; + fp->pub._p += resid; FUNLOCKFILE(fp); return (count); } diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c index a2f4a36384..dca0c98907 100644 --- a/lib/libc/stdio/freopen.c +++ b/lib/libc/stdio/freopen.c @@ -35,7 +35,7 @@ * * @(#)freopen.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/freopen.c,v 1.5.2.1 2001/03/05 10:54:53 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/freopen.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/freopen.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -47,8 +47,10 @@ #include #include #include "un-namespace.h" + #include "libc_private.h" #include "local.h" +#include "priv_stdio.h" /* * Re-direct an existing, open (probably) file to some other file. @@ -80,12 +82,12 @@ freopen(const char *file, const char *mode, FILE *fp) */ if (file == NULL) { /* See comment below regarding freopen() of closed files. */ - if (fp->_flags == 0) { + if (fp->pub._flags == 0) { FUNLOCKFILE(fp); errno = EINVAL; return (NULL); } - if ((dflags = fcntl(fp->_file, F_GETFL)) < 0) { + if ((dflags = _fcntl(fp->pub._fileno, F_GETFL)) < 0) { sverrno = errno; fclose(fp); FUNLOCKFILE(fp); @@ -102,7 +104,7 @@ freopen(const char *file, const char *mode, FILE *fp) if ((oflags ^ dflags) & O_APPEND) { dflags &= ~O_APPEND; dflags |= oflags & O_APPEND; - if (fcntl(fp->_file, F_SETFL, dflags) < 0) { + if (_fcntl(fp->pub._fileno, F_SETFL, dflags) < 0) { sverrno = errno; fclose(fp); FUNLOCKFILE(fp); @@ -111,7 +113,7 @@ freopen(const char *file, const char *mode, FILE *fp) } } if (oflags & O_TRUNC) - ftruncate(fp->_file, 0); + ftruncate(fp->pub._fileno, 0); if (_fseeko(fp, 0, oflags & O_APPEND ? SEEK_END : SEEK_SET) < 0 && errno != ESPIPE) { sverrno = errno; @@ -120,7 +122,7 @@ freopen(const char *file, const char *mode, FILE *fp) errno = sverrno; return (NULL); } - f = fp->_file; + f = fp->pub._fileno; isopen = 0; wantfd = -1; goto finish; @@ -134,17 +136,17 @@ freopen(const char *file, const char *mode, FILE *fp) * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) * should work. This is unnecessary if it was not a Unix file. */ - if (fp->_flags == 0) { - fp->_flags = __SEOF; /* hold on to it */ + if (fp->pub._flags == 0) { + fp->pub._flags = __SEOF; /* hold on to it */ isopen = 0; wantfd = -1; } else { /* flush the stream; ANSI doesn't require this. */ - if (fp->_flags & __SWR) + if (fp->pub._flags & __SWR) (void) __sflush(fp); /* if close is NULL, closing is a no-op, hence pointless */ isopen = fp->_close != NULL; - if ((wantfd = fp->_file) < 0 && isopen) { + if ((wantfd = fp->pub._fileno) < 0 && isopen) { (void) (*fp->_close)(fp->_cookie); isopen = 0; } @@ -170,14 +172,14 @@ finish: */ if (isopen) (void) (*fp->_close)(fp->_cookie); - if (fp->_flags & __SMBF) + if (fp->pub._flags & __SMBF) free((char *)fp->_bf._base); - fp->_w = 0; - fp->_r = 0; - fp->_p = NULL; + fp->pub._w = 0; + fp->pub._r = 0; + fp->pub._p = NULL; fp->_bf._base = NULL; fp->_bf._size = 0; - fp->_lbfsize = 0; + fp->pub._lbfsize = 0; if (HASUB(fp)) FREEUB(fp); fp->_ub._size = 0; @@ -186,7 +188,7 @@ finish: fp->_lb._size = 0; if (f < 0) { /* did not get it after all */ - fp->_flags = 0; /* set it free */ + fp->pub._flags = 0; /* set it free */ errno = sverrno; /* restore in case _close clobbered */ FUNLOCKFILE(fp); return (NULL); @@ -204,8 +206,8 @@ finish: } } - fp->_flags = flags; - fp->_file = f; + fp->pub._flags = flags; + fp->pub._fileno = f; fp->_cookie = fp; fp->_read = __sread; fp->_write = __swrite; diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c index e37a136302..60db812d77 100644 --- a/lib/libc/stdio/fseek.c +++ b/lib/libc/stdio/fseek.c @@ -35,7 +35,7 @@ * * @(#)fseek.c 8.3 (Berkeley) 1/2/94 * $FreeBSD: src/lib/libc/stdio/fseek.c,v 1.9.2.1 2001/03/05 10:56:58 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/fseek.c,v 1.7 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/fseek.c,v 1.8 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -46,8 +46,10 @@ #include #include #include "un-namespace.h" + #include "local.h" #include "libc_private.h" +#include "priv_stdio.h" #define POS_ERR (-(fpos_t)1) @@ -108,7 +110,7 @@ _fseeko(FILE *fp, off_t offset, int whence) * we have to first find the current stream offset a la * ftell (see ftell for details). */ - if (fp->_flags & __SOFF) + if (fp->pub._flags & __SOFF) curoff = fp->_offset; else { curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); @@ -116,12 +118,12 @@ _fseeko(FILE *fp, off_t offset, int whence) return (EOF); } } - if (fp->_flags & __SRD) { - curoff -= fp->_r; + if (fp->pub._flags & __SRD) { + curoff -= fp->pub._r; if (HASUB(fp)) curoff -= fp->_ur; - } else if (fp->_flags & __SWR && fp->_p != NULL) - curoff += fp->_p - fp->_bf._base; + } else if (fp->pub._flags & __SWR && fp->pub._p != NULL) + curoff += fp->pub._p - fp->_bf._base; offset += curoff; whence = SEEK_SET; @@ -149,17 +151,17 @@ _fseeko(FILE *fp, off_t offset, int whence) */ if (fp->_bf._base == NULL) __smakebuf(fp); - if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) + if (fp->pub._flags & (__SWR | __SRW | __SNBF | __SNPT)) goto dumb; - if ((fp->_flags & __SOPT) == 0) { + if ((fp->pub._flags & __SOPT) == 0) { if (seekfn != __sseek || - fp->_file < 0 || _fstat(fp->_file, &st) || + fp->pub._fileno < 0 || _fstat(fp->pub._fileno, &st) || (st.st_mode & S_IFMT) != S_IFREG) { - fp->_flags |= __SNPT; + fp->pub._flags |= __SNPT; goto dumb; } fp->_blksize = st.st_blksize; - fp->_flags |= __SOPT; + fp->pub._flags |= __SOPT; } /* @@ -169,20 +171,20 @@ _fseeko(FILE *fp, off_t offset, int whence) if (whence == SEEK_SET) target = offset; else { - if (_fstat(fp->_file, &st)) + if (_fstat(fp->pub._fileno, &st)) goto dumb; target = st.st_size + offset; } if (!havepos) { - if (fp->_flags & __SOFF) + if (fp->pub._flags & __SOFF) curoff = fp->_offset; else { curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); if (curoff == POS_ERR) goto dumb; } - curoff -= fp->_r; + curoff -= fp->pub._r; if (HASUB(fp)) curoff -= fp->_ur; } @@ -194,14 +196,14 @@ _fseeko(FILE *fp, off_t offset, int whence) * file offset for the first byte in the current input buffer. */ if (HASUB(fp)) { - curoff += fp->_r; /* kill off ungetc */ + curoff += fp->pub._r; /* kill off ungetc */ n = fp->_extra->_up - fp->_bf._base; curoff -= n; n += fp->_ur; } else { - n = fp->_p - fp->_bf._base; + n = fp->pub._p - fp->_bf._base; curoff -= n; - n += fp->_r; + n += fp->pub._r; } /* @@ -210,15 +212,15 @@ _fseeko(FILE *fp, off_t offset, int whence) * and return. (If the buffer was modified, we have to * skip this; see fgetln.c.) */ - if ((fp->_flags & __SMOD) == 0 && + if ((fp->pub._flags & __SMOD) == 0 && target >= curoff && target < curoff + n) { int o = target - curoff; - fp->_p = fp->_bf._base + o; - fp->_r = n - o; + fp->pub._p = fp->_bf._base + o; + fp->pub._r = n - o; if (HASUB(fp)) FREEUB(fp); - fp->_flags &= ~__SEOF; + fp->pub._flags &= ~__SEOF; return (0); } @@ -233,17 +235,17 @@ _fseeko(FILE *fp, off_t offset, int whence) curoff = target & ~(fp->_blksize - 1); if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) goto dumb; - fp->_r = 0; - fp->_p = fp->_bf._base; + fp->pub._r = 0; + fp->pub._p = fp->_bf._base; if (HASUB(fp)) FREEUB(fp); - fp->_flags &= ~__SEOF; + fp->pub._flags &= ~__SEOF; n = target - curoff; if (n) { - if (__srefill(fp) || fp->_r < n) + if (__srefill(fp) || fp->pub._r < n) goto dumb; - fp->_p += n; - fp->_r -= n; + fp->pub._p += n; + fp->pub._r -= n; } return (0); @@ -259,9 +261,9 @@ dumb: /* success: clear EOF indicator and discard ungetc() data */ if (HASUB(fp)) FREEUB(fp); - fp->_p = fp->_bf._base; - fp->_r = 0; - /* fp->_w = 0; */ /* unnecessary (I think...) */ - fp->_flags &= ~__SEOF; + fp->pub._p = fp->_bf._base; + fp->pub._r = 0; + /* fp->pub._w = 0; */ /* unnecessary (I think...) */ + fp->pub._flags &= ~__SEOF; return (0); } diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c index 5f1e755149..9837c59dbe 100644 --- a/lib/libc/stdio/ftell.c +++ b/lib/libc/stdio/ftell.c @@ -35,7 +35,7 @@ * * @(#)ftell.c 8.2 (Berkeley) 5/4/95 * $FreeBSD: src/lib/libc/stdio/ftell.c,v 1.11 1999/08/28 00:01:06 peter Exp $ - * $DragonFly: src/lib/libc/stdio/ftell.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/ftell.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -43,8 +43,10 @@ #include #include #include "un-namespace.h" + #include "local.h" #include "libc_private.h" +#include "priv_stdio.h" /* * standard ftell function. @@ -79,7 +81,7 @@ ftello(FILE *fp) * Find offset of underlying I/O object, then * adjust for buffered bytes. */ - if (fp->_flags & __SOFF) + if (fp->pub._flags & __SOFF) pos = fp->_offset; else { pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR); @@ -88,22 +90,22 @@ ftello(FILE *fp) return (pos); } } - if (fp->_flags & __SRD) { + if (fp->pub._flags & __SRD) { /* * Reading. Any unread characters (including * those from ungetc) cause the position to be * smaller than that in the underlying object. */ - pos -= fp->_r; + pos -= fp->pub._r; if (HASUB(fp)) pos -= fp->_ur; - } else if (fp->_flags & __SWR && fp->_p != NULL) { + } else if (fp->pub._flags & __SWR && fp->pub._p != NULL) { /* * Writing. Any buffered characters cause the * position to be greater than that in the * underlying object. */ - pos += fp->_p - fp->_bf._base; + pos += fp->pub._p - fp->_bf._base; } FUNLOCKFILE(fp); return (pos); diff --git a/lib/libc/stdio/funopen.c b/lib/libc/stdio/funopen.c index df688bdd35..2d55b8633a 100644 --- a/lib/libc/stdio/funopen.c +++ b/lib/libc/stdio/funopen.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/funopen.c,v 1.1.1.1.14.1 2001/03/05 10:57:52 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/funopen.c,v 1.4 2004/06/07 20:35:41 hmp Exp $ + * $DragonFly: src/lib/libc/stdio/funopen.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ * * @(#)funopen.c 8.1 (Berkeley) 6/4/93 */ @@ -43,13 +43,13 @@ #include #include "local.h" +#include "priv_stdio.h" FILE * -funopen(cookie, readfn, writefn, seekfn, closefn) - const void *cookie; - int (*readfn)(), (*writefn)(); - fpos_t (*seekfn)(void *cookie, fpos_t off, int whence); - int (*closefn)(); +funopen(const void *cookie, int (*readfn)(void *, char *, int), + int (*writefn)(void *, const char *, int), + fpos_t (*seekfn)(void *cookie, fpos_t off, int whence), + int (*closefn)(void *)) { FILE *fp; int flags; @@ -68,8 +68,8 @@ funopen(cookie, readfn, writefn, seekfn, closefn) } if ((fp = __sfp()) == NULL) return (NULL); - fp->_flags = flags; - fp->_file = -1; + fp->pub._flags = flags; + fp->pub._fileno = -1; fp->_cookie = (void *)cookie; fp->_read = readfn; fp->_write = writefn; diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c index af16b2b20d..f2cb447b87 100644 --- a/lib/libc/stdio/fvwrite.c +++ b/lib/libc/stdio/fvwrite.c @@ -35,7 +35,7 @@ * * @(#)fvwrite.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fvwrite.c,v 1.10 1999/08/28 00:01:06 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fvwrite.c,v 1.6 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fvwrite.c,v 1.7 2005/07/23 20:23:06 joerg Exp $ */ #include @@ -43,7 +43,7 @@ #include #include #include "local.h" -#include "fvwrite.h" +#include "priv_stdio.h" /* * Write some memory regions. Return zero on success, EOF on error. @@ -70,7 +70,7 @@ __sfvwrite(FILE *fp, struct __suio *uio) } #define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n)) +#define COPY(n) (void)memcpy((void *)fp->pub._p, (void *)p, (size_t)(n)) iov = uio->uio_iov; p = iov->iov_base; @@ -83,7 +83,7 @@ __sfvwrite(FILE *fp, struct __suio *uio) len = iov->iov_len; \ iov++; \ } - if (fp->_flags & __SNBF) { + if (fp->pub._flags & __SNBF) { /* * Unbuffered: write up to BUFSIZ bytes at a time. */ @@ -95,7 +95,7 @@ __sfvwrite(FILE *fp, struct __suio *uio) p += w; len -= w; } while ((uio->uio_resid -= w) != 0); - } else if ((fp->_flags & __SLBF) == 0) { + } else if ((fp->pub._flags & __SLBF) == 0) { /* * Fully buffered: fill partially full buffer, if any, * and then flush. If there is no partial buffer, write @@ -109,37 +109,37 @@ __sfvwrite(FILE *fp, struct __suio *uio) */ do { GETIOV(;); - if ((fp->_flags & (__SALC | __SSTR)) == - (__SALC | __SSTR) && fp->_w < len) { - size_t blen = fp->_p - fp->_bf._base; + if ((fp->pub._flags & (__SALC | __SSTR)) == + (__SALC | __SSTR) && fp->pub._w < len) { + size_t blen = fp->pub._p - fp->_bf._base; /* * Alloc an extra 128 bytes (+ 1 for NULL) * so we don't call realloc(3) so often. */ - fp->_w = len + 128; + fp->pub._w = len + 128; fp->_bf._size = blen + len + 128; fp->_bf._base = reallocf(fp->_bf._base, fp->_bf._size + 1); if (fp->_bf._base == NULL) goto err; - fp->_p = fp->_bf._base + blen; + fp->pub._p = fp->_bf._base + blen; } - w = fp->_w; - if (fp->_flags & __SSTR) { + w = fp->pub._w; + if (fp->pub._flags & __SSTR) { if (len < w) w = len; if (w > 0) { COPY(w); /* copy MIN(fp->_w,len), */ - fp->_w -= w; - fp->_p += w; + fp->pub._w -= w; + fp->pub._p += w; } w = len; /* but pretend copied all */ - } else if (fp->_p > fp->_bf._base && len > w) { + } else if (fp->pub._p > fp->_bf._base && len > w) { /* fill and flush */ COPY(w); /* fp->_w -= w; */ /* unneeded */ - fp->_p += w; + fp->pub._p += w; if (__fflush(fp)) goto err; } else if (len >= (w = fp->_bf._size)) { @@ -151,8 +151,8 @@ __sfvwrite(FILE *fp, struct __suio *uio) /* fill and done */ w = len; COPY(w); - fp->_w -= w; - fp->_p += w; + fp->pub._w -= w; + fp->pub._p += w; } p += w; len -= w; @@ -175,11 +175,11 @@ __sfvwrite(FILE *fp, struct __suio *uio) nlknown = 1; } s = MIN(len, nldist); - w = fp->_w + fp->_bf._size; - if (fp->_p > fp->_bf._base && s > w) { + w = fp->pub._w + fp->_bf._size; + if (fp->pub._p > fp->_bf._base && s > w) { COPY(w); /* fp->_w -= w; */ - fp->_p += w; + fp->pub._p += w; if (__fflush(fp)) goto err; } else if (s >= (w = fp->_bf._size)) { @@ -189,8 +189,8 @@ __sfvwrite(FILE *fp, struct __suio *uio) } else { w = s; COPY(w); - fp->_w -= w; - fp->_p += w; + fp->pub._w -= w; + fp->pub._p += w; } if ((nldist -= w) == 0) { /* copied the newline: flush and forget */ @@ -205,6 +205,6 @@ __sfvwrite(FILE *fp, struct __suio *uio) return (0); err: - fp->_flags |= __SERR; + fp->pub._flags |= __SERR; return (EOF); } diff --git a/lib/libc/stdio/fvwrite.h b/lib/libc/stdio/fvwrite.h deleted file mode 100644 index ec6bc0beef..0000000000 --- a/lib/libc/stdio/fvwrite.h +++ /dev/null @@ -1,56 +0,0 @@ -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)fvwrite.h 8.1 (Berkeley) 6/4/93 - */ - -/* - * I/O descriptors for __sfvwrite(). - */ -struct __siov { - void *iov_base; - size_t iov_len; -}; -struct __suio { - struct __siov *uio_iov; - int uio_iovcnt; - int uio_resid; -}; - -#if __STDC__ || c_plusplus -extern int __sfvwrite(FILE *, struct __suio *); -#else -extern int __sfvwrite(); -#endif diff --git a/lib/libc/stdio/fwalk.c b/lib/libc/stdio/fwalk.c index 80d9bb99d3..b54d52143b 100644 --- a/lib/libc/stdio/fwalk.c +++ b/lib/libc/stdio/fwalk.c @@ -35,14 +35,14 @@ * * @(#)fwalk.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fwalk.c,v 1.6.2.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/fwalk.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fwalk.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include #include "local.h" -#include "glue.h" +#include "priv_stdio.h" int _fwalk(int (*function)(FILE *)) @@ -59,7 +59,7 @@ _fwalk(int (*function)(FILE *)) */ for (g = &__sglue; g != NULL; g = g->next) for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) - if (fp->_flags != 0) + if (fp->pub._flags != 0) ret |= (*function)(fp); return (ret); } diff --git a/lib/libc/stdio/fwrite.c b/lib/libc/stdio/fwrite.c index 09930ab6d6..61e720e66c 100644 --- a/lib/libc/stdio/fwrite.c +++ b/lib/libc/stdio/fwrite.c @@ -35,14 +35,15 @@ * * @(#)fwrite.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/fwrite.c,v 1.7 1999/08/28 00:01:07 peter Exp $ - * $DragonFly: src/lib/libc/stdio/fwrite.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/fwrite.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" +#include #include #include "un-namespace.h" #include "local.h" -#include "fvwrite.h" +#include "priv_stdio.h" #include "libc_private.h" /* @@ -56,7 +57,7 @@ fwrite(const void *buf, size_t size, size_t count, FILE *fp) struct __suio uio; struct __siov iov; - iov.iov_base = (void *)buf; + iov.iov_base = __DECONST(void *, buf); uio.uio_resid = iov.iov_len = n = count * size; uio.uio_iov = &iov; uio.uio_iovcnt = 1; diff --git a/lib/libc/stdio/glue.h b/lib/libc/stdio/glue.h deleted file mode 100644 index bd6e94e9fb..0000000000 --- a/lib/libc/stdio/glue.h +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)glue.h 8.1 (Berkeley) 6/4/93 - * $DragonFly: src/lib/libc/stdio/Attic/glue.h,v 1.2 2005/01/31 22:29:40 dillon Exp $ - */ - -/* - * The first few FILEs are statically allocated; others are dynamically - * allocated and linked in via this glue structure. - */ -struct glue { - struct glue *next; - int niobs; - FILE *iobs; -}; - -extern struct glue __sglue; diff --git a/lib/libc/stdio/local.h b/lib/libc/stdio/local.h index 8d0f68651e..afc5f218bc 100644 --- a/lib/libc/stdio/local.h +++ b/lib/libc/stdio/local.h @@ -36,7 +36,7 @@ * @(#)local.h 8.3 (Berkeley) 7/3/94 * * $FreeBSD: src/lib/libc/stdio/local.h,v 1.1.1.2.6.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/local.h,v 1.6 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/local.h,v 1.7 2005/07/23 20:23:06 joerg Exp $ */ #include /* for off_t */ @@ -82,7 +82,7 @@ struct __sFILEX { * Return true iff the given FILE cannot be written now. */ #define cantwrite(fp) \ - ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \ + ((((fp)->pub._flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \ __swsetup(fp)) /* diff --git a/lib/libc/stdio/makebuf.c b/lib/libc/stdio/makebuf.c index 9d41957894..97208edc51 100644 --- a/lib/libc/stdio/makebuf.c +++ b/lib/libc/stdio/makebuf.c @@ -34,7 +34,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/makebuf.c,v 1.1.1.1.14.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/makebuf.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/makebuf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ * * @(#)makebuf.c 8.1 (Berkeley) 6/4/93 */ @@ -45,9 +45,11 @@ #include #include #include -#include "local.h" #include "un-namespace.h" +#include "local.h" +#include "priv_stdio.h" + /* * Allocate a file buffer, or switch to unbuffered I/O. * Per the ANSI C standard, ALL tty devices default to line buffered. @@ -63,25 +65,25 @@ __smakebuf(FILE *fp) size_t size; int couldbetty; - if (fp->_flags & __SNBF) { - fp->_bf._base = fp->_p = fp->_nbuf; + if (fp->pub._flags & __SNBF) { + fp->_bf._base = fp->pub._p = fp->_nbuf; fp->_bf._size = 1; return; } flags = __swhatbuf(fp, &size, &couldbetty); if ((p = malloc(size)) == NULL) { - fp->_flags |= __SNBF; - fp->_bf._base = fp->_p = fp->_nbuf; + fp->pub._flags |= __SNBF; + fp->_bf._base = fp->pub._p = fp->_nbuf; fp->_bf._size = 1; return; } __cleanup = _cleanup; flags |= __SMBF; - fp->_bf._base = fp->_p = p; + fp->_bf._base = fp->pub._p = p; fp->_bf._size = size; - if (couldbetty && isatty(fp->_file)) + if (couldbetty && isatty(fp->pub._fileno)) flags |= __SLBF; - fp->_flags |= flags; + fp->pub._flags |= flags; } /* @@ -92,7 +94,7 @@ __swhatbuf(FILE *fp, size_t *bufsize, int *couldbetty) { struct stat st; - if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) { + if (fp->pub._fileno < 0 || _fstat(fp->pub._fileno, &st) < 0) { *couldbetty = 0; *bufsize = BUFSIZ; return (__SNPT); diff --git a/lib/libc/stdio/perror.c b/lib/libc/stdio/perror.c index e049e9a550..c018924f2e 100644 --- a/lib/libc/stdio/perror.c +++ b/lib/libc/stdio/perror.c @@ -31,7 +31,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/perror.c,v 1.3.6.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/perror.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/perror.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ * * @(#)perror.c 8.1 (Berkeley) 6/4/93 */ @@ -53,17 +53,17 @@ perror(const char *s) v = iov; if (s != NULL && *s != '\0') { - v->iov_base = (char *)s; + v->iov_base = __DECONST(char *, s); v->iov_len = strlen(s); v++; - v->iov_base = ": "; + v->iov_base = __DECONST(char *, ": "); v->iov_len = 2; v++; } v->iov_base = strerror(errno); v->iov_len = strlen(v->iov_base); v++; - v->iov_base = "\n"; + v->iov_base = __DECONST(char *, "\n"); v->iov_len = 1; (void)_writev(STDERR_FILENO, iov, (v - iov) + 1); } diff --git a/lib/libc/stdio/priv_stdio.h b/lib/libc/stdio/priv_stdio.h new file mode 100644 index 0000000000..3a100ab777 --- /dev/null +++ b/lib/libc/stdio/priv_stdio.h @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * Copyright (c) 2005 Joerg Sonnenberger . All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/lib/libc/stdio/priv_stdio.h,v 1.1 2005/07/23 20:23:06 joerg Exp $ + */ + +#ifndef _LIBC_PRIV_STDIO_H_ +#define _LIBC_PRIV_STDIO_H_ + +/* stdio buffers */ +struct __sbuf { + unsigned char *_base; + int _size; +}; + +struct __sFILEX; + +/* + * _ub, _up, and _ur are used when ungetc() pushes back more characters + * than fit in the current _bf, or when ungetc() pushes back a character + * that does not match the previous one in _bf. When this happens, + * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff + * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. + */ + +struct __FILE { + struct __FILE_public pub; + struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ + + /* operations */ + void *_cookie; /* cookie passed to io functions */ + int (*_close) (void *); + int (*_read) (void *, char *, int); + fpos_t (*_seek) (void *, fpos_t, int); + int (*_write) (void *, const char *, int); + + /* separate buffer for long sequences of ungetc() */ + struct __sbuf _ub; /* ungetc buffer */ + struct __sFILEX *_extra; + int _ur; /* saved _r when _r is counting ungetc data */ + + /* tricks to meet minimum requirements even when malloc() fails */ + unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ + unsigned char _nbuf[1]; /* guarantee a getc() buffer */ + + /* separate buffer for fgetln() when line crosses buffer boundary */ + struct __sbuf _lb; /* buffer for fgetln() */ + + /* Unix stdio files get aligned to block boundaries on fseek() */ + int _blksize; /* stat.st_blksize (may be != _bf._size) */ + fpos_t _offset; /* current lseek offset (see WARNING) */ +}; + +/* + * I/O descriptors for __sfvwrite(). + */ +struct __siov { + void *iov_base; + size_t iov_len; +}; +struct __suio { + struct __siov *uio_iov; + int uio_iovcnt; + int uio_resid; +}; + +/* + * The first few FILEs are statically allocated; others are dynamically + * allocated and linked in via this glue structure. + */ +struct glue { + struct glue *next; + int niobs; + FILE *iobs; +}; + +extern struct glue __sglue; + +__BEGIN_DECLS +int __sfvwrite(FILE *, struct __suio *); +int __fflush(FILE *); +int __ungetc(int, FILE *); +__END_DECLS + +#endif /* _LIBC_PRIV_STDIO_H_ */ diff --git a/lib/libc/stdio/puts.c b/lib/libc/stdio/puts.c index 62eef31031..dc93ae8503 100644 --- a/lib/libc/stdio/puts.c +++ b/lib/libc/stdio/puts.c @@ -35,14 +35,15 @@ * * @(#)puts.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/puts.c,v 1.7 1999/08/28 00:01:12 peter Exp $ - * $DragonFly: src/lib/libc/stdio/puts.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/puts.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" +#include #include #include #include "un-namespace.h" -#include "fvwrite.h" +#include "priv_stdio.h" #include "libc_private.h" /* @@ -56,9 +57,9 @@ puts(char const *s) struct __suio uio; struct __siov iov[2]; - iov[0].iov_base = (void *)s; + iov[0].iov_base = __DECONST(char *, s); iov[0].iov_len = c; - iov[1].iov_base = "\n"; + iov[1].iov_base = __DECONST(char *, "\n"); iov[1].iov_len = 1; uio.uio_resid = c + 1; uio.uio_iov = &iov[0]; diff --git a/lib/libc/stdio/putw.c b/lib/libc/stdio/putw.c index 5bfb542219..9d4c29df2b 100644 --- a/lib/libc/stdio/putw.c +++ b/lib/libc/stdio/putw.c @@ -35,13 +35,13 @@ * * @(#)putw.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/putw.c,v 1.7 1999/08/28 00:01:13 peter Exp $ - * $DragonFly: src/lib/libc/stdio/putw.c,v 1.4 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/putw.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" #include #include "un-namespace.h" -#include "fvwrite.h" +#include "priv_stdio.h" #include "libc_private.h" int diff --git a/lib/libc/stdio/refill.c b/lib/libc/stdio/refill.c index 2d1edf5bb8..008a75064f 100644 --- a/lib/libc/stdio/refill.c +++ b/lib/libc/stdio/refill.c @@ -35,7 +35,7 @@ * * @(#)refill.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/refill.c,v 1.8.2.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/refill.c,v 1.7 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/refill.c,v 1.8 2005/07/23 20:23:06 joerg Exp $ */ #include @@ -43,6 +43,7 @@ #include #include "local.h" +#include "priv_stdio.h" static int lflush (FILE *); @@ -50,7 +51,7 @@ static int lflush(FILE *fp) { - if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR)) + if ((fp->pub._flags & (__SLBF|__SWR)) == (__SLBF|__SWR)) return (__sflush(fp)); return (0); } @@ -67,28 +68,28 @@ __srefill(FILE *fp) if (!__sdidinit) __sinit(); - fp->_r = 0; /* largely a convenience for callers */ + fp->pub._r = 0; /* largely a convenience for callers */ /* SysV does not make this test; take it out for compatibility */ - if (fp->_flags & __SEOF) + if (fp->pub._flags & __SEOF) return (EOF); /* if not already reading, have to be reading and writing */ - if ((fp->_flags & __SRD) == 0) { - if ((fp->_flags & __SRW) == 0) { + if ((fp->pub._flags & __SRD) == 0) { + if ((fp->pub._flags & __SRW) == 0) { errno = EBADF; - fp->_flags |= __SERR; + fp->pub._flags |= __SERR; return (EOF); } /* switch to reading */ - if (fp->_flags & __SWR) { + if (fp->pub._flags & __SWR) { if (__sflush(fp)) return (EOF); - fp->_flags &= ~__SWR; - fp->_w = 0; - fp->_lbfsize = 0; + fp->pub._flags &= ~__SWR; + fp->pub._w = 0; + fp->pub._lbfsize = 0; } - fp->_flags |= __SRD; + fp->pub._flags |= __SRD; } else { /* * We were reading. If there is an ungetc buffer, @@ -98,8 +99,8 @@ __srefill(FILE *fp) */ if (HASUB(fp)) { FREEUB(fp); - if ((fp->_r = fp->_ur) != 0) { - fp->_p = fp->_extra->_up; + if ((fp->pub._r = fp->_ur) != 0) { + fp->pub._p = fp->_extra->_up; return (0); } } @@ -113,17 +114,17 @@ __srefill(FILE *fp) * flush all line buffered output files, per the ANSI C * standard. */ - if (fp->_flags & (__SLBF|__SNBF)) + if (fp->pub._flags & (__SLBF|__SNBF)) (void) _fwalk(lflush); - fp->_p = fp->_bf._base; - fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size); - fp->_flags &= ~__SMOD; /* buffer contents are again pristine */ - if (fp->_r <= 0) { - if (fp->_r == 0) - fp->_flags |= __SEOF; + fp->pub._p = fp->_bf._base; + fp->pub._r = (*fp->_read)(fp->_cookie, (char *)fp->pub._p, fp->_bf._size); + fp->pub._flags &= ~__SMOD; /* buffer contents are again pristine */ + if (fp->pub._r <= 0) { + if (fp->pub._r == 0) + fp->pub._flags |= __SEOF; else { - fp->_r = 0; - fp->_flags |= __SERR; + fp->pub._r = 0; + fp->pub._flags |= __SERR; } return (EOF); } diff --git a/lib/libc/stdio/rget.c b/lib/libc/stdio/rget.c index 3c8bd427a6..70d4035838 100644 --- a/lib/libc/stdio/rget.c +++ b/lib/libc/stdio/rget.c @@ -35,11 +35,13 @@ * * @(#)rget.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/rget.c,v 1.2.8.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/rget.c,v 1.2 2003/06/17 04:26:46 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/rget.c,v 1.3 2005/07/23 20:23:06 joerg Exp $ */ #include + #include "local.h" +#include "priv_stdio.h" /* * Handle getc() when the buffer ran out: @@ -50,8 +52,8 @@ int __srget(FILE *fp) { if (__srefill(fp) == 0) { - fp->_r--; - return (*fp->_p++); + fp->pub._r--; + return (*fp->pub._p++); } return (EOF); } diff --git a/lib/libc/stdio/setvbuf.c b/lib/libc/stdio/setvbuf.c index 135a7df2cc..3c5680f310 100644 --- a/lib/libc/stdio/setvbuf.c +++ b/lib/libc/stdio/setvbuf.c @@ -35,15 +35,17 @@ * * @(#)setvbuf.c 8.2 (Berkeley) 11/16/93 * $FreeBSD: src/lib/libc/stdio/setvbuf.c,v 1.7 1999/08/28 00:01:16 peter Exp $ - * $DragonFly: src/lib/libc/stdio/setvbuf.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/setvbuf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" #include #include #include "un-namespace.h" + #include "local.h" #include "libc_private.h" +#include "priv_stdio.h" /* * Set one of the three kinds of buffering, optionally including @@ -76,8 +78,8 @@ setvbuf(FILE *fp, char *buf, int mode, size_t size) (void)__sflush(fp); if (HASUB(fp)) FREEUB(fp); - fp->_r = fp->_lbfsize = 0; - flags = fp->_flags; + fp->pub._r = fp->pub._lbfsize = 0; + flags = fp->pub._flags; if (flags & __SMBF) free((void *)fp->_bf._base); flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF); @@ -113,9 +115,9 @@ setvbuf(FILE *fp, char *buf, int mode, size_t size) if (buf == NULL) { /* No luck; switch to unbuffered I/O. */ nbf: - fp->_flags = flags | __SNBF; - fp->_w = 0; - fp->_bf._base = fp->_p = fp->_nbuf; + fp->pub._flags = flags | __SNBF; + fp->pub._w = 0; + fp->_bf._base = fp->pub._p = fp->_nbuf; fp->_bf._size = 1; FUNLOCKFILE(fp); return (ret); @@ -138,8 +140,8 @@ nbf: */ if (mode == _IOLBF) flags |= __SLBF; - fp->_flags = flags; - fp->_bf._base = fp->_p = (unsigned char *)buf; + fp->pub._flags = flags; + fp->_bf._base = fp->pub._p = (unsigned char *)buf; fp->_bf._size = size; /* fp->_lbfsize is still 0 */ if (flags & __SWR) { @@ -148,13 +150,13 @@ nbf: * that __SNBF is impossible (it was handled earlier). */ if (flags & __SLBF) { - fp->_w = 0; - fp->_lbfsize = -fp->_bf._size; + fp->pub._w = 0; + fp->pub._lbfsize = -fp->_bf._size; } else - fp->_w = size; + fp->pub._w = size; } else { /* begin/continue reading, or stay in intermediate state */ - fp->_w = 0; + fp->pub._w = 0; } __cleanup = _cleanup; diff --git a/lib/libc/stdio/snprintf.c b/lib/libc/stdio/snprintf.c index ef81d6bf8a..917baa2f7f 100644 --- a/lib/libc/stdio/snprintf.c +++ b/lib/libc/stdio/snprintf.c @@ -35,7 +35,7 @@ * * @(#)snprintf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/snprintf.c,v 1.12 1999/08/28 00:01:16 peter Exp $ - * $DragonFly: src/lib/libc/stdio/snprintf.c,v 1.4 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/snprintf.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include @@ -43,6 +43,7 @@ #include #include "local.h" +#include "priv_stdio.h" int snprintf(char *str, size_t n, char const *fmt, ...) @@ -59,15 +60,15 @@ snprintf(char *str, size_t n, char const *fmt, ...) if (n > INT_MAX) n = INT_MAX; va_start(ap, fmt); - f._file = -1; - f._flags = __SWR | __SSTR; - f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._w = n; + f.pub._fileno = -1; + f.pub._flags = __SWR | __SSTR; + f._bf._base = f.pub._p = (unsigned char *)str; + f._bf._size = f.pub._w = n; f._extra = &ext; INITEXTRA(&f); ret = __vfprintf(&f, fmt, ap); /* Use unlocked __vfprintf */ if (on > 0) - *f._p = '\0'; + *f.pub._p = '\0'; va_end(ap); return (ret); } diff --git a/lib/libc/stdio/sprintf.c b/lib/libc/stdio/sprintf.c index 0e4fab32be..59ac99c688 100644 --- a/lib/libc/stdio/sprintf.c +++ b/lib/libc/stdio/sprintf.c @@ -35,13 +35,15 @@ * * @(#)sprintf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/sprintf.c,v 1.6 1999/08/28 00:01:17 peter Exp $ - * $DragonFly: src/lib/libc/stdio/sprintf.c,v 1.4 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/sprintf.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include + #include "local.h" +#include "priv_stdio.h" int sprintf(char *str, char const *fmt, ...) @@ -51,15 +53,15 @@ sprintf(char *str, char const *fmt, ...) FILE f; struct __sFILEX ext; - f._file = -1; - f._flags = __SWR | __SSTR; - f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._w = INT_MAX; + f.pub._fileno = -1; + f.pub._flags = __SWR | __SSTR; + f._bf._base = f.pub._p = (unsigned char *)str; + f._bf._size = f.pub._w = INT_MAX; f._extra = &ext; INITEXTRA(&f); va_start(ap, fmt); ret = __vfprintf(&f, fmt, ap); /* Use unlocked __vfprintf */ va_end(ap); - *f._p = 0; + *f.pub._p = 0; return (ret); } diff --git a/lib/libc/stdio/sscanf.c b/lib/libc/stdio/sscanf.c index bde5326947..69d0a6cd01 100644 --- a/lib/libc/stdio/sscanf.c +++ b/lib/libc/stdio/sscanf.c @@ -35,13 +35,15 @@ * * @(#)sscanf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/sscanf.c,v 1.6 1999/08/28 00:01:17 peter Exp $ - * $DragonFly: src/lib/libc/stdio/sscanf.c,v 1.5 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/sscanf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include + #include "local.h" +#include "priv_stdio.h" static int eofread (void *, char *, int); @@ -61,10 +63,10 @@ sscanf(const char *str, char const *fmt, ...) FILE f; struct __sFILEX ext; - f._file = -1; - f._flags = __SRD; - f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._r = strlen(str); + f.pub._fileno = -1; + f.pub._flags = __SRD; + f._bf._base = f.pub._p = (unsigned char *)str; + f._bf._size = f.pub._r = strlen(str); f._read = eofread; f._ub._base = NULL; f._lb._base = NULL; diff --git a/lib/libc/stdio/stdio.c b/lib/libc/stdio/stdio.c index c9ecf4338d..18932ae8c5 100644 --- a/lib/libc/stdio/stdio.c +++ b/lib/libc/stdio/stdio.c @@ -35,7 +35,7 @@ * * @(#)stdio.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/stdio.c,v 1.9 2000/01/27 23:06:46 jasone Exp $ - * $DragonFly: src/lib/libc/stdio/stdio.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/stdio.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -43,7 +43,9 @@ #include #include #include "un-namespace.h" + #include "local.h" +#include "priv_stdio.h" /* * Small standard I/O/seek/close functions. @@ -55,12 +57,12 @@ __sread(void *cookie, char *buf, int n) FILE *fp = cookie; int ret; - ret = _read(fp->_file, buf, (size_t)n); + ret = _read(fp->pub._fileno, buf, (size_t)n); /* if the read succeeded, update the current offset */ if (ret >= 0) fp->_offset += ret; else - fp->_flags &= ~__SOFF; /* paranoia */ + fp->pub._flags &= ~__SOFF; /* paranoia */ return (ret); } @@ -69,10 +71,10 @@ __swrite(void *cookie, char const *buf, int n) { FILE *fp = cookie; - if (fp->_flags & __SAPP) - (void) lseek(fp->_file, (off_t)0, SEEK_END); - fp->_flags &= ~__SOFF; /* in case FAPPEND mode is set */ - return (_write(fp->_file, buf, (size_t)n)); + if (fp->pub._flags & __SAPP) + (void) lseek(fp->pub._fileno, (off_t)0, SEEK_END); + fp->pub._flags &= ~__SOFF; /* in case FAPPEND mode is set */ + return (_write(fp->pub._fileno, buf, (size_t)n)); } fpos_t @@ -81,11 +83,11 @@ __sseek(void *cookie, fpos_t offset, int whence) FILE *fp = cookie; off_t ret; - ret = lseek(fp->_file, (off_t)offset, whence); + ret = lseek(fp->pub._fileno, (off_t)offset, whence); if (ret == -1) - fp->_flags &= ~__SOFF; + fp->pub._flags &= ~__SOFF; else { - fp->_flags |= __SOFF; + fp->pub._flags |= __SOFF; fp->_offset = ret; } return (ret); @@ -95,5 +97,5 @@ int __sclose(void *cookie) { - return (_close(((FILE *)cookie)->_file)); + return (_close(((FILE *)cookie)->pub._fileno)); } diff --git a/lib/libc/stdio/ungetc.c b/lib/libc/stdio/ungetc.c index b4bf487cea..3a5f1dcd03 100644 --- a/lib/libc/stdio/ungetc.c +++ b/lib/libc/stdio/ungetc.c @@ -35,7 +35,7 @@ * * @(#)ungetc.c 8.2 (Berkeley) 11/3/93 * $FreeBSD: src/lib/libc/stdio/ungetc.c,v 1.7.2.1 2001/03/05 11:27:49 obrien Exp $ - * $DragonFly: src/lib/libc/stdio/ungetc.c,v 1.5 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/ungetc.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -43,8 +43,10 @@ #include #include #include "un-namespace.h" + #include "local.h" #include "libc_private.h" +#include "priv_stdio.h" static int __submore (FILE *); @@ -71,7 +73,7 @@ __submore(FILE *fp) p += BUFSIZ - sizeof(fp->_ubuf); for (i = sizeof(fp->_ubuf); --i >= 0;) p[i] = fp->_ubuf[i]; - fp->_p = p; + fp->pub._p = p; return (0); } i = fp->_ub._size; @@ -80,7 +82,7 @@ __submore(FILE *fp) return (EOF); /* no overlap (hence can use memcpy) because we doubled the size */ (void)memcpy((void *)(p + i), (void *)p, (size_t)i); - fp->_p = p + i; + fp->pub._p = p + i; fp->_ub._base = p; fp->_ub._size = i << 1; return (0); @@ -112,23 +114,23 @@ __ungetc(int c, FILE *fp) { if (c == EOF) return(EOF); - if ((fp->_flags & __SRD) == 0) { + if ((fp->pub._flags & __SRD) == 0) { /* * Not already reading: no good unless reading-and-writing. * Otherwise, flush any current write stuff. */ - if ((fp->_flags & __SRW) == 0) { + if ((fp->pub._flags & __SRW) == 0) { return (EOF); } - if (fp->_flags & __SWR) { + if (fp->pub._flags & __SWR) { if (__sflush(fp)) { return (EOF); } - fp->_flags &= ~__SWR; - fp->_w = 0; - fp->_lbfsize = 0; + fp->pub._flags &= ~__SWR; + fp->pub._w = 0; + fp->pub._lbfsize = 0; } - fp->_flags |= __SRD; + fp->pub._flags |= __SRD; } c = (unsigned char)c; @@ -137,24 +139,24 @@ __ungetc(int c, FILE *fp) * This may require expanding the current ungetc buffer. */ if (HASUB(fp)) { - if (fp->_r >= fp->_ub._size && __submore(fp)) { + if (fp->pub._r >= fp->_ub._size && __submore(fp)) { return (EOF); } - *--fp->_p = c; - fp->_r++; + *--fp->pub._p = c; + fp->pub._r++; return (c); } - fp->_flags &= ~__SEOF; + fp->pub._flags &= ~__SEOF; /* * If we can handle this by simply backing up, do so, * but never replace the original character. * (This makes sscanf() work when scanning `const' data.) */ - if (fp->_bf._base != NULL && fp->_p > fp->_bf._base && - fp->_p[-1] == c) { - fp->_p--; - fp->_r++; + if (fp->_bf._base != NULL && fp->pub._p > fp->_bf._base && + fp->pub._p[-1] == c) { + fp->pub._p--; + fp->pub._r++; return (c); } @@ -162,12 +164,12 @@ __ungetc(int c, FILE *fp) * Create an ungetc buffer. * Initially, we will use the `reserve' buffer. */ - fp->_ur = fp->_r; - fp->_extra->_up = fp->_p; + fp->_ur = fp->pub._r; + fp->_extra->_up = fp->pub._p; fp->_ub._base = fp->_ubuf; fp->_ub._size = sizeof(fp->_ubuf); fp->_ubuf[sizeof(fp->_ubuf) - 1] = c; - fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1]; - fp->_r = 1; + fp->pub._p = &fp->_ubuf[sizeof(fp->_ubuf) - 1]; + fp->pub._r = 1; return (c); } diff --git a/lib/libc/stdio/vasprintf.c b/lib/libc/stdio/vasprintf.c index 3dde209491..d3e503ffc2 100644 --- a/lib/libc/stdio/vasprintf.c +++ b/lib/libc/stdio/vasprintf.c @@ -27,14 +27,16 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD: src/lib/libc/stdio/vasprintf.c,v 1.11 1999/08/28 00:01:19 peter Exp $ - * $DragonFly: src/lib/libc/stdio/vasprintf.c,v 1.5 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/vasprintf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include #include + #include "local.h" +#include "priv_stdio.h" int vasprintf(char **str, const char *fmt, va_list ap) @@ -43,19 +45,19 @@ vasprintf(char **str, const char *fmt, va_list ap) FILE f; struct __sFILEX ext; - f._file = -1; - f._flags = __SWR | __SSTR | __SALC; - f._bf._base = f._p = (unsigned char *)malloc(128); + f.pub._fileno = -1; + f.pub._flags = __SWR | __SSTR | __SALC; + f._bf._base = f.pub._p = (unsigned char *)malloc(128); if (f._bf._base == NULL) { *str = NULL; errno = ENOMEM; return (-1); } - f._bf._size = f._w = 127; /* Leave room for the NULL */ + f._bf._size = f.pub._w = 127; /* Leave room for the NULL */ f._extra = &ext; INITEXTRA(&f); ret = __vfprintf(&f, fmt, ap); - *f._p = '\0'; + *f.pub._p = '\0'; f._bf._base = reallocf(f._bf._base, f._bf._size + 1); if (f._bf._base == NULL) { errno = ENOMEM; diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index ae50c9c976..23fd9568bb 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -35,7 +35,7 @@ * * @(#)vfprintf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.34 2001/12/13 19:45:41 phantom Exp $ - * $DragonFly: src/lib/libc/stdio/vfprintf.c,v 1.9 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/vfprintf.c,v 1.10 2005/07/23 20:23:06 joerg Exp $ */ /* @@ -61,7 +61,7 @@ #include "libc_private.h" #include "local.h" -#include "fvwrite.h" +#include "priv_stdio.h" /* Define FLOATING_POINT to get floating point. */ #define FLOATING_POINT @@ -118,7 +118,6 @@ static char * __ujtoa(uintmax_t, char *, int, int, char *, int, char, const char *); static char * __ultoa(u_long, char *, int, int, char *, int, char, const char *); -static char * __uqtoa(u_quad_t, char *, int, int, char *); static void __find_arguments(const char *, va_list, union arg **); static void __grow_type_table(int, enum typeid **, int *); @@ -154,23 +153,23 @@ __sbprintf(FILE *fp, const char *fmt, va_list ap) unsigned char buf[BUFSIZ]; /* copy the important variables */ - fake._flags = fp->_flags & ~__SNBF; - fake._file = fp->_file; + fake.pub._flags = fp->pub._flags & ~__SNBF; + fake.pub._fileno = fp->pub._fileno; fake._cookie = fp->_cookie; fake._write = fp->_write; fake._extra = fp->_extra; /* set up the buffer */ - fake._bf._base = fake._p = buf; - fake._bf._size = fake._w = sizeof(buf); - fake._lbfsize = 0; /* not actually used, but Just In Case */ + fake._bf._base = fake.pub._p = buf; + fake._bf._size = fake.pub._w = sizeof(buf); + fake.pub._lbfsize = 0; /* not actually used, but Just In Case */ /* do the work, then copy any error status */ ret = __vfprintf(&fake, fmt, ap); if (ret >= 0 && __fflush(&fake)) ret = EOF; - if (fake._flags & __SERR) - fp->_flags |= __SERR; + if (fake.pub._flags & __SERR) + fp->pub._flags |= __SERR; return (ret); } @@ -545,8 +544,8 @@ __vfprintf(FILE *fp, const char *fmt0, va_list ap) } /* optimise fprintf(stderr) (and other unbuffered Unix files) */ - if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && - fp->_file >= 0) { + if ((fp->pub._flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && + fp->pub._fileno >= 0) { return (__sbprintf(fp, fmt0, ap)); } diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c index 0da546f37c..d460a07454 100644 --- a/lib/libc/stdio/vfscanf.c +++ b/lib/libc/stdio/vfscanf.c @@ -35,7 +35,7 @@ * * @(#)vfscanf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: /repoman/r/ncvs/src/lib/libc/stdio/vfscanf.c,v 1.35 2004/01/31 23:16:09 das Exp $ - * $DragonFly: src/lib/libc/stdio/vfscanf.c,v 1.7 2005/04/21 16:36:35 joerg Exp $ + * $DragonFly: src/lib/libc/stdio/vfscanf.c,v 1.8 2005/07/23 20:23:06 joerg Exp $ */ #include "namespace.h" @@ -50,6 +50,7 @@ #include "collate.h" #include "libc_private.h" #include "local.h" +#include "priv_stdio.h" #define FLOATING_POINT @@ -152,8 +153,8 @@ __svfscanf(FILE *fp, char const *fmt0, va_list ap) if (c == 0) return (nassigned); if (isspace(c)) { - while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p)) - nread++, fp->_r--, fp->_p++; + while ((fp->pub._r > 0 || __srefill(fp) == 0) && isspace(*fp->pub._p)) + nread++, fp->pub._r--, fp->pub._p++; continue; } if (c != '%') @@ -168,11 +169,11 @@ again: c = *fmt++; switch (c) { case '%': literal: - if (fp->_r <= 0 && __srefill(fp)) + if (fp->pub._r <= 0 && __srefill(fp)) goto input_failure; - if (*fp->_p != c) + if (*fp->pub._p != c) goto match_failure; - fp->_r--, fp->_p++; + fp->pub._r--, fp->pub._p++; nread++; continue; @@ -308,7 +309,7 @@ literal: /* * We have a conversion that requires input. */ - if (fp->_r <= 0 && __srefill(fp)) + if (fp->pub._r <= 0 && __srefill(fp)) goto input_failure; /* @@ -316,10 +317,10 @@ literal: * that suppress this. */ if ((flags & NOSKIP) == 0) { - while (isspace(*fp->_p)) { + while (isspace(*fp->pub._p)) { nread++; - if (--fp->_r > 0) - fp->_p++; + if (--fp->pub._r > 0) + fp->pub._p++; else if (__srefill(fp)) goto input_failure; } @@ -342,10 +343,10 @@ literal: if (flags & SUPPRESS) { size_t sum = 0; for (;;) { - if ((n = fp->_r) < width) { + if ((n = fp->pub._r) < width) { sum += n; width -= n; - fp->_p += n; + fp->pub._p += n; if (__srefill(fp)) { if (sum == 0) goto input_failure; @@ -353,8 +354,8 @@ literal: } } else { sum += width; - fp->_r -= width; - fp->_p += width; + fp->pub._r -= width; + fp->pub._p += width; break; } } @@ -378,11 +379,11 @@ literal: /* take only those things in the class */ if (flags & SUPPRESS) { n = 0; - while (ccltab[*fp->_p]) { - n++, fp->_r--, fp->_p++; + while (ccltab[*fp->pub._p]) { + n++, fp->pub._r--, fp->pub._p++; if (--width == 0) break; - if (fp->_r <= 0 && __srefill(fp)) { + if (fp->pub._r <= 0 && __srefill(fp)) { if (n == 0) goto input_failure; break; @@ -392,12 +393,12 @@ literal: goto match_failure; } else { p0 = p = va_arg(ap, char *); - while (ccltab[*fp->_p]) { - fp->_r--; - *p++ = *fp->_p++; + while (ccltab[*fp->pub._p]) { + fp->pub._r--; + *p++ = *fp->pub._p++; if (--width == 0) break; - if (fp->_r <= 0 && __srefill(fp)) { + if (fp->pub._r <= 0 && __srefill(fp)) { if (p == p0) goto input_failure; break; @@ -419,22 +420,22 @@ literal: width = (size_t)~0; if (flags & SUPPRESS) { n = 0; - while (!isspace(*fp->_p)) { - n++, fp->_r--, fp->_p++; + while (!isspace(*fp->pub._p)) { + n++, fp->pub._r--, fp->pub._p++; if (--width == 0) break; - if (fp->_r <= 0 && __srefill(fp)) + if (fp->pub._r <= 0 && __srefill(fp)) break; } nread += n; } else { p0 = p = va_arg(ap, char *); - while (!isspace(*fp->_p)) { - fp->_r--; - *p++ = *fp->_p++; + while (!isspace(*fp->pub._p)) { + fp->pub._r--; + *p++ = *fp->pub._p++; if (--width == 0) break; - if (fp->_r <= 0 && __srefill(fp)) + if (fp->pub._r <= 0 && __srefill(fp)) break; } *p = 0; @@ -457,7 +458,7 @@ literal: #endif flags |= SIGNOK | NDIGITS | NZDIGITS; for (p = buf; width; width--) { - c = *fp->_p; + c = *fp->pub._p; /* * Switch on the character; `goto ok' * if we accept it as a part of number. @@ -546,8 +547,8 @@ literal: * c is legal: store it and look at the next. */ *p++ = c; - if (--fp->_r > 0) - fp->_p++; + if (--fp->pub._r > 0) + fp->pub._p++; else if (__srefill(fp)) break; /* EOF */ } @@ -603,7 +604,7 @@ literal: #endif flags |= SIGNOK | NDIGITS | DPTOK | EXPOK; for (p = buf; width; width--) { - c = *fp->_p; + c = *fp->pub._p; /* * This code mimicks the integer conversion * code, but is much simpler. @@ -642,8 +643,8 @@ literal: break; fok: *p++ = c; - if (--fp->_r > 0) - fp->_p++; + if (--fp->pub._r > 0) + fp->pub._p++; else if (__srefill(fp)) break; /* EOF */ } diff --git a/lib/libc/stdio/vsnprintf.c b/lib/libc/stdio/vsnprintf.c index 11ee3fc23f..df481832a4 100644 --- a/lib/libc/stdio/vsnprintf.c +++ b/lib/libc/stdio/vsnprintf.c @@ -35,13 +35,15 @@ * * @(#)vsnprintf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/vsnprintf.c,v 1.12.2.1 2002/09/23 06:58:17 maxim Exp $ - * $DragonFly: src/lib/libc/stdio/vsnprintf.c,v 1.5 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/vsnprintf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include + #include "local.h" +#include "priv_stdio.h" int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) @@ -62,14 +64,14 @@ vsnprintf(char *str, size_t n, const char *fmt, va_list ap) str = &dummy; n = 1; } - f._file = -1; - f._flags = __SWR | __SSTR; - f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._w = n; + f.pub._fileno = -1; + f.pub._flags = __SWR | __SSTR; + f._bf._base = f.pub._p = (unsigned char *)str; + f._bf._size = f.pub._w = n; f._extra = &ext; INITEXTRA(&f); ret = __vfprintf(&f, fmt, ap); if (on > 0) - *f._p = '\0'; + *f.pub._p = '\0'; return (ret); } diff --git a/lib/libc/stdio/vsprintf.c b/lib/libc/stdio/vsprintf.c index d8dd8300fd..823fc9f274 100644 --- a/lib/libc/stdio/vsprintf.c +++ b/lib/libc/stdio/vsprintf.c @@ -35,13 +35,15 @@ * * @(#)vsprintf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/vsprintf.c,v 1.6 1999/08/28 00:01:21 peter Exp $ - * $DragonFly: src/lib/libc/stdio/vsprintf.c,v 1.5 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/vsprintf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include + #include "local.h" +#include "priv_stdio.h" int vsprintf(char *str, const char *fmt, va_list ap) @@ -50,13 +52,13 @@ vsprintf(char *str, const char *fmt, va_list ap) FILE f; struct __sFILEX ext; - f._file = -1; - f._flags = __SWR | __SSTR; - f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._w = INT_MAX; + f.pub._fileno = -1; + f.pub._flags = __SWR | __SSTR; + f._bf._base = f.pub._p = (unsigned char *)str; + f._bf._size = f.pub._w = INT_MAX; f._extra = &ext; INITEXTRA(&f); ret = __vfprintf(&f, fmt, ap); - *f._p = 0; + *f.pub._p = 0; return (ret); } diff --git a/lib/libc/stdio/vsscanf.c b/lib/libc/stdio/vsscanf.c index 8d658f9a1f..bfe0db9b58 100644 --- a/lib/libc/stdio/vsscanf.c +++ b/lib/libc/stdio/vsscanf.c @@ -35,13 +35,15 @@ * * @(#)vsscanf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/vsscanf.c,v 1.7 1999/08/28 00:01:22 peter Exp $ - * $DragonFly: src/lib/libc/stdio/vsscanf.c,v 1.6 2005/05/09 12:43:40 davidxu Exp $ + * $DragonFly: src/lib/libc/stdio/vsscanf.c,v 1.7 2005/07/23 20:23:06 joerg Exp $ */ #include #include #include + #include "local.h" +#include "priv_stdio.h" static int eofread (void *, char *, int); @@ -60,10 +62,10 @@ vsscanf(const char *str, const char *fmt, va_list ap) FILE f; struct __sFILEX ext; - f._file = -1; - f._flags = __SRD; - f._bf._base = f._p = (unsigned char *)str; - f._bf._size = f._r = strlen(str); + f.pub._fileno = -1; + f.pub._flags = __SRD; + f._bf._base = f.pub._p = (unsigned char *)str; + f._bf._size = f.pub._r = strlen(str); f._read = eofread; f._ub._base = NULL; f._lb._base = NULL; diff --git a/lib/libc/stdio/wbuf.c b/lib/libc/stdio/wbuf.c index 04034b9a28..c24747111c 100644 --- a/lib/libc/stdio/wbuf.c +++ b/lib/libc/stdio/wbuf.c @@ -35,11 +35,13 @@ * * @(#)wbuf.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/wbuf.c,v 1.6 1999/08/28 00:01:22 peter Exp $ - * $DragonFly: src/lib/libc/stdio/wbuf.c,v 1.5 2005/01/31 22:29:40 dillon Exp $ + * $DragonFly: src/lib/libc/stdio/wbuf.c,v 1.6 2005/07/23 20:23:06 joerg Exp $ */ #include + #include "local.h" +#include "priv_stdio.h" /* * Write the given character into the (probably full) buffer for @@ -60,7 +62,7 @@ __swbuf(int c, FILE *fp) * If we did not do this, a sufficient number of putc() * calls might wrap _w from negative to positive. */ - fp->_w = fp->_lbfsize; + fp->pub._w = fp->pub._lbfsize; if (cantwrite(fp)) return (EOF); c = (unsigned char)c; @@ -74,15 +76,15 @@ __swbuf(int c, FILE *fp) * guarantees that putc() will always call wbuf() by setting _w * to 0, so we need not do anything else. */ - n = fp->_p - fp->_bf._base; + n = fp->pub._p - fp->_bf._base; if (n >= fp->_bf._size) { if (__fflush(fp)) return (EOF); n = 0; } - fp->_w--; - *fp->_p++ = c; - if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) + fp->pub._w--; + *fp->pub._p++ = c; + if (++n == fp->_bf._size || (fp->pub._flags & __SLBF && c == '\n')) if (__fflush(fp)) return (EOF); return (c); diff --git a/lib/libc/stdio/wsetup.c b/lib/libc/stdio/wsetup.c index 64028033c6..b2a209ad63 100644 --- a/lib/libc/stdio/wsetup.c +++ b/lib/libc/stdio/wsetup.c @@ -35,12 +35,14 @@ * * @(#)wsetup.c 8.1 (Berkeley) 6/4/93 * $FreeBSD: src/lib/libc/stdio/wsetup.c,v 1.6 1999/08/28 00:01:22 peter Exp $ - * $DragonFly: src/lib/libc/stdio/wsetup.c,v 1.4 2004/06/07 20:35:41 hmp Exp $ + * $DragonFly: src/lib/libc/stdio/wsetup.c,v 1.5 2005/07/23 20:23:06 joerg Exp $ */ #include #include + #include "local.h" +#include "priv_stdio.h" /* * Various output routines call wsetup to be sure it is safe to write, @@ -57,18 +59,18 @@ __swsetup(FILE *fp) /* * If we are not writing, we had better be reading and writing. */ - if ((fp->_flags & __SWR) == 0) { - if ((fp->_flags & __SRW) == 0) + if ((fp->pub._flags & __SWR) == 0) { + if ((fp->pub._flags & __SRW) == 0) return (EOF); - if (fp->_flags & __SRD) { + if (fp->pub._flags & __SRD) { /* clobber any ungetc data */ if (HASUB(fp)) FREEUB(fp); - fp->_flags &= ~(__SRD|__SEOF); - fp->_r = 0; - fp->_p = fp->_bf._base; + fp->pub._flags &= ~(__SRD|__SEOF); + fp->pub._r = 0; + fp->pub._p = fp->_bf._base; } - fp->_flags |= __SWR; + fp->pub._flags |= __SWR; } /* @@ -76,15 +78,15 @@ __swsetup(FILE *fp) */ if (fp->_bf._base == NULL) __smakebuf(fp); - if (fp->_flags & __SLBF) { + if (fp->pub._flags & __SLBF) { /* * It is line buffered, so make _lbfsize be -_bufsize * for the putc() macro. We will change _lbfsize back * to 0 whenever we turn off __SWR. */ - fp->_w = 0; - fp->_lbfsize = -fp->_bf._size; + fp->pub._w = 0; + fp->pub._lbfsize = -fp->_bf._size; } else - fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size; + fp->pub._w = fp->pub._flags & __SNBF ? 0 : fp->_bf._size; return (0); } diff --git a/lib/libftpio/ftpio.c b/lib/libftpio/ftpio.c index 0d83f67ad5..1c8e494728 100644 --- a/lib/libftpio/ftpio.c +++ b/lib/libftpio/ftpio.c @@ -15,7 +15,7 @@ * `state' of FTP_t * * $FreeBSD: src/lib/libftpio/ftpio.c,v 1.33.2.4 2002/07/25 15:25:32 ume Exp $ - * $DragonFly: src/lib/libftpio/ftpio.c,v 1.7 2004/08/19 23:57:46 joerg Exp $ + * $DragonFly: src/lib/libftpio/ftpio.c,v 1.8 2005/07/23 20:23:06 joerg Exp $ * */ @@ -95,16 +95,6 @@ int FtpTimedOut; /* FTP unhappy status codes */ #define FTP_TIMED_OUT 421 -/* - * XXX - * gross! evil! bad! We really need an access primitive for cookie in stdio - * itself. - * it's too convenient a hook to bury and it's already exported through funopen - * as it is, so... - * XXX - */ -#define fcookie(fp) ((fp)->_cookie) - /* Placeholder in case we want to do any pre-init stuff at some point */ int networkInit(void) @@ -306,7 +296,6 @@ ftpLoginAf(const char *host, int af, const char *user, const char *passwd, fp = NULL; if (n && ftp_login_session(n, host, af, user, passwd, port, verbose) == SUCCESS) { fp = funopen(n, ftp_read_method, ftp_write_method, NULL, ftp_close_method); /* BSD 4.4 function! */ - fp->_file = n->fd_ctrl; } if (retcode) { if (!n)