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