]> git.cworth.org Git - tar/blob - gnu/fseeko.c
Imported Upstream version 1.24
[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-2010 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, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <stdio.h>
24
25 /* Get off_t and lseek.  */
26 #include <unistd.h>
27
28 #include "stdio-impl.h"
29
30 int
31 fseeko (FILE *fp, off_t offset, int whence)
32 #undef fseeko
33 #if !HAVE_FSEEKO
34 # undef fseek
35 # define fseeko fseek
36 #endif
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_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
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 || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
50 # if defined __SL64 && defined __SCLE /* Cygwin */
51   if ((fp->_flags & __SL64) == 0)
52     {
53       /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
54          mode; but has an fseeko that requires 64-bit mode.  */
55       FILE *tmp = fopen ("/dev/null", "r");
56       if (!tmp)
57         return -1;
58       fp->_flags |= __SL64;
59       fp->_seek64 = tmp->_seek64;
60       fclose (tmp);
61     }
62 # endif
63   if (fp_->_p == fp_->_bf._base
64       && fp_->_r == 0
65       && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
66                      ? fp_->_bf._size
67                      : 0)
68       && fp_ub._base == NULL)
69 #elif defined __EMX__               /* emx+gcc */
70   if (fp->_ptr == fp->_buffer
71       && fp->_rcount == 0
72       && fp->_wcount == 0
73       && fp->_ungetc_count == 0)
74 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
75   if (fp_->_ptr == fp_->_base
76       && (fp_->_ptr == NULL || fp_->_cnt == 0))
77 #elif defined __UCLIBC__            /* uClibc */
78   if (((fp->__modeflags & __FLAG_WRITING) == 0
79        || fp->__bufpos == fp->__bufstart)
80       && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
81           || fp->__bufpos == fp->__bufread))
82 #elif defined __QNX__               /* QNX */
83   if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
84       && fp->_Rback == fp->_Back + sizeof (fp->_Back)
85       && fp->_Rsave == NULL)
86 #elif defined __MINT__              /* Atari FreeMiNT */
87   if (fp->__bufp == fp->__buffer
88       && fp->__get_limit == fp->__bufp
89       && fp->__put_limit == fp->__bufp
90       && !fp->__pushed_back)
91 #else
92   #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
93 #endif
94     {
95       /* We get here when an fflush() call immediately preceded this one.  We
96          know there are no buffers.
97          POSIX requires us to modify the file descriptor's position.
98          But we cannot position beyond end of file here.  */
99       off_t pos =
100         lseek (fileno (fp),
101                whence == SEEK_END && offset > 0 ? 0 : offset,
102                whence);
103       if (pos == -1)
104         {
105 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
106           fp_->_flags &= ~__SOFF;
107 #endif
108           return -1;
109         }
110
111 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
112       fp->_flags &= ~_IO_EOF_SEEN;
113 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
114 # if defined __CYGWIN__
115       /* fp_->_offset is typed as an integer.  */
116       fp_->_offset = pos;
117 # else
118       /* fp_->_offset is an fpos_t.  */
119       {
120         /* Use a union, since on NetBSD, the compilation flags
121            determine whether fpos_t is typedef'd to off_t or a struct
122            containing a single off_t member.  */
123         union
124           {
125             fpos_t f;
126             off_t o;
127           } u;
128         u.o = pos;
129         fp_->_offset = u.f;
130       }
131 # endif
132       fp_->_flags |= __SOFF;
133       fp_->_flags &= ~__SEOF;
134 #elif defined __EMX__               /* emx+gcc */
135       fp->_flags &= ~_IOEOF;
136 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
137       fp->_flag &= ~_IOEOF;
138 #elif defined __MINT__              /* Atari FreeMiNT */
139       fp->__offset = pos;
140       fp->__eof = 0;
141 #endif
142       /* If we were not requested to position beyond end of file, we're
143          done.  */
144       if (!(whence == SEEK_END && offset > 0))
145         return 0;
146     }
147   return fseeko (fp, offset, whence);
148 }