]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/fseeko.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / fseeko.c
1 /* $NetBSD: fseeko.c,v 1.5 2005/03/04 16:04:58 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 //#include <Uefi.h> // REMOVE, For DEBUG only
35 //#include <Library/UefiLib.h> // REMOVE, For DEBUG only
36
37 #include <LibConfig.h>
38 #include <sys/EfiCdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 __RCSID("$NetBSD: fseeko.c,v 1.5 2005/03/04 16:04:58 dsl Exp $");
41 #endif /* LIBC_SCCS and not lint */
42
43 #include "namespace.h"
44 #include <sys/types.h>
45 #include <sys/stat.h>
46
47 #include <assert.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include "reentrant.h"
53 #include "local.h"
54
55 #ifdef __weak_alias
56 __weak_alias(fseeko, _fseeko)
57 #endif
58
59 #define POS_ERR (-(fpos_t)1)
60
61 /*
62 * Seek the given file to the given offset.
63 * `Whence' must be one of the three SEEK_* macros.
64 */
65 int
66 fseeko(FILE *fp, off_t offset, int whence)
67 {
68 fpos_t (*seekfn)(void *, fpos_t, int);
69 fpos_t target, curoff;
70 size_t n;
71 struct stat st;
72 int havepos;
73
74 _DIAGASSERT(fp != NULL);
75
76 #ifdef __GNUC__
77 /* This outrageous construct just to shut up a GCC warning. */
78 (void) &curoff;
79 #endif
80
81 /* make sure stdio is set up */
82 if (!__sdidinit)
83 __sinit();
84
85 //Print(L"%a( %d, %Ld, %d)\n", __func__, fp->_file, offset, whence);
86 FLOCKFILE(fp);
87
88 /*
89 * Have to be able to seek.
90 */
91 if ((seekfn = fp->_seek) == NULL) {
92 errno = ESPIPE; /* historic practice */
93 FUNLOCKFILE(fp);
94 //Print(L"%a: %d\n", __func__, __LINE__);
95 return (-1);
96 }
97
98 /*
99 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
100 * After this, whence is either SEEK_SET or SEEK_END.
101 */
102 switch (whence) {
103
104 case SEEK_CUR:
105 /*
106 * In order to seek relative to the current stream offset,
107 * we have to first find the current stream offset a la
108 * ftell (see ftell for details).
109 */
110 __sflush(fp); /* may adjust seek offset on append stream */
111 if (fp->_flags & __SOFF)
112 curoff = fp->_offset;
113 else {
114 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
115 if (curoff == POS_ERR) {
116 FUNLOCKFILE(fp);
117 //Print(L"%a: %d\n", __func__, __LINE__);
118 return (-1);
119 }
120 }
121 if (fp->_flags & __SRD) {
122 curoff -= fp->_r;
123 if (HASUB(fp))
124 curoff -= fp->_ur;
125 } else if (fp->_flags & __SWR && fp->_p != NULL)
126 curoff += fp->_p - fp->_bf._base;
127
128 offset += curoff;
129 whence = SEEK_SET;
130 havepos = 1;
131 break;
132
133 case SEEK_SET:
134 case SEEK_END:
135 curoff = 0; /* XXX just to keep gcc quiet */
136 havepos = 0;
137 break;
138
139 default:
140 errno = EINVAL;
141 FUNLOCKFILE(fp);
142 //Print(L"%a: %d\n", __func__, __LINE__);
143 return (-1);
144 }
145
146 /*
147 * Can only optimise if:
148 * reading (and not reading-and-writing);
149 * not unbuffered; and
150 * this is a `regular' Unix file (and hence seekfn==__sseek).
151 * We must check __NBF first, because it is possible to have __NBF
152 * and __SOPT both set.
153 */
154 if (fp->_bf._base == NULL)
155 __smakebuf(fp);
156 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
157 goto dumb;
158 if ((fp->_flags & __SOPT) == 0) {
159 if (seekfn != __sseek ||
160 fp->_file < 0 || fstat(fp->_file, &st) ||
161 !S_ISREG(st.st_mode)) {
162 fp->_flags |= __SNPT;
163 goto dumb;
164 }
165 fp->_blksize = st.st_blksize;
166 fp->_flags |= __SOPT;
167 }
168
169 /*
170 * We are reading; we can try to optimise.
171 * Figure out where we are going and where we are now.
172 */
173 if (whence == SEEK_SET)
174 target = offset;
175 else {
176 if (fstat(fp->_file, &st))
177 {
178 //Print(L"%a: %d\n", __func__, __LINE__);
179 goto dumb;
180 }
181 target = st.st_size + offset;
182 }
183
184 if (!havepos) {
185 if (fp->_flags & __SOFF)
186 curoff = fp->_offset;
187 else {
188 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
189 if (curoff == POS_ERR)
190 {
191 //Print(L"%a: %d\n", __func__, __LINE__);
192 goto dumb;
193 }
194 }
195 curoff -= fp->_r;
196 if (HASUB(fp))
197 curoff -= fp->_ur;
198 }
199
200 /*
201 * Compute the number of bytes in the input buffer (pretending
202 * that any ungetc() input has been discarded). Adjust current
203 * offset backwards by this count so that it represents the
204 * file offset for the first byte in the current input buffer.
205 */
206 if (HASUB(fp)) {
207 curoff += fp->_r; /* kill off ungetc */
208 n = fp->_up - fp->_bf._base;
209 curoff -= n;
210 n += fp->_ur;
211 } else {
212 n = fp->_p - fp->_bf._base;
213 curoff -= n;
214 n += fp->_r;
215 }
216
217 /*
218 * If the target offset is within the current buffer,
219 * simply adjust the pointers, clear EOF, undo ungetc(),
220 * and return. (If the buffer was modified, we have to
221 * skip this; see fgetln.c.)
222 */
223 if ((fp->_flags & __SMOD) == 0 &&
224 target >= curoff && target < (fpos_t)(curoff + n)) {
225 int o = (int)(target - curoff);
226
227 fp->_p = fp->_bf._base + o;
228 fp->_r = (int)(n - o);
229 if (HASUB(fp))
230 FREEUB(fp);
231 fp->_flags &= ~__SEOF;
232 FUNLOCKFILE(fp);
233 return (0);
234 }
235
236 /*
237 * The place we want to get to is not within the current buffer,
238 * but we can still be kind to the kernel copyout mechanism.
239 * By aligning the file offset to a block boundary, we can let
240 * the kernel use the VM hardware to map pages instead of
241 * copying bytes laboriously. Using a block boundary also
242 * ensures that we only read one block, rather than two.
243 */
244 curoff = target & ~(fp->_blksize - 1);
245 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
246 {
247 //Print(L"%a: %d\n", __func__, __LINE__);
248 goto dumb;
249 }
250 fp->_r = 0;
251 fp->_p = fp->_bf._base;
252 if (HASUB(fp))
253 FREEUB(fp);
254 fp->_flags &= ~__SEOF;
255 n = (int)(target - curoff);
256 if (n) {
257 if (__srefill(fp) || fp->_r < (int)n)
258 {
259 //Print(L"%a: %d\n", __func__, __LINE__);
260 goto dumb;
261 }
262 fp->_p += n;
263 fp->_r -= (int)n;
264 }
265 FUNLOCKFILE(fp);
266 return (0);
267
268 /*
269 * We get here if we cannot optimise the seek ... just
270 * do it. Allow the seek function to change fp->_bf._base.
271 */
272 dumb:
273 //Print(L"%a: %d\n", __func__, __LINE__);
274 if (__sflush(fp) ||
275 (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
276 FUNLOCKFILE(fp);
277 //Print(L"%a: %d\n", __func__, __LINE__);
278 return (-1);
279 }
280 /* success: clear EOF indicator and discard ungetc() data */
281 if (HASUB(fp))
282 FREEUB(fp);
283 fp->_p = fp->_bf._base;
284 fp->_r = 0;
285 /* fp->_w = 0; */ /* unnecessary (I think...) */
286 fp->_flags &= ~__SEOF;
287 FUNLOCKFILE(fp);
288 //Print(L"%a: %d\n", __func__, __LINE__);
289 return (0);
290 }