]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/LibC/Stdio/fgetstr.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / fgetstr.c
diff --git a/StdLib/LibC/Stdio/fgetstr.c b/StdLib/LibC/Stdio/fgetstr.c
new file mode 100644 (file)
index 0000000..7364d3b
--- /dev/null
@@ -0,0 +1,169 @@
+/*  $NetBSD: fgetstr.c,v 1.4 2006/11/24 19:46:58 christos 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  <LibConfig.h>\r
+#include <sys/EfiCdefs.h>\r
+#if defined(LIBC_SCCS) && !defined(lint)\r
+#if 0\r
+static char sccsid[] = "@(#)fgetline.c  8.1 (Berkeley) 6/4/93";\r
+#else\r
+__RCSID("$NetBSD: fgetstr.c,v 1.4 2006/11/24 19:46:58 christos Exp $");\r
+#endif\r
+#endif /* LIBC_SCCS and not lint */\r
+\r
+#include "namespace.h"\r
+\r
+#include <assert.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include "reentrant.h"\r
+#include "local.h"\r
+\r
+/*\r
+ * Expand the line buffer.  Return -1 on error.\r
+#ifdef notdef\r
+ * The `new size' does not account for a terminating '\0',\r
+ * so we add 1 here.\r
+#endif\r
+ */\r
+int\r
+__slbexpand(FILE *fp, size_t newsize)\r
+{\r
+  void *p;\r
+\r
+#ifdef notdef\r
+  ++newsize;\r
+#endif\r
+  _DIAGASSERT(fp != NULL);\r
+\r
+  if ((size_t)fp->_lb._size >= newsize)\r
+    return (0);\r
+  if ((p = realloc(fp->_lb._base, newsize)) == NULL)\r
+    return (-1);\r
+  fp->_lb._base = p;\r
+  fp->_lb._size = (int)newsize;\r
+  return (0);\r
+}\r
+\r
+/*\r
+ * Get an input line.  The returned pointer often (but not always)\r
+ * points into a stdio buffer.  Fgetline does not alter the text of\r
+ * the returned line (which is thus not a C string because it will\r
+ * not necessarily end with '\0'), but does allow callers to modify\r
+ * it if they wish.  Thus, we set __SMOD in case the caller does.\r
+ */\r
+char *\r
+__fgetstr(FILE *fp, size_t *lenp, int sep)\r
+{\r
+  unsigned char *p;\r
+  size_t len;\r
+  size_t off;\r
+\r
+  _DIAGASSERT(fp != NULL);\r
+  _DIAGASSERT(lenp != NULL);\r
+\r
+  /* make sure there is input */\r
+  if (fp->_r <= 0 && __srefill(fp)) {\r
+    *lenp = 0;\r
+    return (NULL);\r
+  }\r
+\r
+  /* look for a newline in the input */\r
+  if ((p = memchr((void *)fp->_p, sep, (size_t)fp->_r)) != NULL) {\r
+    char *ret;\r
+\r
+    /*\r
+     * Found one.  Flag buffer as modified to keep fseek from\r
+     * `optimising' a backward seek, in case the user stomps on\r
+     * the text.\r
+     */\r
+    p++;    /* advance over it */\r
+    ret = (char *)fp->_p;\r
+    *lenp = len = p - fp->_p;\r
+    fp->_flags |= __SMOD;\r
+    fp->_r -= (int)len;\r
+    fp->_p = p;\r
+    return (ret);\r
+  }\r
+\r
+  /*\r
+   * We have to copy the current buffered data to the line buffer.\r
+   * As a bonus, though, we can leave off the __SMOD.\r
+   *\r
+   * OPTIMISTIC is length that we (optimistically) expect will\r
+   * accommodate the `rest' of the string, on each trip through the\r
+   * loop below.\r
+   */\r
+#define OPTIMISTIC 80\r
+\r
+  for (len = fp->_r, off = 0;; len += fp->_r) {\r
+    size_t diff;\r
+\r
+    /*\r
+     * Make sure there is room for more bytes.  Copy data from\r
+     * file buffer to line buffer, refill file and look for\r
+     * newline.  The loop stops only when we find a newline.\r
+     */\r
+    if (__slbexpand(fp, len + OPTIMISTIC))\r
+      goto error;\r
+    (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,\r
+        len - off);\r
+    off = len;\r
+    if (__srefill(fp))\r
+      break;  /* EOF or error: return partial line */\r
+    if ((p = memchr((void *)fp->_p, sep, (size_t)fp->_r)) == NULL)\r
+      continue;\r
+\r
+    /* got it: finish up the line (like code above) */\r
+    p++;\r
+    diff = p - fp->_p;\r
+    len += diff;\r
+    if (__slbexpand(fp, len))\r
+      goto error;\r
+    (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,\r
+        diff);\r
+    fp->_r -= (int)diff;\r
+    fp->_p = p;\r
+    break;\r
+  }\r
+  *lenp = len;\r
+#ifdef notdef\r
+  fp->_lb._base[len] = 0;\r
+#endif\r
+  return ((char *)fp->_lb._base);\r
+\r
+error:\r
+  *lenp = 0;    /* ??? */\r
+  return (NULL);    /* ??? */\r
+}\r