7c1ad9b4026a8e15b566bc7f98afad55b63d9254
[debian/tar] / gnu / fseeko.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* An fseeko() function that, together with fflush(), is POSIX compliant.
4    Copyright (C) 2007-2013 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <stdio.h>
23
24 /* Get off_t, lseek, _POSIX_VERSION.  */
25 #include <unistd.h>
26
27 #include "stdio-impl.h"
28
29 int
30 fseeko (FILE *fp, off_t offset, int whence)
31 #undef fseeko
32 #if !HAVE_FSEEKO
33 # undef fseek
34 # define fseeko fseek
35 #endif
36 #if _GL_WINDOWS_64_BIT_OFF_T
37 # undef fseeko
38 # if HAVE__FSEEKI64 /* msvc, mingw64 */
39 #  define fseeko _fseeki64
40 # else /* mingw */
41 #  define fseeko fseeko64
42 # endif
43 #endif
44 {
45 #if LSEEK_PIPE_BROKEN
46   /* mingw gives bogus answers rather than failure on non-seekable files.  */
47   if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
48     return EOF;
49 #endif
50
51   /* These tests are based on fpurge.c.  */
52 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
53   if (fp->_IO_read_end == fp->_IO_read_ptr
54       && fp->_IO_write_ptr == fp->_IO_write_base
55       && fp->_IO_save_base == NULL)
56 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
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 __EMX__               /* emx+gcc */
77   if (fp->_ptr == fp->_buffer
78       && fp->_rcount == 0
79       && fp->_wcount == 0
80       && fp->_ungetc_count == 0)
81 #elif defined __minix               /* Minix */
82   if (fp_->_ptr == fp_->_buf
83       && (fp_->_ptr == NULL || fp_->_count == 0))
84 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
85   if (fp_->_ptr == fp_->_base
86       && (fp_->_ptr == NULL || fp_->_cnt == 0))
87 #elif defined __UCLIBC__            /* uClibc */
88   if (((fp->__modeflags & __FLAG_WRITING) == 0
89        || fp->__bufpos == fp->__bufstart)
90       && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
91           || fp->__bufpos == fp->__bufread))
92 #elif defined __QNX__               /* QNX */
93   if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
94       && fp->_Rback == fp->_Back + sizeof (fp->_Back)
95       && fp->_Rsave == NULL)
96 #elif defined __MINT__              /* Atari FreeMiNT */
97   if (fp->__bufp == fp->__buffer
98       && fp->__get_limit == fp->__bufp
99       && fp->__put_limit == fp->__bufp
100       && !fp->__pushed_back)
101 #elif defined EPLAN9                /* Plan9 */
102   if (fp->rp == fp->buf
103       && fp->wp == fp->buf)
104 #elif FUNC_FFLUSH_STDIN < 0 && 200809 <= _POSIX_VERSION
105   /* Cross-compiling to some other system advertising conformance to
106      POSIX.1-2008 or later.  Assume fseeko and fflush work as advertised.
107      If this assumption is incorrect, please report the bug to
108      bug-gnulib.  */
109   if (0)
110 #else
111   #error "Please port gnulib fseeko.c to your platform! Look at the code in fseeko.c, then report this to bug-gnulib."
112 #endif
113     {
114       /* We get here when an fflush() call immediately preceded this one (or
115          if ftell() has created buffers but no I/O has occurred on a
116          newly-opened stream).  We know there are no buffers.  */
117       off_t pos = lseek (fileno (fp), offset, whence);
118       if (pos == -1)
119         {
120 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
121           fp_->_flags &= ~__SOFF;
122 #endif
123           return -1;
124         }
125
126 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
127       fp->_flags &= ~_IO_EOF_SEEN;
128       fp->_offset = pos;
129 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
130 # if defined __CYGWIN__
131       /* fp_->_offset is typed as an integer.  */
132       fp_->_offset = pos;
133 # else
134       /* fp_->_offset is an fpos_t.  */
135       {
136         /* Use a union, since on NetBSD, the compilation flags
137            determine whether fpos_t is typedef'd to off_t or a struct
138            containing a single off_t member.  */
139         union
140           {
141             fpos_t f;
142             off_t o;
143           } u;
144         u.o = pos;
145         fp_->_offset = u.f;
146       }
147 # endif
148       fp_->_flags |= __SOFF;
149       fp_->_flags &= ~__SEOF;
150 #elif defined __EMX__               /* emx+gcc */
151       fp->_flags &= ~_IOEOF;
152 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
153       fp->_flag &= ~_IOEOF;
154 #elif defined __MINT__              /* Atari FreeMiNT */
155       fp->__offset = pos;
156       fp->__eof = 0;
157 #endif
158       return 0;
159     }
160   return fseeko (fp, offset, whence);
161 }