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