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