]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/fgetstr.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / fgetstr.c
CommitLineData
2aa62f2b 1/* $NetBSD: fgetstr.c,v 1.4 2006/11/24 19:46:58 christos Exp $ */\r
2\r
3/*-\r
4 * Copyright (c) 1990, 1993\r
5 * The Regents of the University of California. All rights reserved.\r
6 *\r
7 * This code is derived from software contributed to Berkeley by\r
8 * Chris Torek.\r
9 *\r
10 * Redistribution and use in source and binary forms, with or without\r
11 * modification, are permitted provided that the following conditions\r
12 * are met:\r
13 * 1. Redistributions of source code must retain the above copyright\r
14 * notice, this list of conditions and the following disclaimer.\r
15 * 2. Redistributions in binary form must reproduce the above copyright\r
16 * notice, this list of conditions and the following disclaimer in the\r
17 * documentation and/or other materials provided with the distribution.\r
18 * 3. Neither the name of the University nor the names of its contributors\r
19 * may be used to endorse or promote products derived from this software\r
20 * without specific prior written permission.\r
21 *\r
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
32 * SUCH DAMAGE.\r
33 */\r
34#include <LibConfig.h>\r
35#include <sys/EfiCdefs.h>\r
36#if defined(LIBC_SCCS) && !defined(lint)\r
37#if 0\r
38static char sccsid[] = "@(#)fgetline.c 8.1 (Berkeley) 6/4/93";\r
39#else\r
40__RCSID("$NetBSD: fgetstr.c,v 1.4 2006/11/24 19:46:58 christos Exp $");\r
41#endif\r
42#endif /* LIBC_SCCS and not lint */\r
43\r
44#include "namespace.h"\r
45\r
46#include <assert.h>\r
47#include <stdio.h>\r
48#include <stdlib.h>\r
49#include <string.h>\r
50#include "reentrant.h"\r
51#include "local.h"\r
52\r
53/*\r
54 * Expand the line buffer. Return -1 on error.\r
55#ifdef notdef\r
56 * The `new size' does not account for a terminating '\0',\r
57 * so we add 1 here.\r
58#endif\r
59 */\r
60int\r
61__slbexpand(FILE *fp, size_t newsize)\r
62{\r
63 void *p;\r
64\r
65#ifdef notdef\r
66 ++newsize;\r
67#endif\r
68 _DIAGASSERT(fp != NULL);\r
69\r
70 if ((size_t)fp->_lb._size >= newsize)\r
71 return (0);\r
72 if ((p = realloc(fp->_lb._base, newsize)) == NULL)\r
73 return (-1);\r
74 fp->_lb._base = p;\r
75 fp->_lb._size = (int)newsize;\r
76 return (0);\r
77}\r
78\r
79/*\r
80 * Get an input line. The returned pointer often (but not always)\r
81 * points into a stdio buffer. Fgetline does not alter the text of\r
82 * the returned line (which is thus not a C string because it will\r
83 * not necessarily end with '\0'), but does allow callers to modify\r
84 * it if they wish. Thus, we set __SMOD in case the caller does.\r
85 */\r
86char *\r
87__fgetstr(FILE *fp, size_t *lenp, int sep)\r
88{\r
89 unsigned char *p;\r
90 size_t len;\r
91 size_t off;\r
92\r
93 _DIAGASSERT(fp != NULL);\r
94 _DIAGASSERT(lenp != NULL);\r
95\r
96 /* make sure there is input */\r
97 if (fp->_r <= 0 && __srefill(fp)) {\r
98 *lenp = 0;\r
99 return (NULL);\r
100 }\r
101\r
102 /* look for a newline in the input */\r
103 if ((p = memchr((void *)fp->_p, sep, (size_t)fp->_r)) != NULL) {\r
104 char *ret;\r
105\r
106 /*\r
107 * Found one. Flag buffer as modified to keep fseek from\r
108 * `optimising' a backward seek, in case the user stomps on\r
109 * the text.\r
110 */\r
111 p++; /* advance over it */\r
112 ret = (char *)fp->_p;\r
113 *lenp = len = p - fp->_p;\r
114 fp->_flags |= __SMOD;\r
115 fp->_r -= (int)len;\r
116 fp->_p = p;\r
117 return (ret);\r
118 }\r
119\r
120 /*\r
121 * We have to copy the current buffered data to the line buffer.\r
122 * As a bonus, though, we can leave off the __SMOD.\r
123 *\r
124 * OPTIMISTIC is length that we (optimistically) expect will\r
125 * accommodate the `rest' of the string, on each trip through the\r
126 * loop below.\r
127 */\r
128#define OPTIMISTIC 80\r
129\r
130 for (len = fp->_r, off = 0;; len += fp->_r) {\r
131 size_t diff;\r
132\r
133 /*\r
134 * Make sure there is room for more bytes. Copy data from\r
135 * file buffer to line buffer, refill file and look for\r
136 * newline. The loop stops only when we find a newline.\r
137 */\r
138 if (__slbexpand(fp, len + OPTIMISTIC))\r
139 goto error;\r
140 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,\r
141 len - off);\r
142 off = len;\r
143 if (__srefill(fp))\r
144 break; /* EOF or error: return partial line */\r
145 if ((p = memchr((void *)fp->_p, sep, (size_t)fp->_r)) == NULL)\r
146 continue;\r
147\r
148 /* got it: finish up the line (like code above) */\r
149 p++;\r
150 diff = p - fp->_p;\r
151 len += diff;\r
152 if (__slbexpand(fp, len))\r
153 goto error;\r
154 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,\r
155 diff);\r
156 fp->_r -= (int)diff;\r
157 fp->_p = p;\r
158 break;\r
159 }\r
160 *lenp = len;\r
161#ifdef notdef\r
162 fp->_lb._base[len] = 0;\r
163#endif\r
164 return ((char *)fp->_lb._base);\r
165\r
166error:\r
167 *lenp = 0; /* ??? */\r
168 return (NULL); /* ??? */\r
169}\r