Another fseek test.
[gnulib.git] / lib / fseeko.c
1 /* An fseeko() function that, together with fflush(), is POSIX compliant.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include <stdio.h>
22
23 /* Get off_t and lseek.  */
24 #include <unistd.h>
25
26 #undef fseeko
27 #if !HAVE_FSEEKO
28 # undef fseek
29 # define fseeko fseek
30
31 # include <verify.h>
32 verify (sizeof (off_t) == sizeof (long));
33 #endif
34
35 int
36 rpl_fseeko (FILE *fp, off_t offset, int whence)
37 {
38 #if LSEEK_PIPE_BROKEN
39   /* mingw gives bogus answers rather than failure on non-seekable files.  */
40   if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
41     return EOF;
42 #endif
43
44   /* These tests are based on fpurge.c.  */
45 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
46   if (fp->_IO_read_end == fp->_IO_read_ptr
47       && fp->_IO_write_ptr == fp->_IO_write_base
48       && fp->_IO_save_base == NULL)
49 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
50 # if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
51    /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
52       and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
53 #  define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
54 # else                                         /* FreeBSD, MacOS X, Cygwin */
55 #  define fp_ub fp->_ub
56 # endif
57 # if defined __SL64 && defined __SCLE /* Cygwin */
58   if ((fp->_flags & __SL64) == 0)
59     {
60       /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
61          mode; but has an fseeko that requires 64-bit mode.  */
62       FILE *tmp = fopen ("/dev/null", "r");
63       if (!tmp)
64         return -1;
65       fp->_flags |= __SL64;
66       fp->_seek64 = tmp->_seek64;
67       fclose (tmp);
68     }
69 # endif
70   if (fp->_p == fp->_bf._base
71       && fp->_r == 0
72       && fp->_w == ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
73                     ? fp->_bf._size
74                     : 0)
75       && fp_ub._base == NULL)
76 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
77 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
78 #  define fp_ ((struct { unsigned char *_ptr; \
79                          unsigned char *_base; \
80                          unsigned char *_end; \
81                          long _cnt; \
82                          int _file; \
83                          unsigned int _flag; \
84                        } *) fp)
85   if (fp_->_ptr == fp_->_base
86       && (fp_->_ptr == NULL || fp_->_cnt == 0))
87 # else
88   if (fp->_ptr == fp->_base
89       && (fp->_ptr == NULL || fp->_cnt == 0))
90 # endif
91 #elif defined __UCLIBC__            /* uClibc */
92   if (((fp->__modeflags & __FLAG_WRITING) == 0
93        || fp->__bufpos == fp->__bufstart)
94       && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
95           || fp->__bufpos == fp->__bufread))
96 #elif defined __QNX__               /* QNX */
97   if ((fp->_Mode & _MWRITE ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
98       && fp->_Rback == fp->_Back + sizeof (fp->_Back)
99       && fp->_Rsave == NULL)
100 #else
101   #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
102 #endif
103     {
104       off_t pos = lseek (fileno (fp), offset, whence);
105       if (pos == -1)
106         {
107 #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
108           fp->_flags &= ~__SOFF;
109 #endif
110           return -1;
111         }
112       else
113         {
114 #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
115           fp->_offset = pos;
116           fp->_flags |= __SOFF;
117           fp->_flags &= ~__SEOF;
118 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
119           fp->_flag &= ~_IOEOF;
120 #endif
121           return 0;
122         }
123     }
124   else
125     return fseeko (fp, offset, whence);
126 }