1/* -*- buffer-read-only: t -*- vi: set ro: */
2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3#line 1
4/* An fseeko() function that, together with fflush(), is POSIX compliant.
5   Copyright (C) 2007-2010 Free Software Foundation, Inc.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3, or (at your option)
10   any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License along
18   with this program; if not, write to the Free Software Foundation,
19   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21#include <config.h>
22
23/* Specification.  */
24#include <stdio.h>
25
26/* Get off_t and lseek.  */
27#include <unistd.h>
28
29#include "stdio-impl.h"
30
31#undef fseeko
32#if !HAVE_FSEEKO
33# undef fseek
34# define fseeko fseek
35#endif
36
37int
38rpl_fseeko (FILE *fp, off_t offset, int whence)
39{
40#if LSEEK_PIPE_BROKEN
41  /* mingw gives bogus answers rather than failure on non-seekable files.  */
42  if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
43    return EOF;
44#endif
45
46  /* These tests are based on fpurge.c.  */
47#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
48  if (fp->_IO_read_end == fp->_IO_read_ptr
49      && fp->_IO_write_ptr == fp->_IO_write_base
50      && fp->_IO_save_base == NULL)
51#elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
52# if defined __SL64 && defined __SCLE /* Cygwin */
53  if ((fp->_flags & __SL64) == 0)
54    {
55      /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
56         mode; but has an fseeko that requires 64-bit mode.  */
57      FILE *tmp = fopen ("/dev/null", "r");
58      if (!tmp)
59        return -1;
60      fp->_flags |= __SL64;
61      fp->_seek64 = tmp->_seek64;
62      fclose (tmp);
63    }
64# endif
65  if (fp_->_p == fp_->_bf._base
66      && fp_->_r == 0
67      && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
68                     ? fp_->_bf._size
69                     : 0)
70      && fp_ub._base == NULL)
71#elif defined __EMX__               /* emx+gcc */
72  if (fp->_ptr == fp->_buffer
73      && fp->_rcount == 0
74      && fp->_wcount == 0
75      && fp->_ungetc_count == 0)
76#elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
77  if (fp_->_ptr == fp_->_base
78      && (fp_->_ptr == NULL || fp_->_cnt == 0))
79#elif defined __UCLIBC__            /* uClibc */
80  if (((fp->__modeflags & __FLAG_WRITING) == 0
81       || fp->__bufpos == fp->__bufstart)
82      && ((fp->__modeflags & (__FLAG_READONLY | __FLAG_READING)) == 0
83          || fp->__bufpos == fp->__bufread))
84#elif defined __QNX__               /* QNX */
85  if ((fp->_Mode & 0x2000 /* _MWRITE */ ? fp->_Next == fp->_Buf : fp->_Next == fp->_Rend)
86      && fp->_Rback == fp->_Back + sizeof (fp->_Back)
87      && fp->_Rsave == NULL)
88#elif defined __MINT__              /* Atari FreeMiNT */
89  if (fp->__bufp == fp->__buffer
90      && fp->__get_limit == fp->__bufp
91      && fp->__put_limit == fp->__bufp
92      && !fp->__pushed_back)
93#else
94  #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
95#endif
96    {
97      /* We get here when an fflush() call immediately preceded this one.  We
98         know there are no buffers.
99         POSIX requires us to modify the file descriptor's position.
100         But we cannot position beyond end of file here.  */
101      off_t pos =
102        lseek (fileno (fp),
103               whence == SEEK_END && offset > 0 ? 0 : offset,
104               whence);
105      if (pos == -1)
106        {
107#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
108          fp_->_flags &= ~__SOFF;
109#endif
110          return -1;
111        }
112
113#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
114      fp->_flags &= ~_IO_EOF_SEEN;
115#elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
116# if defined __CYGWIN__
117      /* fp_->_offset is typed as an integer.  */
118      fp_->_offset = pos;
119# else
120      /* fp_->_offset is an fpos_t.  */
121      {
122        /* Use a union, since on NetBSD, the compilation flags
123           determine whether fpos_t is typedef'd to off_t or a struct
124           containing a single off_t member.  */
125        union
126          {
127            fpos_t f;
128            off_t o;
129          } u;
130        u.o = pos;
131        fp_->_offset = u.f;
132      }
133# endif
134      fp_->_flags |= __SOFF;
135      fp_->_flags &= ~__SEOF;
136#elif defined __EMX__               /* emx+gcc */
137      fp->_flags &= ~_IOEOF;
138#elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
139      fp->_flag &= ~_IOEOF;
140#elif defined __MINT__              /* Atari FreeMiNT */
141      fp->__offset = pos;
142      fp->__eof = 0;
143#endif
144      /* If we were not requested to position beyond end of file, we're
145         done.  */
146      if (!(whence == SEEK_END && offset > 0))
147        return 0;
148    }
149  return fseeko (fp, offset, whence);
150}
151