Added better sanity checks in Diffie-Hellman key exchange.
[gnutls.git] / gl / fseeko.c
blob64c0640810a3c225879056b008125cf5e992019f
1 /* An fseeko() function that, together with fflush(), is POSIX compliant.
2 Copyright (C) 2007-2012 Free Software Foundation, Inc.
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 3, or (at your option)
7 any later version.
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.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <stdio.h>
22 /* Get off_t and lseek. */
23 #include <unistd.h>
25 #include "stdio-impl.h"
27 int
28 fseeko (FILE *fp, off_t offset, int whence)
29 #undef fseeko
30 #if !HAVE_FSEEKO
31 # undef fseek
32 # define fseeko fseek
33 #endif
35 #if LSEEK_PIPE_BROKEN
36 /* mingw gives bogus answers rather than failure on non-seekable files. */
37 if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
38 return EOF;
39 #endif
41 /* These tests are based on fpurge.c. */
42 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
43 if (fp->_IO_read_end == fp->_IO_read_ptr
44 && fp->_IO_write_ptr == fp->_IO_write_base
45 && fp->_IO_save_base == NULL)
46 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
47 # if defined __SL64 && defined __SCLE /* Cygwin */
48 if ((fp->_flags & __SL64) == 0)
50 /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
51 mode; but has an fseeko that requires 64-bit mode. */
52 FILE *tmp = fopen ("/dev/null", "r");
53 if (!tmp)
54 return -1;
55 fp->_flags |= __SL64;
56 fp->_seek64 = tmp->_seek64;
57 fclose (tmp);
59 # endif
60 if (fp_->_p == fp_->_bf._base
61 && fp_->_r == 0
62 && fp_->_w == ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
63 ? fp_->_bf._size
64 : 0)
65 && fp_ub._base == NULL)
66 #elif defined __EMX__ /* emx+gcc */
67 if (fp->_ptr == fp->_buffer
68 && fp->_rcount == 0
69 && fp->_wcount == 0
70 && fp->_ungetc_count == 0)
71 #elif defined __minix /* Minix */
72 if (fp_->_ptr == fp_->_buf
73 && (fp_->_ptr == NULL || fp_->_count == 0))
74 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
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 #elif defined EPLAN9 /* Plan9 */
92 if (fp->rp == fp->buf
93 && fp->wp == fp->buf)
94 #else
95 #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
96 #endif
98 /* We get here when an fflush() call immediately preceded this one (or
99 if ftell() has created buffers but no I/O has occurred on a
100 newly-opened stream). We know there are no buffers. */
101 off_t pos = lseek (fileno (fp), offset, whence);
102 if (pos == -1)
104 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
105 fp_->_flags &= ~__SOFF;
106 #endif
107 return -1;
110 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
111 fp->_flags &= ~_IO_EOF_SEEN;
112 fp->_offset = pos;
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. */
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
125 fpos_t f;
126 off_t o;
127 } u;
128 u.o = pos;
129 fp_->_offset = u.f;
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, NonStop Kernel */
137 fp->_flag &= ~_IOEOF;
138 #elif defined __MINT__ /* Atari FreeMiNT */
139 fp->__offset = pos;
140 fp->__eof = 0;
141 #endif
142 return 0;
144 return fseeko (fp, offset, whence);