Port the extended stdio functions to emx+gcc.
[gnulib.git] / lib / fseeko.c
1 /* An fseeko() function that, together with fflush(), is POSIX compliant.
2    Copyright (C) 2007-2008 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 #endif
31
32 int
33 rpl_fseeko (FILE *fp, off_t offset, int whence)
34 {
35 #if LSEEK_PIPE_BROKEN
36   /* mingw gives bogus answers rather than failure on non-seekable files.  */
37   if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
38     return EOF;
39 #endif
40
41   /* These tests are based on fpurge.c.  */
42 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
43   if (fp->_IO_read_end == fp->_IO_read_ptr
44       && fp->_IO_write_ptr == fp->_IO_write_base
45       && fp->_IO_save_base == NULL)
46 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
47 # if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
48    /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
49       and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
50 #  define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
51 # else                                         /* FreeBSD, MacOS X, Cygwin */
52 #  define fp_ub fp->_ub
53 # endif
54 # if defined __SL64 && defined __SCLE /* Cygwin */
55   if ((fp->_flags & __SL64) == 0)
56     {
57       /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
58          mode; but has an fseeko that requires 64-bit mode.  */
59       FILE *tmp = fopen ("/dev/null", "r");
60       if (!tmp)
61         return -1;
62       fp->_flags |= __SL64;
63       fp->_seek64 = tmp->_seek64;
64       fclose (tmp);
65     }
66 # endif
67   if (fp->_p == fp->_bf._base
68       && fp->_r == 0
69       && fp->_w == ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
70                     ? fp->_bf._size
71                     : 0)
72       && fp_ub._base == NULL)
73 #elif defined __EMX__               /* emx+gcc */
74   if (fp->_ptr == fp->_buffer
75       && fp->_rcount == 0
76       && fp->_wcount == 0
77       && fp->_ungetc_count == 0)
78 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
79 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
80 #  define fp_ ((struct { unsigned char *_ptr; \
81                          unsigned char *_base; \
82                          unsigned char *_end; \
83                          long _cnt; \
84                          int _file; \
85                          unsigned int _flag; \
86                        } *) fp)
87   if (fp_->_ptr == fp_->_base
88       && (fp_->_ptr == NULL || fp_->_cnt == 0))
89 # else
90 #  if defined _SCO_DS               /* OpenServer */
91 #   define _base __base
92 #   define _ptr __ptr
93 #   define _cnt __cnt
94 #  endif
95   if (fp->_ptr == fp->_base
96       && (fp->_ptr == NULL || fp->_cnt == 0))
97 # endif
98 #elif defined __UCLIBC__            /* uClibc */
99   if (((fp->__modeflags & __FLAG_WRITING) == 0
100        || fp->__bufpos == fp->__bufstart)
101       && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
102           || fp->__bufpos == fp->__bufread))
103 #elif defined __QNX__               /* QNX */
104   if ((fp->_Mode & _MWRITE ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
105       && fp->_Rback == fp->_Back + sizeof (fp->_Back)
106       && fp->_Rsave == NULL)
107 #else
108   #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
109 #endif
110     {
111       off_t pos = lseek (fileno (fp), offset, whence);
112       if (pos == -1)
113         {
114 #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
115           fp->_flags &= ~__SOFF;
116 #endif
117           return -1;
118         }
119       else
120         {
121 #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
122           fp->_offset = pos;
123           fp->_flags |= __SOFF;
124           fp->_flags &= ~__SEOF;
125 #elif defined __EMX__               /* emx+gcc */
126           fp->_flags &= ~_IOEOF;
127 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
128 # if defined _SCO_DS                /* OpenServer */
129 #  define _flag __flag
130 # endif
131           fp->_flag &= ~_IOEOF;
132 #endif
133           return 0;
134         }
135     }
136   else
137     return fseeko (fp, offset, whence);
138 }