2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 <<fseeko64>>---set file position for large file
29 int fseeko64(FILE *<[fp]>, _off64_t <[offset]>, int <[whence]>);
30 int _fseeko64_r (struct _reent *<[ptr]>, FILE *<[fp]>,
31 _off64_t <[offset]>, int <[whence]>);
34 Objects of type <<FILE>> can have a ``position'' that records how much
35 of the file your program has already read. Many of the <<stdio>> functions
36 depend on this position, and many change it as a side effect.
38 You can use <<fseeko64>> to set the position for the file identified by
39 <[fp]> that was opened via <<fopen64>>. The value of <[offset]> determines
40 the new position, in one of three ways selected by the value of <[whence]>
41 (defined as macros in `<<stdio.h>>'):
43 <<SEEK_SET>>---<[offset]> is the absolute file position (an offset
44 from the beginning of the file) desired. <[offset]> must be positive.
46 <<SEEK_CUR>>---<[offset]> is relative to the current file position.
47 <[offset]> can meaningfully be either positive or negative.
49 <<SEEK_END>>---<[offset]> is relative to the current end of file.
50 <[offset]> can meaningfully be either positive (to increase the size
51 of the file) or negative.
53 See <<ftello64>> to determine the current file position.
56 <<fseeko64>> returns <<0>> when successful. On failure, the
57 result is <<EOF>>. The reason for failure is indicated in <<errno>>:
58 either <<ESPIPE>> (the stream identified by <[fp]> doesn't support
59 repositioning or wasn't opened via <<fopen64>>) or <<EINVAL>>
60 (invalid file position).
63 <<fseeko64>> is a glibc extension.
65 Supporting OS subroutines required: <<close>>, <<fstat64>>, <<isatty>>,
66 <<lseek64>>, <<read>>, <<sbrk>>, <<write>>.
74 #include <sys/types.h>
78 #define POS_ERR (-(_fpos64_t)1)
80 #ifdef __LARGE64_FILES
83 * Seek the given file to the given offset.
84 * `Whence' must be one of the three SEEK_* macros.
88 _fseeko64_r (struct _reent
*ptr
,
93 _fpos64_t (*seekfn
) (struct _reent
*, void *, _fpos64_t
, int);
94 _fpos64_t target
, curoff
;
100 /* Only do 64-bit seek on large file. */
101 if (!(fp
->_flags
& __SL64
))
103 if ((_off_t
) offset
!= offset
)
105 _REENT_ERRNO(ptr
) = EOVERFLOW
;
108 return (_off64_t
) _fseeko_r (ptr
, fp
, offset
, whence
);
111 /* Make sure stdio is set up. */
113 CHECK_INIT (ptr
, fp
);
115 _newlib_flockfile_start (fp
);
117 curoff
= fp
->_offset
;
119 /* If we've been doing some writing, and we're in append mode
120 then we don't really know where the filepos is. */
122 if (fp
->_flags
& __SAPP
&& fp
->_flags
& __SWR
)
124 /* So flush the buffer and seek to the end. */
128 /* Have to be able to seek. */
130 if ((seekfn
= fp
->_seek64
) == NULL
)
132 _REENT_ERRNO(ptr
) = ESPIPE
; /* ??? */
133 _newlib_flockfile_exit(fp
);
138 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
139 * After this, whence is either SEEK_SET or SEEK_END.
146 * In order to seek relative to the current stream offset,
147 * we have to first find the current stream offset a la
148 * ftell (see ftell for details).
150 _fflush_r (ptr
, fp
); /* may adjust seek offset on append stream */
151 if (fp
->_flags
& __SOFF
)
152 curoff
= fp
->_offset
;
155 curoff
= seekfn (ptr
, fp
->_cookie
, (_fpos64_t
) 0, SEEK_CUR
);
158 _newlib_flockfile_exit(fp
);
162 if (fp
->_flags
& __SRD
)
168 else if (fp
->_flags
& __SWR
&& fp
->_p
!= NULL
)
169 curoff
+= fp
->_p
- fp
->_bf
._base
;
182 _REENT_ERRNO(ptr
) = EINVAL
;
183 _newlib_flockfile_exit(fp
);
188 * Can only optimise if:
189 * reading (and not reading-and-writing);
190 * not unbuffered; and
191 * this is a `regular' Unix file (and hence seekfn==__sseek).
192 * We must check __NBF first, because it is possible to have __NBF
193 * and __SOPT both set.
196 if (fp
->_bf
._base
== NULL
)
197 __smakebuf_r (ptr
, fp
);
199 #if _FSEEK_OPTIMIZATION
200 if (fp
->_flags
& (__SWR
| __SRW
| __SNBF
| __SNPT
))
202 if ((fp
->_flags
& __SOPT
) == 0)
204 if (seekfn
!= __sseek64
206 || _fstat64_r (ptr
, fp
->_file
, &st
)
207 || (st
.st_mode
& S_IFMT
) != S_IFREG
)
209 fp
->_flags
|= __SNPT
;
213 fp
->_blksize
= st
.st_blksize
;
217 fp
->_flags
|= __SOPT
;
221 * We are reading; we can try to optimise.
222 * Figure out where we are going and where we are now.
225 if (whence
== SEEK_SET
)
229 if (_fstat64_r (ptr
, fp
->_file
, &st
))
231 target
= st
.st_size
+ offset
;
236 if (fp
->_flags
& __SOFF
)
237 curoff
= fp
->_offset
;
240 curoff
= seekfn (ptr
, fp
->_cookie
, (_fpos64_t
)0, SEEK_CUR
);
241 if (curoff
== POS_ERR
)
250 * Compute the number of bytes in the input buffer (pretending
251 * that any ungetc() input has been discarded). Adjust current
252 * offset backwards by this count so that it represents the
253 * file offset for the first byte in the current input buffer.
258 curoff
+= fp
->_r
; /* kill off ungetc */
259 n
= fp
->_up
- fp
->_bf
._base
;
265 n
= fp
->_p
- fp
->_bf
._base
;
271 * If the target offset is within the current buffer,
272 * simply adjust the pointers, clear EOF, undo ungetc(),
276 if (target
>= curoff
&& target
< curoff
+ n
)
278 register int o
= target
- curoff
;
280 fp
->_p
= fp
->_bf
._base
+ o
;
284 fp
->_flags
&= ~__SEOF
;
285 _newlib_flockfile_exit(fp
);
290 * The place we want to get to is not within the current buffer,
291 * but we can still be kind to the kernel copyout mechanism.
292 * By aligning the file offset to a block boundary, we can let
293 * the kernel use the VM hardware to map pages instead of
294 * copying bytes laboriously. Using a block boundary also
295 * ensures that we only read one block, rather than two.
298 curoff
= target
& ~((_fpos64_t
)(fp
->_blksize
- 1));
299 if (seekfn (ptr
, fp
->_cookie
, curoff
, SEEK_SET
) == POS_ERR
)
302 fp
->_p
= fp
->_bf
._base
;
305 fp
->_flags
&= ~__SEOF
;
309 if (__srefill_r (ptr
, fp
) || fp
->_r
< n
)
314 _newlib_flockfile_end(fp
);
318 * We get here if we cannot optimise the seek ... just
319 * do it. Allow the seek function to change fp->_bf._base.
324 if (_fflush_r (ptr
, fp
)
325 || seekfn (ptr
, fp
->_cookie
, offset
, whence
) == POS_ERR
)
330 /* success: clear EOF indicator and discard ungetc() data */
333 fp
->_p
= fp
->_bf
._base
;
335 /* fp->_w = 0; *//* unnecessary (I think...) */
336 fp
->_flags
&= ~__SEOF
;
344 fseeko64 (register FILE *fp
,
348 return _fseeko64_r (_REENT
, fp
, offset
, whence
);
351 #endif /* !_REENT_ONLY */
353 #endif /* __LARGE64_FILES */