e7a7f74952fd1542fa64022e280f9af2cc4d0368
[gnulib.git] / lib / fseeko.c
1 /* An fseek() function that, together with fflush(), is POSIX compliant.
2    Copyright (C) 2007 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 # define fseeko fseek
29 #endif
30
31 int
32 rpl_fseeko (FILE *fp, off_t offset, int whence)
33 {
34   /* These tests are based on fpurge.c.  */
35 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
36   if (fp->_IO_read_end == fp->_IO_read_ptr
37       && fp->_IO_write_ptr == fp->_IO_write_base
38       && fp->_IO_save_base == NULL)
39 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
40 # if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
41    /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
42       and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
43 #  define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
44 # else                                         /* FreeBSD, MacOS X, Cygwin */
45 #  define fp_ub fp->_ub
46 # endif
47   if (fp->_p == fp->_bf._base
48       && fp->_r == 0
49       && fp->_w == ((fp->_flags & (__SLBF | __SNBF)) == 0 /* fully buffered? */
50                     ? fp->_bf._size
51                     : 0)
52       && fp_ub._base == NULL)
53 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
54 # if defined __sun && defined __sparc && defined _LP64 /* Solaris/SPARC 64-bit */
55 #  define fp_ ((struct { unsigned char *_ptr; \
56                          unsigned char *_base; \
57                          unsigned char *_end; \
58                          long _cnt; \
59                        } *) fp)
60   if (fp_->_ptr == fp_->_base
61       && (fp_->_ptr == NULL || fp_->_cnt == 0))
62 # else
63   if (fp->_ptr == fp->_base
64       && (fp->_ptr == NULL || fp->_cnt == 0))
65 # endif
66 #else
67   #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
68 #endif
69     {
70       off_t pos = lseek (fileno (fp), offset, whence);
71       if (pos == -1)
72         {
73 #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
74           fp->_flags &= ~__SOFF;
75 #endif
76           return -1;
77         }
78       else
79         {
80 #if defined __sferror               /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
81           fp->_offset = pos;
82           fp->_flags |= __SOFF;
83 #endif
84           return 0;
85         }
86     }
87   else
88     return fseeko (fp, offset, whence);
89 }