blob: 439308f7574ba7efbd03ea9626c9041f3b7a322c [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include "stdio_impl.h"
2
3int __fseeko_unlocked(FILE *f, off_t off, int whence)
4{
5 /* Adjust relative offset for unread data in buffer, if any. */
Rich Felker849e7602018-09-16 13:46:46 -04006 if (whence == SEEK_CUR && f->rend) off -= f->rend - f->rpos;
Rich Felker0b44a032011-02-12 00:22:29 -05007
Rich Felkere3cd6c52011-03-28 01:14:44 -04008 /* Flush write buffer, and report error on failure. */
Rich Felker849e7602018-09-16 13:46:46 -04009 if (f->wpos != f->wbase) {
Rich Felkere3cd6c52011-03-28 01:14:44 -040010 f->write(f, 0, 0);
11 if (!f->wpos) return -1;
12 }
Rich Felker0b44a032011-02-12 00:22:29 -050013
Rich Felkere3cd6c52011-03-28 01:14:44 -040014 /* Leave writing mode */
15 f->wpos = f->wbase = f->wend = 0;
16
17 /* Perform the underlying seek. */
Rich Felker9a909fc2011-04-02 13:54:55 -040018 if (f->seek(f, off, whence) < 0) return -1;
Rich Felker0b44a032011-02-12 00:22:29 -050019
20 /* If seek succeeded, file is seekable and we discard read buffer. */
Rich Felkere3cd6c52011-03-28 01:14:44 -040021 f->rpos = f->rend = 0;
Rich Felker0b44a032011-02-12 00:22:29 -050022 f->flags &= ~F_EOF;
23
Rich Felker0b44a032011-02-12 00:22:29 -050024 return 0;
25}
26
27int __fseeko(FILE *f, off_t off, int whence)
28{
29 int result;
30 FLOCK(f);
31 result = __fseeko_unlocked(f, off, whence);
32 FUNLOCK(f);
33 return result;
34}
35
36int fseek(FILE *f, long off, int whence)
37{
38 return __fseeko(f, off, whence);
39}
40
41weak_alias(__fseeko, fseeko);
42
Rich Felker63a4c9a2018-09-12 00:28:34 -040043weak_alias(fseeko, fseeko64);