Deleted Added
full compact
vfscanf.c (234587) vfscanf.c (234799)
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (c) 2011 The FreeBSD Foundation
6 * All rights reserved.
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.

--- 25 unchanged lines hidden (view full) ---

34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#if defined(LIBC_SCCS) && !defined(lint)
39static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
40#endif /* LIBC_SCCS and not lint */
41#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (c) 2011 The FreeBSD Foundation
6 * All rights reserved.
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.

--- 25 unchanged lines hidden (view full) ---

34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#if defined(LIBC_SCCS) && !defined(lint)
39static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
40#endif /* LIBC_SCCS and not lint */
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/lib/libc/stdio/vfscanf.c 234587 2012-04-22 21:28:14Z das $");
42__FBSDID("$FreeBSD: head/lib/libc/stdio/vfscanf.c 234799 2012-04-29 16:28:39Z das $");
43
44#include "namespace.h"
45#include <ctype.h>
46#include <inttypes.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <stddef.h>
50#include <stdarg.h>

--- 71 unchanged lines hidden (view full) ---

122 * The following conversion functions return the number of characters consumed,
123 * or -1 on input failure. Character class conversion returns 0 on match
124 * failure.
125 */
126
127static __inline int
128convert_char(FILE *fp, char * __restrict p, int width)
129{
43
44#include "namespace.h"
45#include <ctype.h>
46#include <inttypes.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <stddef.h>
50#include <stdarg.h>

--- 71 unchanged lines hidden (view full) ---

122 * The following conversion functions return the number of characters consumed,
123 * or -1 on input failure. Character class conversion returns 0 on match
124 * failure.
125 */
126
127static __inline int
128convert_char(FILE *fp, char * __restrict p, int width)
129{
130 int n, nread;
130 int n;
131
131
132 nread = 0;
133 if (p == SUPPRESS_PTR) {
134 size_t sum = 0;
135 for (;;) {
136 if ((n = fp->_r) < width) {
137 sum += n;
138 width -= n;
139 fp->_p += n;
140 if (__srefill(fp)) {
141 if (sum == 0)
142 return (-1);
143 break;
144 }
145 } else {
146 sum += width;
147 fp->_r -= width;
148 fp->_p += width;
149 break;
150 }
151 }
132 if (p == SUPPRESS_PTR) {
133 size_t sum = 0;
134 for (;;) {
135 if ((n = fp->_r) < width) {
136 sum += n;
137 width -= n;
138 fp->_p += n;
139 if (__srefill(fp)) {
140 if (sum == 0)
141 return (-1);
142 break;
143 }
144 } else {
145 sum += width;
146 fp->_r -= width;
147 fp->_p += width;
148 break;
149 }
150 }
152 nread += sum;
151 return (sum);
153 } else {
154 size_t r = __fread(p, 1, width, fp);
155
156 if (r == 0)
157 return (-1);
152 } else {
153 size_t r = __fread(p, 1, width, fp);
154
155 if (r == 0)
156 return (-1);
158 nread += r;
157 return (r);
159 }
158 }
160 return (nread);
161}
162
163static __inline int
159}
160
161static __inline int
164convert_wchar(FILE *fp, wchar_t *wcp, int width)
162convert_wchar(FILE *fp, wchar_t *wcp, int width, locale_t locale)
165{
166 mbstate_t mbs;
163{
164 mbstate_t mbs;
167 size_t nconv;
168 int n, nread;
165 int n, nread;
169 char buf[MB_CUR_MAX];
166 wint_t wi;
170
167
171 nread = 0;
168 mbs = initial_mbs;
172 n = 0;
169 n = 0;
173 while (width != 0) {
174 if (n == MB_CUR_MAX) {
175 fp->_flags |= __SERR;
176 return (-1);
177 }
178 buf[n++] = *fp->_p;
179 fp->_p++;
180 fp->_r--;
181 mbs = initial_mbs;
182 nconv = mbrtowc(wcp, buf, n, &mbs);
183 if (nconv == (size_t)-1) {
184 fp->_flags |= __SERR;
185 return (-1);
186 }
187 if (nconv == 0 && wcp != SUPPRESS_PTR)
188 *wcp = L'\0';
189 if (nconv != (size_t)-2) {
190 nread += n;
191 width--;
192 if (wcp != SUPPRESS_PTR)
193 wcp++;
194 n = 0;
195 }
196 if (fp->_r <= 0 && __srefill(fp)) {
197 if (n != 0) {
198 fp->_flags |= __SERR;
199 return (-1);
200 }
201 break;
202 }
170 while (width-- != 0 &&
171 (wi = __fgetwc_mbs(fp, &mbs, &nread, locale)) != WEOF) {
172 if (wcp != SUPPRESS_PTR)
173 *wcp++ = (wchar_t)wi;
174 n += nread;
203 }
175 }
204 return (nread);
176 if (n == 0)
177 return (-1);
178 return (n);
205}
206
207static __inline int
208convert_ccl(FILE *fp, char * __restrict p, int width, const char *ccltab)
209{
210 char *p0;
211 int n;
212

--- 26 unchanged lines hidden (view full) ---

239 if (n == 0)
240 return (0);
241 *p = 0;
242 }
243 return (n);
244}
245
246static __inline int
179}
180
181static __inline int
182convert_ccl(FILE *fp, char * __restrict p, int width, const char *ccltab)
183{
184 char *p0;
185 int n;
186

--- 26 unchanged lines hidden (view full) ---

213 if (n == 0)
214 return (0);
215 *p = 0;
216 }
217 return (n);
218}
219
220static __inline int
247convert_wccl(FILE *fp, wchar_t *wcp, int width, const char *ccltab)
221convert_wccl(FILE *fp, wchar_t *wcp, int width, const char *ccltab,
222 locale_t locale)
248{
249 mbstate_t mbs;
223{
224 mbstate_t mbs;
250 wchar_t twc;
251 int n, nchars, nconv;
252 char buf[MB_CUR_MAX];
225 wint_t wi;
226 int n, nread;
253
227
254 if (wcp == SUPPRESS_PTR)
255 wcp = &twc;
228 mbs = initial_mbs;
256 n = 0;
229 n = 0;
257 nchars = 0;
258 while (width != 0) {
259 if (n == MB_CUR_MAX) {
260 fp->_flags |= __SERR;
261 return (-1);
230 if (wcp == SUPPRESS_PTR) {
231 while ((wi = __fgetwc_mbs(fp, &mbs, &nread, locale)) != WEOF &&
232 width-- != 0 && ccltab[wctob(wi)])
233 n += nread;
234 if (wi != WEOF)
235 __ungetwc(wi, fp, __get_locale());
236 } else {
237 while ((wi = __fgetwc_mbs(fp, &mbs, &nread, locale)) != WEOF &&
238 width-- != 0 && ccltab[wctob(wi)]) {
239 *wcp++ = (wchar_t)wi;
240 n += nread;
262 }
241 }
263 buf[n++] = *fp->_p;
264 fp->_p++;
265 fp->_r--;
266 mbs = initial_mbs;
267 nconv = mbrtowc(wcp, buf, n, &mbs);
268 if (nconv == (size_t)-1) {
269 fp->_flags |= __SERR;
270 return (-1);
271 }
272 if (nconv == 0)
273 *wcp = L'\0';
274 if (nconv != (size_t)-2) {
275 if (wctob(*wcp) != EOF && !ccltab[wctob(*wcp)]) {
276 while (n != 0) {
277 n--;
278 __ungetc(buf[n], fp);
279 }
280 break;
281 }
282 width--;
283 if (wcp != &twc)
284 wcp++;
285 nchars++;
286 n = 0;
287 }
288 if (fp->_r <= 0 && __srefill(fp)) {
289 if (n != 0) {
290 fp->_flags |= __SERR;
291 return (-1);
292 }
293 break;
294 }
242 if (wi != WEOF)
243 __ungetwc(wi, fp, __get_locale());
244 if (n == 0)
245 return (0);
246 *wcp = 0;
295 }
247 }
296 if (n != 0) {
297 fp->_flags |= __SERR;
298 return (-1);
299 }
300 if (nchars == 0)
301 return (0);
302 *wcp = L'\0';
303 return (nchars);
248 return (n);
304}
305
306static __inline int
307convert_string(FILE *fp, char * __restrict p, int width)
308{
309 char *p0;
310 int n;
311

--- 18 unchanged lines hidden (view full) ---

330 }
331 *p = 0;
332 n = p - p0;
333 }
334 return (n);
335}
336
337static __inline int
249}
250
251static __inline int
252convert_string(FILE *fp, char * __restrict p, int width)
253{
254 char *p0;
255 int n;
256

--- 18 unchanged lines hidden (view full) ---

275 }
276 *p = 0;
277 n = p - p0;
278 }
279 return (n);
280}
281
282static __inline int
338convert_wstring(FILE *fp, wchar_t *wcp, int width)
283convert_wstring(FILE *fp, wchar_t *wcp, int width, locale_t locale)
339{
340 mbstate_t mbs;
284{
285 mbstate_t mbs;
341 wchar_t twc;
342 int n, nconv, nread;
343 char buf[MB_CUR_MAX];
286 wint_t wi;
287 int n, nread;
344
288
345 if (wcp == SUPPRESS_PTR)
346 wcp = &twc;
347 n = nread = 0;
348 while (!isspace(*fp->_p) && width != 0) {
349 if (n == MB_CUR_MAX) {
350 fp->_flags |= __SERR;
351 return (-1);
289 mbs = initial_mbs;
290 n = 0;
291 if (wcp == SUPPRESS_PTR) {
292 while ((wi = __fgetwc_mbs(fp, &mbs, &nread, locale)) != WEOF &&
293 width-- != 0 && !iswspace(wi))
294 n += nread;
295 if (wi != WEOF)
296 __ungetwc(wi, fp, __get_locale());
297 } else {
298 while ((wi = __fgetwc_mbs(fp, &mbs, &nread, locale)) != WEOF &&
299 width-- != 0 && !iswspace(wi)) {
300 *wcp++ = (wchar_t)wi;
301 n += nread;
352 }
302 }
353 buf[n++] = *fp->_p;
354 fp->_p++;
355 fp->_r--;
356 mbs = initial_mbs;
357 nconv = mbrtowc(wcp, buf, n, &mbs);
358 if (nconv == (size_t)-1) {
359 fp->_flags |= __SERR;
360 return (-1);
361 }
362 if (nconv == 0)
363 *wcp = L'\0';
364 if (nconv != (size_t)-2) {
365 if (iswspace(*wcp)) {
366 while (n != 0) {
367 n--;
368 __ungetc(buf[n], fp);
369 }
370 break;
371 }
372 nread += n;
373 width--;
374 if (wcp != &twc)
375 wcp++;
376 n = 0;
377 }
378 if (fp->_r <= 0 && __srefill(fp)) {
379 if (n != 0) {
380 fp->_flags |= __SERR;
381 return (-1);
382 }
383 break;
384 }
303 if (wi != WEOF)
304 __ungetwc(wi, fp, __get_locale());
305 *wcp = '\0';
385 }
306 }
386 *wcp = L'\0';
387 return (nread);
307 return (n);
388}
389
390/*
391 * Read an integer, storing it in buf. The only relevant bit in the
392 * flags argument is PFXOK.
393 *
394 * Return 0 on a match failure, and the number of characters read
395 * otherwise.

--- 365 unchanged lines hidden (view full) ---

761 switch (c) {
762
763 case CT_CHAR:
764 /* scan arbitrary characters (sets NOSKIP) */
765 if (width == 0)
766 width = 1;
767 if (flags & LONG) {
768 nr = convert_wchar(fp, GETARG(wchar_t *),
308}
309
310/*
311 * Read an integer, storing it in buf. The only relevant bit in the
312 * flags argument is PFXOK.
313 *
314 * Return 0 on a match failure, and the number of characters read
315 * otherwise.

--- 365 unchanged lines hidden (view full) ---

681 switch (c) {
682
683 case CT_CHAR:
684 /* scan arbitrary characters (sets NOSKIP) */
685 if (width == 0)
686 width = 1;
687 if (flags & LONG) {
688 nr = convert_wchar(fp, GETARG(wchar_t *),
769 width);
689 width, locale);
770 } else {
771 nr = convert_char(fp, GETARG(char *), width);
772 }
773 if (nr < 0)
774 goto input_failure;
775 break;
776
777 case CT_CCL:
778 /* scan a (nonempty) character class (sets NOSKIP) */
779 if (width == 0)
780 width = (size_t)~0; /* `infinity' */
781 if (flags & LONG) {
782 nr = convert_wccl(fp, GETARG(wchar_t *), width,
690 } else {
691 nr = convert_char(fp, GETARG(char *), width);
692 }
693 if (nr < 0)
694 goto input_failure;
695 break;
696
697 case CT_CCL:
698 /* scan a (nonempty) character class (sets NOSKIP) */
699 if (width == 0)
700 width = (size_t)~0; /* `infinity' */
701 if (flags & LONG) {
702 nr = convert_wccl(fp, GETARG(wchar_t *), width,
783 ccltab);
703 ccltab, locale);
784 } else {
785 nr = convert_ccl(fp, GETARG(char *), width,
786 ccltab);
787 }
788 if (nr <= 0) {
789 if (nr < 0)
790 goto input_failure;
791 else /* nr == 0 */
792 goto match_failure;
793 }
794 break;
795
796 case CT_STRING:
797 /* like CCL, but zero-length string OK, & no NOSKIP */
798 if (width == 0)
799 width = (size_t)~0;
800 if (flags & LONG) {
801 nr = convert_wstring(fp, GETARG(wchar_t *),
704 } else {
705 nr = convert_ccl(fp, GETARG(char *), width,
706 ccltab);
707 }
708 if (nr <= 0) {
709 if (nr < 0)
710 goto input_failure;
711 else /* nr == 0 */
712 goto match_failure;
713 }
714 break;
715
716 case CT_STRING:
717 /* like CCL, but zero-length string OK, & no NOSKIP */
718 if (width == 0)
719 width = (size_t)~0;
720 if (flags & LONG) {
721 nr = convert_wstring(fp, GETARG(wchar_t *),
802 width);
722 width, locale);
803 } else {
804 nr = convert_string(fp, GETARG(char *), width);
805 }
806 if (nr < 0)
807 goto input_failure;
808 break;
809
810 case CT_INT:

--- 364 unchanged lines hidden ---
723 } else {
724 nr = convert_string(fp, GETARG(char *), width);
725 }
726 if (nr < 0)
727 goto input_failure;
728 break;
729
730 case CT_INT:

--- 364 unchanged lines hidden ---