blob: b16e3c711bc0885ad0214faabcae4de2ec03c72b [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $OpenBSD: vfscanf.c,v 1.21 2006/01/13 21:33:28 millert Exp $ */
2/*-
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <ctype.h>
35#include <inttypes.h>
36#include <stdarg.h>
37#include <stddef.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include "local.h"
41
42#ifdef FLOATING_POINT
43#include "floatio.h"
44#endif
45
46#define BUF 513 /* Maximum length of numeric string. */
47
48/*
49 * Flags used during conversion.
50 */
51#define LONG 0x00001 /* l: long or double */
52#define LONGDBL 0x00002 /* L: long double; unimplemented */
53#define SHORT 0x00004 /* h: short */
54#define SHORTSHORT 0x00008 /* hh: 8 bit integer */
55#define LLONG 0x00010 /* ll: long long (+ deprecated q: quad) */
56#define POINTER 0x00020 /* p: void * (as hex) */
57#define SIZEINT 0x00040 /* z: (signed) size_t */
58#define MAXINT 0x00080 /* j: intmax_t */
59#define PTRINT 0x00100 /* t: ptrdiff_t */
60#define NOSKIP 0x00200 /* [ or c: do not skip blanks */
61#define SUPPRESS 0x00400 /* *: suppress assignment */
62#define UNSIGNED 0x00800 /* %[oupxX] conversions */
63
64/*
65 * The following are used in numeric conversions only:
66 * SIGNOK, HAVESIGN, NDIGITS, DPTOK, and EXPOK are for floating point;
67 * SIGNOK, HAVESIGN, NDIGITS, PFXOK, and NZDIGITS are for integral.
68 */
69#define SIGNOK 0x01000 /* +/- is (still) legal */
70#define HAVESIGN 0x02000 /* sign detected */
71#define NDIGITS 0x04000 /* no digits detected */
72
73#define DPTOK 0x08000 /* (float) decimal point is still legal */
74#define EXPOK 0x10000 /* (float) exponent (e+3, etc) still legal */
75
76#define PFXOK 0x08000 /* 0x prefix is (still) legal */
77#define NZDIGITS 0x10000 /* no zero digits detected */
78
79/*
80 * Conversion types.
81 */
82#define CT_CHAR 0 /* %c conversion */
83#define CT_CCL 1 /* %[...] conversion */
84#define CT_STRING 2 /* %s conversion */
85#define CT_INT 3 /* integer, i.e., strtoimax or strtoumax */
86#define CT_FLOAT 4 /* floating, i.e., strtod */
87
88#define u_char unsigned char
89#define u_long unsigned long
90
91static u_char *__sccl(char *, u_char *);
92
93#if !defined(VFSCANF)
94#define VFSCANF vfscanf
95#endif
96
97/*
98 * vfscanf
99 */
100int
101VFSCANF(FILE *fp, const char *fmt0, __va_list ap)
102{
103 u_char *fmt = (u_char *)fmt0;
104 int c; /* character from format, or conversion */
105 size_t width; /* field width, or 0 */
106 char *p; /* points into all kinds of strings */
107 int n; /* handy integer */
108 int flags; /* flags as defined above */
109 char *p0; /* saves original value of p when necessary */
110 int nassigned; /* number of fields assigned */
111 int nread; /* number of characters consumed from fp */
112 int base; /* base argument to strtoimax/strtouimax */
113 char ccltab[256]; /* character class table for %[...] */
114 char buf[BUF]; /* buffer for numeric conversions */
115
116 /* `basefix' is used to avoid `if' tests in the integer scanner */
117 static short basefix[17] =
118 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
119
Kenny Rootf5823402011-02-12 07:13:44 -0800120 FLOCKFILE(fp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121 _SET_ORIENTATION(fp, -1);
122
123 nassigned = 0;
124 nread = 0;
125 base = 0; /* XXX just to keep gcc happy */
126 for (;;) {
127 c = *fmt++;
Kenny Rootf5823402011-02-12 07:13:44 -0800128 if (c == 0) {
129 FUNLOCKFILE(fp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130 return (nassigned);
Kenny Rootf5823402011-02-12 07:13:44 -0800131 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132 if (isspace(c)) {
133 while ((fp->_r > 0 || __srefill(fp) == 0) &&
134 isspace(*fp->_p))
135 nread++, fp->_r--, fp->_p++;
136 continue;
137 }
138 if (c != '%')
139 goto literal;
140 width = 0;
141 flags = 0;
142 /*
143 * switch on the format. continue if done;
144 * break once format type is derived.
145 */
146again: c = *fmt++;
147 switch (c) {
148 case '%':
149literal:
150 if (fp->_r <= 0 && __srefill(fp))
151 goto input_failure;
152 if (*fp->_p != c)
153 goto match_failure;
154 fp->_r--, fp->_p++;
155 nread++;
156 continue;
157
158 case '*':
159 flags |= SUPPRESS;
160 goto again;
161 case 'j':
162 flags |= MAXINT;
163 goto again;
164 case 'L':
165 flags |= LONGDBL;
166 goto again;
167 case 'h':
168 if (*fmt == 'h') {
169 fmt++;
170 flags |= SHORTSHORT;
171 } else {
172 flags |= SHORT;
173 }
174 goto again;
175 case 'l':
176 if (*fmt == 'l') {
177 fmt++;
178 flags |= LLONG;
179 } else {
180 flags |= LONG;
181 }
182 goto again;
183 case 'q':
184 flags |= LLONG; /* deprecated */
185 goto again;
186 case 't':
187 flags |= PTRINT;
188 goto again;
189 case 'z':
190 flags |= SIZEINT;
191 goto again;
192
193 case '0': case '1': case '2': case '3': case '4':
194 case '5': case '6': case '7': case '8': case '9':
195 width = width * 10 + c - '0';
196 goto again;
197
198 /*
199 * Conversions.
200 * Those marked `compat' are for 4.[123]BSD compatibility.
201 *
202 * (According to ANSI, E and X formats are supposed
203 * to the same as e and x. Sorry about that.)
204 */
205 case 'D': /* compat */
206 flags |= LONG;
207 /* FALLTHROUGH */
208 case 'd':
209 c = CT_INT;
210 base = 10;
211 break;
212
213 case 'i':
214 c = CT_INT;
215 base = 0;
216 break;
217
218 case 'O': /* compat */
219 flags |= LONG;
220 /* FALLTHROUGH */
221 case 'o':
222 c = CT_INT;
223 flags |= UNSIGNED;
224 base = 8;
225 break;
226
227 case 'u':
228 c = CT_INT;
229 flags |= UNSIGNED;
230 base = 10;
231 break;
232
233 case 'X':
234 case 'x':
235 flags |= PFXOK; /* enable 0x prefixing */
236 c = CT_INT;
237 flags |= UNSIGNED;
238 base = 16;
239 break;
240
241#ifdef FLOATING_POINT
242 case 'E':
243 case 'G':
244 case 'e':
245 case 'f':
246 case 'g':
247 c = CT_FLOAT;
248 break;
249#endif
250
251 case 's':
252 c = CT_STRING;
253 break;
254
255 case '[':
256 fmt = __sccl(ccltab, fmt);
257 flags |= NOSKIP;
258 c = CT_CCL;
259 break;
260
261 case 'c':
262 flags |= NOSKIP;
263 c = CT_CHAR;
264 break;
265
266 case 'p': /* pointer format is like hex */
267 flags |= POINTER | PFXOK;
268 c = CT_INT;
269 flags |= UNSIGNED;
270 base = 16;
271 break;
272
273 case 'n':
274 if (flags & SUPPRESS)
275 continue;
276 if (flags & SHORTSHORT)
277 *va_arg(ap, __signed char *) = nread;
278 else if (flags & SHORT)
279 *va_arg(ap, short *) = nread;
280 else if (flags & LONG)
281 *va_arg(ap, long *) = nread;
282 else if (flags & SIZEINT)
283 *va_arg(ap, ssize_t *) = nread;
284 else if (flags & PTRINT)
285 *va_arg(ap, ptrdiff_t *) = nread;
286 else if (flags & LLONG)
287 *va_arg(ap, long long *) = nread;
288 else if (flags & MAXINT)
289 *va_arg(ap, intmax_t *) = nread;
290 else
291 *va_arg(ap, int *) = nread;
292 continue;
293
294 /*
295 * Disgusting backwards compatibility hacks. XXX
296 */
297 case '\0': /* compat */
Kenny Rootf5823402011-02-12 07:13:44 -0800298 FUNLOCKFILE(fp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299 return (EOF);
300
301 default: /* compat */
302 if (isupper(c))
303 flags |= LONG;
304 c = CT_INT;
305 base = 10;
306 break;
307 }
308
309 /*
310 * We have a conversion that requires input.
311 */
312 if (fp->_r <= 0 && __srefill(fp))
313 goto input_failure;
314
315 /*
316 * Consume leading white space, except for formats
317 * that suppress this.
318 */
319 if ((flags & NOSKIP) == 0) {
320 while (isspace(*fp->_p)) {
321 nread++;
322 if (--fp->_r > 0)
323 fp->_p++;
324 else if (__srefill(fp))
325 goto input_failure;
326 }
327 /*
328 * Note that there is at least one character in
329 * the buffer, so conversions that do not set NOSKIP
330 * ca no longer result in an input failure.
331 */
332 }
333
334 /*
335 * Do the conversion.
336 */
337 switch (c) {
338
339 case CT_CHAR:
340 /* scan arbitrary characters (sets NOSKIP) */
341 if (width == 0)
342 width = 1;
343 if (flags & SUPPRESS) {
344 size_t sum = 0;
345 for (;;) {
346 if ((n = fp->_r) < (int)width) {
347 sum += n;
348 width -= n;
349 fp->_p += n;
350 if (__srefill(fp)) {
351 if (sum == 0)
352 goto input_failure;
353 break;
354 }
355 } else {
356 sum += width;
357 fp->_r -= width;
358 fp->_p += width;
359 break;
360 }
361 }
362 nread += sum;
363 } else {
364 size_t r = fread((void *)va_arg(ap, char *), 1,
365 width, fp);
366
367 if (r == 0)
368 goto input_failure;
369 nread += r;
370 nassigned++;
371 }
372 break;
373
374 case CT_CCL:
375 /* scan a (nonempty) character class (sets NOSKIP) */
376 if (width == 0)
377 width = (size_t)~0; /* `infinity' */
378 /* take only those things in the class */
379 if (flags & SUPPRESS) {
380 n = 0;
381 while (ccltab[*fp->_p]) {
382 n++, fp->_r--, fp->_p++;
383 if (--width == 0)
384 break;
385 if (fp->_r <= 0 && __srefill(fp)) {
386 if (n == 0)
387 goto input_failure;
388 break;
389 }
390 }
391 if (n == 0)
392 goto match_failure;
393 } else {
394 p0 = p = va_arg(ap, char *);
395 while (ccltab[*fp->_p]) {
396 fp->_r--;
397 *p++ = *fp->_p++;
398 if (--width == 0)
399 break;
400 if (fp->_r <= 0 && __srefill(fp)) {
401 if (p == p0)
402 goto input_failure;
403 break;
404 }
405 }
406 n = p - p0;
407 if (n == 0)
408 goto match_failure;
409 *p = '\0';
410 nassigned++;
411 }
412 nread += n;
413 break;
414
415 case CT_STRING:
416 /* like CCL, but zero-length string OK, & no NOSKIP */
417 if (width == 0)
418 width = (size_t)~0;
419 if (flags & SUPPRESS) {
420 n = 0;
421 while (!isspace(*fp->_p)) {
422 n++, fp->_r--, fp->_p++;
423 if (--width == 0)
424 break;
425 if (fp->_r <= 0 && __srefill(fp))
426 break;
427 }
428 nread += n;
429 } else {
430 p0 = p = va_arg(ap, char *);
431 while (!isspace(*fp->_p)) {
432 fp->_r--;
433 *p++ = *fp->_p++;
434 if (--width == 0)
435 break;
436 if (fp->_r <= 0 && __srefill(fp))
437 break;
438 }
439 *p = '\0';
440 nread += p - p0;
441 nassigned++;
442 }
443 continue;
444
445 case CT_INT:
446 /* scan an integer as if by strtoimax/strtoumax */
447#ifdef hardway
448 if (width == 0 || width > sizeof(buf) - 1)
449 width = sizeof(buf) - 1;
450#else
451 /* size_t is unsigned, hence this optimisation */
452 if (--width > sizeof(buf) - 2)
453 width = sizeof(buf) - 2;
454 width++;
455#endif
456 flags |= SIGNOK | NDIGITS | NZDIGITS;
457 for (p = buf; width; width--) {
458 c = *fp->_p;
459 /*
460 * Switch on the character; `goto ok'
461 * if we accept it as a part of number.
462 */
463 switch (c) {
464
465 /*
466 * The digit 0 is always legal, but is
467 * special. For %i conversions, if no
468 * digits (zero or nonzero) have been
469 * scanned (only signs), we will have
470 * base==0. In that case, we should set
471 * it to 8 and enable 0x prefixing.
472 * Also, if we have not scanned zero digits
473 * before this, do not turn off prefixing
474 * (someone else will turn it off if we
475 * have scanned any nonzero digits).
476 */
477 case '0':
478 if (base == 0) {
479 base = 8;
480 flags |= PFXOK;
481 }
482 if (flags & NZDIGITS)
483 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
484 else
485 flags &= ~(SIGNOK|PFXOK|NDIGITS);
486 goto ok;
487
488 /* 1 through 7 always legal */
489 case '1': case '2': case '3':
490 case '4': case '5': case '6': case '7':
491 base = basefix[base];
492 flags &= ~(SIGNOK | PFXOK | NDIGITS);
493 goto ok;
494
495 /* digits 8 and 9 ok iff decimal or hex */
496 case '8': case '9':
497 base = basefix[base];
498 if (base <= 8)
499 break; /* not legal here */
500 flags &= ~(SIGNOK | PFXOK | NDIGITS);
501 goto ok;
502
503 /* letters ok iff hex */
504 case 'A': case 'B': case 'C':
505 case 'D': case 'E': case 'F':
506 case 'a': case 'b': case 'c':
507 case 'd': case 'e': case 'f':
508 /* no need to fix base here */
509 if (base <= 10)
510 break; /* not legal here */
511 flags &= ~(SIGNOK | PFXOK | NDIGITS);
512 goto ok;
513
514 /* sign ok only as first character */
515 case '+': case '-':
516 if (flags & SIGNOK) {
517 flags &= ~SIGNOK;
518 flags |= HAVESIGN;
519 goto ok;
520 }
521 break;
522
523 /*
524 * x ok iff flag still set and 2nd char (or
525 * 3rd char if we have a sign).
526 */
527 case 'x': case 'X':
528 if ((flags & PFXOK) && p ==
529 buf + 1 + !!(flags & HAVESIGN)) {
530 base = 16; /* if %i */
531 flags &= ~PFXOK;
532 goto ok;
533 }
534 break;
535 }
536
537 /*
538 * If we got here, c is not a legal character
539 * for a number. Stop accumulating digits.
540 */
541 break;
542 ok:
543 /*
544 * c is legal: store it and look at the next.
545 */
546 *p++ = c;
547 if (--fp->_r > 0)
548 fp->_p++;
549 else if (__srefill(fp))
550 break; /* EOF */
551 }
552 /*
553 * If we had only a sign, it is no good; push
554 * back the sign. If the number ends in `x',
555 * it was [sign] '0' 'x', so push back the x
556 * and treat it as [sign] '0'.
557 */
558 if (flags & NDIGITS) {
559 if (p > buf)
560 (void) ungetc(*(u_char *)--p, fp);
561 goto match_failure;
562 }
563 c = ((u_char *)p)[-1];
564 if (c == 'x' || c == 'X') {
565 --p;
566 (void) ungetc(c, fp);
567 }
568 if ((flags & SUPPRESS) == 0) {
569 uintmax_t res;
570
571 *p = '\0';
572 if (flags & UNSIGNED)
573 res = strtoumax(buf, NULL, base);
574 else
575 res = strtoimax(buf, NULL, base);
576 if (flags & POINTER)
577 *va_arg(ap, void **) =
578 (void *)(uintptr_t)res;
579 else if (flags & MAXINT)
580 *va_arg(ap, intmax_t *) = res;
581 else if (flags & LLONG)
582 *va_arg(ap, long long *) = res;
583 else if (flags & SIZEINT)
584 *va_arg(ap, ssize_t *) = res;
585 else if (flags & PTRINT)
586 *va_arg(ap, ptrdiff_t *) = res;
587 else if (flags & LONG)
588 *va_arg(ap, long *) = res;
589 else if (flags & SHORT)
590 *va_arg(ap, short *) = res;
591 else if (flags & SHORTSHORT)
592 *va_arg(ap, __signed char *) = res;
593 else
594 *va_arg(ap, int *) = res;
595 nassigned++;
596 }
597 nread += p - buf;
598 break;
599
600#ifdef FLOATING_POINT
601 case CT_FLOAT:
602 /* scan a floating point number as if by strtod */
603#ifdef hardway
604 if (width == 0 || width > sizeof(buf) - 1)
605 width = sizeof(buf) - 1;
606#else
607 /* size_t is unsigned, hence this optimisation */
608 if (--width > sizeof(buf) - 2)
609 width = sizeof(buf) - 2;
610 width++;
611#endif
612 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
613 for (p = buf; width; width--) {
614 c = *fp->_p;
615 /*
616 * This code mimicks the integer conversion
617 * code, but is much simpler.
618 */
619 switch (c) {
620
621 case '0': case '1': case '2': case '3':
622 case '4': case '5': case '6': case '7':
623 case '8': case '9':
624 flags &= ~(SIGNOK | NDIGITS);
625 goto fok;
626
627 case '+': case '-':
628 if (flags & SIGNOK) {
629 flags &= ~SIGNOK;
630 goto fok;
631 }
632 break;
633 case '.':
634 if (flags & DPTOK) {
635 flags &= ~(SIGNOK | DPTOK);
636 goto fok;
637 }
638 break;
639 case 'e': case 'E':
640 /* no exponent without some digits */
641 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
642 flags =
643 (flags & ~(EXPOK|DPTOK)) |
644 SIGNOK | NDIGITS;
645 goto fok;
646 }
647 break;
648 }
649 break;
650 fok:
651 *p++ = c;
652 if (--fp->_r > 0)
653 fp->_p++;
654 else if (__srefill(fp))
655 break; /* EOF */
656 }
657 /*
658 * If no digits, might be missing exponent digits
659 * (just give back the exponent) or might be missing
660 * regular digits, but had sign and/or decimal point.
661 */
662 if (flags & NDIGITS) {
663 if (flags & EXPOK) {
664 /* no digits at all */
665 while (p > buf)
666 ungetc(*(u_char *)--p, fp);
667 goto match_failure;
668 }
669 /* just a bad exponent (e and maybe sign) */
670 c = *(u_char *)--p;
671 if (c != 'e' && c != 'E') {
672 (void) ungetc(c, fp);/* sign */
673 c = *(u_char *)--p;
674 }
675 (void) ungetc(c, fp);
676 }
677 if ((flags & SUPPRESS) == 0) {
678 double res;
679
680 *p = '\0';
681 res = strtod(buf, (char **) NULL);
682 if (flags & LONGDBL)
683 *va_arg(ap, long double *) = res;
684 else if (flags & LONG)
685 *va_arg(ap, double *) = res;
686 else
687 *va_arg(ap, float *) = res;
688 nassigned++;
689 }
690 nread += p - buf;
691 break;
692#endif /* FLOATING_POINT */
693 }
694 }
695input_failure:
Kenny Rootf5823402011-02-12 07:13:44 -0800696 if (nassigned == 0)
697 nassigned = -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698match_failure:
Kenny Rootf5823402011-02-12 07:13:44 -0800699 FUNLOCKFILE(fp);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700 return (nassigned);
701}
702
703/*
704 * Fill in the given table from the scanset at the given format
705 * (just after `['). Return a pointer to the character past the
706 * closing `]'. The table has a 1 wherever characters should be
707 * considered part of the scanset.
708 */
709static u_char *
710__sccl(char *tab, u_char *fmt)
711{
712 int c, n, v;
713
714 /* first `clear' the whole table */
715 c = *fmt++; /* first char hat => negated scanset */
716 if (c == '^') {
717 v = 1; /* default => accept */
718 c = *fmt++; /* get new first char */
719 } else
720 v = 0; /* default => reject */
721 /* should probably use memset here */
722 for (n = 0; n < 256; n++)
723 tab[n] = v;
724 if (c == 0)
725 return (fmt - 1);/* format ended before closing ] */
726
727 /*
728 * Now set the entries corresponding to the actual scanset
729 * to the opposite of the above.
730 *
731 * The first character may be ']' (or '-') without being special;
732 * the last character may be '-'.
733 */
734 v = 1 - v;
735 for (;;) {
736 tab[c] = v; /* take character c */
737doswitch:
738 n = *fmt++; /* and examine the next */
739 switch (n) {
740
741 case 0: /* format ended too soon */
742 return (fmt - 1);
743
744 case '-':
745 /*
746 * A scanset of the form
747 * [01+-]
748 * is defined as `the digit 0, the digit 1,
749 * the character +, the character -', but
750 * the effect of a scanset such as
751 * [a-zA-Z0-9]
752 * is implementation defined. The V7 Unix
753 * scanf treats `a-z' as `the letters a through
754 * z', but treats `a-a' as `the letter a, the
755 * character -, and the letter a'.
756 *
757 * For compatibility, the `-' is not considerd
758 * to define a range if the character following
759 * it is either a close bracket (required by ANSI)
760 * or is not numerically greater than the character
761 * we just stored in the table (c).
762 */
763 n = *fmt;
764 if (n == ']' || n < c) {
765 c = '-';
766 break; /* resume the for(;;) */
767 }
768 fmt++;
769 do { /* fill in the range */
770 tab[++c] = v;
771 } while (c < n);
772#if 1 /* XXX another disgusting compatibility hack */
773 /*
774 * Alas, the V7 Unix scanf also treats formats
775 * such as [a-c-e] as `the letters a through e'.
776 * This too is permitted by the standard....
777 */
778 goto doswitch;
779#else
780 c = *fmt++;
781 if (c == 0)
782 return (fmt - 1);
783 if (c == ']')
784 return (fmt);
785#endif
786 break;
787
788 case ']': /* end of scanset */
789 return (fmt);
790
791 default: /* just another character */
792 c = n;
793 break;
794 }
795 }
796 /* NOTREACHED */
797}