]> git.cworth.org Git - tar/blob - lib/fseeko.c
Imported Upstream version 1.22
[tar] / lib / 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-2009 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 #undef fseeko
31 #if !HAVE_FSEEKO
32 # undef fseek
33 # define fseeko fseek
34 #endif
35
36 int
37 rpl_fseeko (FILE *fp, off_t offset, int whence)
38 {
39 #if LSEEK_PIPE_BROKEN
40   /* mingw gives bogus answers rather than failure on non-seekable files.  */
41   if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
42     return EOF;
43 #endif
44
45   /* These tests are based on fpurge.c.  */
46 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
47   if (fp->_IO_read_end == fp->_IO_read_ptr
48       && fp->_IO_write_ptr == fp->_IO_write_base
49       && fp->_IO_save_base == NULL)
50 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
51 # if defined __SL64 && defined __SCLE /* Cygwin */
52   if ((fp->_flags & __SL64) == 0)
53     {
54       /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
55          mode; but has an fseeko that requires 64-bit mode.  */
56       FILE *tmp = fopen ("/dev/null", "r");
57       if (!tmp)
58         return -1;
59       fp->_flags |= __SL64;
60       fp->_seek64 = tmp->_seek64;
61       fclose (tmp);
62     }
63 # endif
64   if (fp_->_p == fp_->_bf._base
65       && fp_->_r == 0
66       && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
67                      ? fp_->_bf._size
68                      : 0)
69       && fp_ub._base == NULL)
70 #elif defined __EMX__               /* emx+gcc */
71   if (fp->_ptr == fp->_buffer
72       && fp->_rcount == 0
73       && fp->_wcount == 0
74       && fp->_ungetc_count == 0)
75 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
76   if (fp_->_ptr == fp_->_base
77       && (fp_->_ptr == NULL || fp_->_cnt == 0))
78 #elif defined __UCLIBC__            /* uClibc */
79   if (((fp->__modeflags & __FLAG_WRITING) == 0
80        || fp->__bufpos == fp->__bufstart)
81       && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
82           || fp->__bufpos == fp->__bufread))
83 #elif defined __QNX__               /* QNX */
84   if ((fp->_Mode & _MWRITE ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
85       && fp->_Rback == fp->_Back + sizeof (fp->_Back)
86       && fp->_Rsave == NULL)
87 #else
88   #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
89 #endif
90     {
91       /* We get here when an fflush() call immediately preceded this one.  We
92          know there are no buffers.
93          POSIX requires us to modify the file descriptor's position.
94          But we cannot position beyond end of file here.  */
95       off_t pos =
96         lseek (fileno (fp),
97                whence == SEEK_END && offset > 0 ? 0 : offset,
98                whence);
99       if (pos == -1)
100         {
101 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
102           fp_->_flags &= ~__SOFF;
103 #endif
104           return -1;
105         }
106
107 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
108       fp->_flags &= ~_IO_EOF_SEEN;
109 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
110       fp_->_offset = pos;
111       fp_->_flags |= __SOFF;
112       fp_->_flags &= ~__SEOF;
113 #elif defined __EMX__               /* emx+gcc */
114       fp->_flags &= ~_IOEOF;
115 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
116       fp->_flag &= ~_IOEOF;
117 #endif
118       /* If we were not requested to position beyond end of file, we're
119          done.  */
120       if (!(whence == SEEK_END && offset > 0))
121         return 0;
122     }
123   return fseeko (fp, offset, whence);
124 }