]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/fseeko.c
2 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available
4 under the terms and conditions of the BSD License that accompanies this
5 distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 * Copyright (c) 1990, 1993
12 * The Regents of the University of California. All rights reserved.
14 * This code is derived from software contributed to Berkeley by
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 NetBSD: fseeko.c,v 1.5 2005/03/04 16:04:58 dsl Exp
43 //#include <Uefi.h> // REMOVE, For DEBUG only
44 //#include <Library/UefiLib.h> // REMOVE, For DEBUG only
46 #include <LibConfig.h>
47 #include <sys/EfiCdefs.h>
49 #include "namespace.h"
50 #include <sys/types.h>
58 #include "reentrant.h"
62 __weak_alias(fseeko
, _fseeko
)
65 #define POS_ERR (-(fpos_t)1)
68 * Seek the given file to the given offset.
69 * `Whence' must be one of the three SEEK_* macros.
72 fseeko(FILE *fp
, off_t offset
, int whence
)
74 fpos_t (*seekfn
)(void *, fpos_t, int);
75 fpos_t target
, curoff
;
80 _DIAGASSERT(fp
!= NULL
);
87 /* This outrageous construct just to shut up a GCC warning. */
91 /* make sure stdio is set up */
95 //Print(L"%a( %d, %Ld, %d)\n", __func__, fp->_file, offset, whence);
99 * Have to be able to seek.
101 if ((seekfn
= fp
->_seek
) == NULL
) {
102 errno
= ESPIPE
; /* historic practice */
104 //Print(L"%a: %d\n", __func__, __LINE__);
109 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
110 * After this, whence is either SEEK_SET or SEEK_END.
116 * In order to seek relative to the current stream offset,
117 * we have to first find the current stream offset a la
118 * ftell (see ftell for details).
120 __sflush(fp
); /* may adjust seek offset on append stream */
121 if (fp
->_flags
& __SOFF
)
122 curoff
= fp
->_offset
;
124 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
125 if (curoff
== POS_ERR
) {
127 //Print(L"%a: %d\n", __func__, __LINE__);
131 if (fp
->_flags
& __SRD
) {
135 } else if (fp
->_flags
& __SWR
&& fp
->_p
!= NULL
)
136 curoff
+= fp
->_p
- fp
->_bf
._base
;
145 curoff
= 0; /* XXX just to keep gcc quiet */
152 //Print(L"%a: %d\n", __func__, __LINE__);
157 * Can only optimise if:
158 * reading (and not reading-and-writing);
159 * not unbuffered; and
160 * this is a `regular' Unix file (and hence seekfn==__sseek).
161 * We must check __NBF first, because it is possible to have __NBF
162 * and __SOPT both set.
164 if (fp
->_bf
._base
== NULL
)
166 if (fp
->_flags
& (__SWR
| __SRW
| __SNBF
| __SNPT
))
168 if ((fp
->_flags
& __SOPT
) == 0) {
169 if (seekfn
!= __sseek
||
170 fp
->_file
< 0 || fstat(fp
->_file
, &st
) ||
171 !S_ISREG(st
.st_mode
)) {
172 fp
->_flags
|= __SNPT
;
175 fp
->_blksize
= st
.st_blksize
;
176 fp
->_flags
|= __SOPT
;
180 * We are reading; we can try to optimise.
181 * Figure out where we are going and where we are now.
183 if (whence
== SEEK_SET
)
186 if (fstat(fp
->_file
, &st
))
188 //Print(L"%a: %d\n", __func__, __LINE__);
191 target
= st
.st_size
+ offset
;
195 if (fp
->_flags
& __SOFF
)
196 curoff
= fp
->_offset
;
198 curoff
= (*seekfn
)(fp
->_cookie
, (fpos_t)0, SEEK_CUR
);
199 if (curoff
== POS_ERR
)
201 //Print(L"%a: %d\n", __func__, __LINE__);
211 * Compute the number of bytes in the input buffer (pretending
212 * that any ungetc() input has been discarded). Adjust current
213 * offset backwards by this count so that it represents the
214 * file offset for the first byte in the current input buffer.
217 curoff
+= fp
->_r
; /* kill off ungetc */
218 n
= fp
->_up
- fp
->_bf
._base
;
222 n
= fp
->_p
- fp
->_bf
._base
;
228 * If the target offset is within the current buffer,
229 * simply adjust the pointers, clear EOF, undo ungetc(),
230 * and return. (If the buffer was modified, we have to
231 * skip this; see fgetln.c.)
233 if ((fp
->_flags
& __SMOD
) == 0 &&
234 target
>= curoff
&& target
< (fpos_t)(curoff
+ n
)) {
235 int o
= (int)(target
- curoff
);
237 fp
->_p
= fp
->_bf
._base
+ o
;
238 fp
->_r
= (int)(n
- o
);
241 WCIO_FREE(fp
); /* Should this really be unconditional??? */
242 fp
->_flags
&= ~__SEOF
;
248 * The place we want to get to is not within the current buffer,
249 * but we can still be kind to the kernel copyout mechanism.
250 * By aligning the file offset to a block boundary, we can let
251 * the kernel use the VM hardware to map pages instead of
252 * copying bytes laboriously. Using a block boundary also
253 * ensures that we only read one block, rather than two.
255 curoff
= target
& ~(fp
->_blksize
- 1);
256 if ((*seekfn
)(fp
->_cookie
, curoff
, SEEK_SET
) == POS_ERR
)
258 //Print(L"%a: %d\n", __func__, __LINE__);
262 fp
->_p
= fp
->_bf
._base
;
265 WCIO_FREE(fp
); /* Should this really be unconditional??? */
266 fp
->_flags
&= ~__SEOF
;
267 n
= (int)(target
- curoff
);
269 if (__srefill(fp
) || fp
->_r
< (int)n
)
271 //Print(L"%a: %d\n", __func__, __LINE__);
281 * We get here if we cannot optimise the seek ... just
282 * do it. Allow the seek function to change fp->_bf._base.
285 //Print(L"%a: %d\n", __func__, __LINE__);
287 (*seekfn
)(fp
->_cookie
, (fpos_t)offset
, whence
) == POS_ERR
) {
289 //Print(L"%a: %d\n", __func__, __LINE__);
292 /* success: clear EOF indicator and discard ungetc() data */
295 WCIO_FREE(fp
); /* Should this really be unconditional??? */
296 fp
->_p
= fp
->_bf
._base
;
299 fp
->_flags
&= ~__SEOF
;
301 //Print(L"%a: %d\n", __func__, __LINE__);