X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffreadptr.c;h=05f501bfeb34a8fadbfb810977a805f58748058e;hb=d1fb15ba12436afdd742eaff1ec46f75ee397bda;hp=46e9dac9d50067bdf2e26e97f1871954d2391e15;hpb=56690adc74a0b9d99a9a313947cf7515ce8a40db;p=gnulib.git diff --git a/lib/freadptr.c b/lib/freadptr.c index 46e9dac9d..05f501bfe 100644 --- a/lib/freadptr.c +++ b/lib/freadptr.c @@ -1,5 +1,5 @@ /* Retrieve information about a FILE stream. - Copyright (C) 2007-2008 Free Software Foundation, Inc. + Copyright (C) 2007-2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,38 +17,87 @@ #include /* Specification. */ -#include "freadahead.h" +#include "freadptr.h" + +#include + +#include "stdio-impl.h" const char * -freadptr (FILE *fp) +freadptr (FILE *fp, size_t *sizep) { + size_t size; + /* Keep this code in sync with freadahead! */ -#if defined _IO_ferror_unlocked /* GNU libc, BeOS */ +#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */ + if (fp->_IO_write_ptr > fp->_IO_write_base) + return NULL; + size = fp->_IO_read_end - fp->_IO_read_ptr; + if (size == 0) + return NULL; + *sizep = size; return (const char *) fp->_IO_read_ptr; -#elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ - return (const char *) fp->_p; -#elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */ -# if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */ -# define fp_ ((struct { unsigned char *_ptr; \ - unsigned char *_base; \ - unsigned char *_end; \ - long _cnt; \ - int _file; \ - unsigned int _flag; \ - } *) fp) +#elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */ + if ((fp_->_flags & __SWR) != 0 || fp_->_r < 0) + return NULL; + size = fp_->_r; + if (size == 0) + return NULL; + *sizep = size; + return (const char *) fp_->_p; +#elif defined __EMX__ /* emx+gcc */ + if ((fp->_flags & _IOWRT) != 0) + return NULL; + /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0, + fp->_ungetc_count = 0 implies fp->_rcount >= 0. */ + if (fp->_rcount <= 0) + return NULL; + if (!(fp->_ungetc_count == 0)) + abort (); + *sizep = fp->_rcount; + return fp->_ptr; +#elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */ + if ((fp_->_flag & _IOWRT) != 0) + return NULL; + size = fp_->_cnt; + if (size == 0) + return NULL; + *sizep = size; return (const char *) fp_->_ptr; -# else - return (const char *) fp->_ptr; -# endif #elif defined __UCLIBC__ /* uClibc */ # ifdef __STDIO_BUFFERS + if (fp->__modeflags & __FLAG_WRITING) + return NULL; + size = fp->__bufread - fp->__bufpos; + if (size == 0) + return NULL; + *sizep = size; return (const char *) fp->__bufpos; # else return NULL; # endif #elif defined __QNX__ /* QNX */ + if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0) + return NULL; + /* fp->_Buf <= fp->_Next <= fp->_Rend */ + size = fp->_Rend - fp->_Next; + if (size == 0) + return NULL; + *sizep = size; return (const char *) fp->_Next; +#elif defined __MINT__ /* Atari FreeMiNT */ + if (!fp->__mode.__read) + return NULL; + size = fp->__get_limit - fp->__bufp; + if (size == 0) + return NULL; + *sizep = size; + return fp->__bufp; +#elif defined SLOW_BUT_NO_HACKS /* users can define this */ + /* This implementation is correct on any ANSI C platform. It is just + awfully slow. */ + return NULL; #else - #error "Please port gnulib freadptr.c to your platform! Look at the definition of getc, getc_unlocked on your system, then report this to bug-gnulib." + #error "Please port gnulib freadptr.c to your platform! Look at the definition of fflush, fread, getc, getc_unlocked on your system, then report this to bug-gnulib." #endif }