--- /dev/null
+/* $NetBSD: fseeko.c,v 1.5 2005/03/04 16:04:58 dsl Exp $ */\r
+\r
+/*-\r
+ * Copyright (c) 1990, 1993\r
+ * The Regents of the University of California. All rights reserved.\r
+ *\r
+ * This code is derived from software contributed to Berkeley by\r
+ * Chris Torek.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * 3. Neither the name of the University nor the names of its contributors\r
+ * may be used to endorse or promote products derived from this software\r
+ * without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
+ * SUCH DAMAGE.\r
+ */\r
+//#include <Uefi.h> // REMOVE, For DEBUG only\r
+//#include <Library/UefiLib.h> // REMOVE, For DEBUG only\r
+\r
+#include <LibConfig.h>\r
+#include <sys/EfiCdefs.h>\r
+#if defined(LIBC_SCCS) && !defined(lint)\r
+__RCSID("$NetBSD: fseeko.c,v 1.5 2005/03/04 16:04:58 dsl Exp $");\r
+#endif /* LIBC_SCCS and not lint */\r
+\r
+#include "namespace.h"\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+\r
+#include <assert.h>\r
+#include <errno.h>\r
+#include <fcntl.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include "reentrant.h"\r
+#include "local.h"\r
+\r
+#ifdef __weak_alias\r
+__weak_alias(fseeko, _fseeko)\r
+#endif\r
+\r
+#define POS_ERR (-(fpos_t)1)\r
+\r
+/*\r
+ * Seek the given file to the given offset.\r
+ * `Whence' must be one of the three SEEK_* macros.\r
+ */\r
+int\r
+fseeko(FILE *fp, off_t offset, int whence)\r
+{\r
+ fpos_t (*seekfn)(void *, fpos_t, int);\r
+ fpos_t target, curoff;\r
+ size_t n;\r
+ struct stat st;\r
+ int havepos;\r
+\r
+ _DIAGASSERT(fp != NULL);\r
+\r
+#ifdef __GNUC__\r
+ /* This outrageous construct just to shut up a GCC warning. */\r
+ (void) &curoff;\r
+#endif\r
+\r
+ /* make sure stdio is set up */\r
+ if (!__sdidinit)\r
+ __sinit();\r
+\r
+//Print(L"%a( %d, %Ld, %d)\n", __func__, fp->_file, offset, whence);\r
+ FLOCKFILE(fp);\r
+\r
+ /*\r
+ * Have to be able to seek.\r
+ */\r
+ if ((seekfn = fp->_seek) == NULL) {\r
+ errno = ESPIPE; /* historic practice */\r
+ FUNLOCKFILE(fp);\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ return (-1);\r
+ }\r
+\r
+ /*\r
+ * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.\r
+ * After this, whence is either SEEK_SET or SEEK_END.\r
+ */\r
+ switch (whence) {\r
+\r
+ case SEEK_CUR:\r
+ /*\r
+ * In order to seek relative to the current stream offset,\r
+ * we have to first find the current stream offset a la\r
+ * ftell (see ftell for details).\r
+ */\r
+ __sflush(fp); /* may adjust seek offset on append stream */\r
+ if (fp->_flags & __SOFF)\r
+ curoff = fp->_offset;\r
+ else {\r
+ curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);\r
+ if (curoff == POS_ERR) {\r
+ FUNLOCKFILE(fp);\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ return (-1);\r
+ }\r
+ }\r
+ if (fp->_flags & __SRD) {\r
+ curoff -= fp->_r;\r
+ if (HASUB(fp))\r
+ curoff -= fp->_ur;\r
+ } else if (fp->_flags & __SWR && fp->_p != NULL)\r
+ curoff += fp->_p - fp->_bf._base;\r
+\r
+ offset += curoff;\r
+ whence = SEEK_SET;\r
+ havepos = 1;\r
+ break;\r
+\r
+ case SEEK_SET:\r
+ case SEEK_END:\r
+ curoff = 0; /* XXX just to keep gcc quiet */\r
+ havepos = 0;\r
+ break;\r
+\r
+ default:\r
+ errno = EINVAL;\r
+ FUNLOCKFILE(fp);\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ return (-1);\r
+ }\r
+\r
+ /*\r
+ * Can only optimise if:\r
+ * reading (and not reading-and-writing);\r
+ * not unbuffered; and\r
+ * this is a `regular' Unix file (and hence seekfn==__sseek).\r
+ * We must check __NBF first, because it is possible to have __NBF\r
+ * and __SOPT both set.\r
+ */\r
+ if (fp->_bf._base == NULL)\r
+ __smakebuf(fp);\r
+ if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))\r
+ goto dumb;\r
+ if ((fp->_flags & __SOPT) == 0) {\r
+ if (seekfn != __sseek ||\r
+ fp->_file < 0 || fstat(fp->_file, &st) ||\r
+ !S_ISREG(st.st_mode)) {\r
+ fp->_flags |= __SNPT;\r
+ goto dumb;\r
+ }\r
+ fp->_blksize = st.st_blksize;\r
+ fp->_flags |= __SOPT;\r
+ }\r
+\r
+ /*\r
+ * We are reading; we can try to optimise.\r
+ * Figure out where we are going and where we are now.\r
+ */\r
+ if (whence == SEEK_SET)\r
+ target = offset;\r
+ else {\r
+ if (fstat(fp->_file, &st))\r
+ {\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ goto dumb;\r
+ }\r
+ target = st.st_size + offset;\r
+ }\r
+\r
+ if (!havepos) {\r
+ if (fp->_flags & __SOFF)\r
+ curoff = fp->_offset;\r
+ else {\r
+ curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);\r
+ if (curoff == POS_ERR)\r
+ {\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ goto dumb;\r
+ }\r
+ }\r
+ curoff -= fp->_r;\r
+ if (HASUB(fp))\r
+ curoff -= fp->_ur;\r
+ }\r
+\r
+ /*\r
+ * Compute the number of bytes in the input buffer (pretending\r
+ * that any ungetc() input has been discarded). Adjust current\r
+ * offset backwards by this count so that it represents the\r
+ * file offset for the first byte in the current input buffer.\r
+ */\r
+ if (HASUB(fp)) {\r
+ curoff += fp->_r; /* kill off ungetc */\r
+ n = fp->_up - fp->_bf._base;\r
+ curoff -= n;\r
+ n += fp->_ur;\r
+ } else {\r
+ n = fp->_p - fp->_bf._base;\r
+ curoff -= n;\r
+ n += fp->_r;\r
+ }\r
+\r
+ /*\r
+ * If the target offset is within the current buffer,\r
+ * simply adjust the pointers, clear EOF, undo ungetc(),\r
+ * and return. (If the buffer was modified, we have to\r
+ * skip this; see fgetln.c.)\r
+ */\r
+ if ((fp->_flags & __SMOD) == 0 &&\r
+ target >= curoff && target < (fpos_t)(curoff + n)) {\r
+ int o = (int)(target - curoff);\r
+\r
+ fp->_p = fp->_bf._base + o;\r
+ fp->_r = (int)(n - o);\r
+ if (HASUB(fp))\r
+ FREEUB(fp);\r
+ fp->_flags &= ~__SEOF;\r
+ FUNLOCKFILE(fp);\r
+ return (0);\r
+ }\r
+\r
+ /*\r
+ * The place we want to get to is not within the current buffer,\r
+ * but we can still be kind to the kernel copyout mechanism.\r
+ * By aligning the file offset to a block boundary, we can let\r
+ * the kernel use the VM hardware to map pages instead of\r
+ * copying bytes laboriously. Using a block boundary also\r
+ * ensures that we only read one block, rather than two.\r
+ */\r
+ curoff = target & ~(fp->_blksize - 1);\r
+ if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)\r
+ {\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ goto dumb;\r
+ }\r
+ fp->_r = 0;\r
+ fp->_p = fp->_bf._base;\r
+ if (HASUB(fp))\r
+ FREEUB(fp);\r
+ fp->_flags &= ~__SEOF;\r
+ n = (int)(target - curoff);\r
+ if (n) {\r
+ if (__srefill(fp) || fp->_r < (int)n)\r
+ {\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ goto dumb;\r
+ }\r
+ fp->_p += n;\r
+ fp->_r -= (int)n;\r
+ }\r
+ FUNLOCKFILE(fp);\r
+ return (0);\r
+\r
+ /*\r
+ * We get here if we cannot optimise the seek ... just\r
+ * do it. Allow the seek function to change fp->_bf._base.\r
+ */\r
+dumb:\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ if (__sflush(fp) ||\r
+ (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {\r
+ FUNLOCKFILE(fp);\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ return (-1);\r
+ }\r
+ /* success: clear EOF indicator and discard ungetc() data */\r
+ if (HASUB(fp))\r
+ FREEUB(fp);\r
+ fp->_p = fp->_bf._base;\r
+ fp->_r = 0;\r
+ /* fp->_w = 0; */ /* unnecessary (I think...) */\r
+ fp->_flags &= ~__SEOF;\r
+ FUNLOCKFILE(fp);\r
+//Print(L"%a: %d\n", __func__, __LINE__);\r
+ return (0);\r
+}\r