OSDN Git Service

f63ebad4edb8fc60f843a89986428278df299ea7
[uclinux-h8/uClibc.git] / libc / stdio / fseeko.c
1 /* 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
19 #ifdef __UCLIBC_HAS_LFS__
20 libc_hidden_proto(fseeko64)
21 #endif
22 libc_hidden_proto(fseek)
23
24 int FSEEK(register FILE *stream, OFFSET_TYPE offset, int whence)
25 {
26 #if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE)
27
28         return fseeko64(stream, offset, whence);
29
30 #else
31
32         __offmax_t pos = offset;
33         int retval = -1;
34         __STDIO_AUTO_THREADLOCK_VAR;
35
36         if (((unsigned int) whence) > 2) {
37                 __set_errno(EINVAL);
38         } else {
39                 __STDIO_AUTO_THREADLOCK(stream);
40
41                 __STDIO_STREAM_VALIDATE(stream);
42
43                 if ((!__STDIO_STREAM_IS_WRITING(stream)
44                           || !__STDIO_COMMIT_WRITE_BUFFER(stream))
45                         && ((whence != SEEK_CUR) 
46                                  || (__stdio_adjust_position(stream, &pos) >= 0))
47                         && (__SEEK(stream, &pos, whence) >= 0)
48                         ) {
49
50                         /* Clear reading/writing modes, EOF, and ungots. */
51                         stream->__modeflags &=
52                                 ~(__MASK_READING|__FLAG_WRITING|__FLAG_EOF);
53
54                         /* Make sure all pointers are reset. */
55                         __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
56                         __STDIO_STREAM_DISABLE_GETC(stream);
57                         __STDIO_STREAM_DISABLE_PUTC(stream);
58
59                         /* We reinitialize the mbstate object.  Doing so is
60                          * implementation defined behavior. */
61 #ifdef __STDIO_MBSTATE
62                         __INIT_MBSTATE(&(stream->__state));
63 #endif
64 #ifdef __UCLIBC_HAS_WCHAR__
65                         stream->__ungot_width[0] = 0;
66 #endif
67
68                         retval = 0;
69                 }
70
71                 __STDIO_STREAM_VALIDATE(stream);
72
73                 __STDIO_AUTO_THREADUNLOCK(stream);
74         }
75
76         return retval;
77
78 #endif
79 }
80
81 #ifdef __DO_LARGEFILE
82 libc_hidden_def(fseeko64)
83 #else
84 libc_hidden_def(fseek)
85 strong_alias(fseek,fseeko)
86 #endif