1 /* Copyright (C) 1991, 92, 93, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
23 /* Move the file position of STREAM to OFFSET
24 bytes from the beginning of the file if WHENCE
25 is SEEK_SET, the end of the file is it is SEEK_END,
26 or the current position if it is SEEK_CUR. */
28 fseek (stream
, offset
, whence
)
29 register FILE *stream
;
35 if (!__validfp (stream
))
41 /* Write out any pending data. */
42 if (stream
->__mode
.__write
&& __flshfp (stream
, EOF
) == EOF
)
45 /* Make sure we know the current offset info. */
46 stream
->__offset
= -1;
47 if (__stdio_check_offset (stream
) == EOF
)
50 /* We are moving the file position, so we are no longer at EOF. */
53 if (stream
->__pushed_back
)
55 /* Discard the character pushed back by ungetc. */
56 stream
->__bufp
= stream
->__pushback_bufp
;
57 stream
->__pushed_back
= 0;
60 /* Check the WHENCE argument for validity, and process OFFSET
61 into an absolute position in O. By the end of this switch,
62 either we have returned, or O contains an absolute position. */
71 /* We don't know where the end of the file is,
72 so seek to the position in the file the user asked
73 for, and then look where that is. */
74 if (stream
->__io_funcs
.__seek
== NULL
)
81 fpos_t pos
= (fpos_t) o
;
82 if ((*stream
->__io_funcs
.__seek
)
83 (stream
->__cookie
, &pos
, SEEK_END
) < 0)
86 stream
->__io_funcs
.__seek
= NULL
;
89 stream
->__offset
= pos
;
90 /* Make O be absolute, rather than
91 relative to the end of the file. */
95 /* Fall through to try an absolute seek. */
98 /* Make O be relative to the buffer. */
99 o
-= stream
->__target
;
100 /* Make O be relative to the current position in the buffer. */
101 o
-= stream
->__bufp
- stream
->__buffer
;
103 /* Fall through to see if we can do it by
104 moving the pointer around in the buffer. */
107 /* If the offset is small enough, we can just
108 move the pointer around in the buffer. */
110 #if 0 /* Why did I think this would ever work??? */
111 if (stream
->__put_limit
> stream
->__buffer
)
113 /* We are writing. */
114 if (stream
->__bufp
+ o
>= stream
->__buffer
&&
115 stream
->__put_limit
> stream
->__bufp
+ o
&&
116 stream
->__get_limit
> stream
->__bufp
+ o
)
118 /* We have read all the data we will change soon.
119 We can just move the pointer around. */
125 /* Flush the buffer. */
126 if (__flshfp(stream
, EOF
) == EOF
)
132 (-o
<= stream
->__bufp
- stream
->__buffer
) :
133 (o
<= stream
->__get_limit
- stream
->__bufp
))
139 /* Turn it into an absolute seek. */
140 o
+= stream
->__bufp
- stream
->__buffer
;
141 o
+= stream
->__target
;
147 /* Negative file position is meaningless. */
148 __set_errno (EINVAL
);
152 /* O is now an absolute position, the new target. */
153 stream
->__target
= o
;
155 /* Set bufp and both end pointers to the beginning of the buffer.
156 The next i/o will force a call to the input/output room function. */
158 = stream
->__get_limit
= stream
->__put_limit
= stream
->__buffer
;
160 /* Make sure __flshfp doesn't think the put_limit is at the beginning
161 of the buffer because of line-buffering magic. */
162 stream
->__linebuf_active
= 0;
164 /* If there is no seek function, seeks always fail. */
165 if (stream
->__io_funcs
.__seek
== NULL
)
167 /* This is preemptive, since we don't actually do the seeking.
168 But it makes more sense for fseek to to fail with ESPIPE
169 than for the next reading or writing operation to fail
171 __set_errno (ESPIPE
);
175 /* Don't actually seek. The next reading or writing operation
176 will force a call to the input or output room function,
177 which will move to the target file position before reading or writing. */