blob: 3caf1ac798dd0fe25c41a20d901f4a11a02f785f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
2 *
3 * GNU Library General Public License (LGPL) version 2 or later.
4 *
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
7
8#include "_stdio.h"
9
10#if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
11# error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
12#endif
13
14#ifndef __DO_LARGEFILE
15# define FSEEK fseek
16# define OFFSET_TYPE long int
17#endif
18
19int FSEEK(register FILE *stream, OFFSET_TYPE offset, int whence)
20{
21#if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE)
22
23 return fseeko64(stream, offset, whence);
24
25#else
26
27 __offmax_t pos = offset;
28 int retval = -1;
29 __STDIO_AUTO_THREADLOCK_VAR;
30
31 if (((unsigned int) whence) > 2) {
32 __set_errno(EINVAL);
33 } else {
34 __STDIO_AUTO_THREADLOCK(stream);
35
36 __STDIO_STREAM_VALIDATE(stream);
37
38 if ((!__STDIO_STREAM_IS_WRITING(stream)
39 || !__STDIO_COMMIT_WRITE_BUFFER(stream))
40 && ((whence != SEEK_CUR)
41 || (__stdio_adjust_position(stream, &pos) >= 0))
42 && (__SEEK(stream, &pos, whence) >= 0)
43 ) {
44
45 /* Clear reading/writing modes, EOF, and ungots. */
46 stream->__modeflags &=
47 ~(__MASK_READING|__FLAG_WRITING|__FLAG_EOF);
48
49 /* Make sure all pointers are reset. */
50 __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
51 __STDIO_STREAM_DISABLE_GETC(stream);
52 __STDIO_STREAM_DISABLE_PUTC(stream);
53
54 /* We reinitialize the mbstate object. Doing so is
55 * implementation defined behavior. */
56#ifdef __STDIO_MBSTATE
57 __INIT_MBSTATE(&(stream->__state));
58#endif
59#ifdef __UCLIBC_HAS_WCHAR__
60 stream->__ungot_width[0] = 0;
61#endif
62
63 retval = 0;
64 }
65
66 __STDIO_STREAM_VALIDATE(stream);
67
68 __STDIO_AUTO_THREADUNLOCK(stream);
69 }
70
71 return retval;
72
73#endif
74}
75
76#ifdef __DO_LARGEFILE
77libc_hidden_def(fseeko64)
78#else
79libc_hidden_def(fseek)
80strong_alias(fseek,fseeko)
81#endif