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