Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 1 | /* $OpenBSD: vfscanf.c,v 1.31 2014/03/19 05:17:01 guenther Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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> |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 35 | #include <wctype.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | #include <inttypes.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <stddef.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 41 | #include <string.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | #include "local.h" |
| 43 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | #include "floatio.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 */ |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 52 | #define LONGDBL 0x00002 /* L: long double */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | |
| 91 | static u_char *__sccl(char *, u_char *); |
| 92 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | /* |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 94 | * Internal, unlocked version of vfscanf |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 | */ |
| 96 | int |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 97 | __svfscanf(FILE *fp, const char *fmt0, __va_list ap) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | { |
| 99 | u_char *fmt = (u_char *)fmt0; |
| 100 | int c; /* character from format, or conversion */ |
| 101 | size_t width; /* field width, or 0 */ |
| 102 | char *p; /* points into all kinds of strings */ |
| 103 | int n; /* handy integer */ |
| 104 | int flags; /* flags as defined above */ |
| 105 | char *p0; /* saves original value of p when necessary */ |
| 106 | int nassigned; /* number of fields assigned */ |
| 107 | int nread; /* number of characters consumed from fp */ |
| 108 | int base; /* base argument to strtoimax/strtouimax */ |
| 109 | char ccltab[256]; /* character class table for %[...] */ |
| 110 | char buf[BUF]; /* buffer for numeric conversions */ |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 111 | wchar_t *wcp; /* handy wide character pointer */ |
| 112 | size_t nconv; /* length of multibyte sequence converted */ |
| 113 | mbstate_t mbs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 114 | |
| 115 | /* `basefix' is used to avoid `if' tests in the integer scanner */ |
| 116 | static short basefix[17] = |
| 117 | { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; |
| 118 | |
| 119 | _SET_ORIENTATION(fp, -1); |
| 120 | |
| 121 | nassigned = 0; |
| 122 | nread = 0; |
| 123 | base = 0; /* XXX just to keep gcc happy */ |
| 124 | for (;;) { |
| 125 | c = *fmt++; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 126 | if (c == 0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 127 | return (nassigned); |
| 128 | if (isspace(c)) { |
| 129 | while ((fp->_r > 0 || __srefill(fp) == 0) && |
| 130 | isspace(*fp->_p)) |
| 131 | nread++, fp->_r--, fp->_p++; |
| 132 | continue; |
| 133 | } |
| 134 | if (c != '%') |
| 135 | goto literal; |
| 136 | width = 0; |
| 137 | flags = 0; |
| 138 | /* |
| 139 | * switch on the format. continue if done; |
| 140 | * break once format type is derived. |
| 141 | */ |
| 142 | again: c = *fmt++; |
| 143 | switch (c) { |
| 144 | case '%': |
| 145 | literal: |
| 146 | if (fp->_r <= 0 && __srefill(fp)) |
| 147 | goto input_failure; |
| 148 | if (*fp->_p != c) |
| 149 | goto match_failure; |
| 150 | fp->_r--, fp->_p++; |
| 151 | nread++; |
| 152 | continue; |
| 153 | |
| 154 | case '*': |
| 155 | flags |= SUPPRESS; |
| 156 | goto again; |
| 157 | case 'j': |
| 158 | flags |= MAXINT; |
| 159 | goto again; |
| 160 | case 'L': |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 161 | flags |= LONGDBL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | goto again; |
| 163 | case 'h': |
| 164 | if (*fmt == 'h') { |
| 165 | fmt++; |
| 166 | flags |= SHORTSHORT; |
| 167 | } else { |
| 168 | flags |= SHORT; |
| 169 | } |
| 170 | goto again; |
| 171 | case 'l': |
| 172 | if (*fmt == 'l') { |
| 173 | fmt++; |
| 174 | flags |= LLONG; |
| 175 | } else { |
| 176 | flags |= LONG; |
| 177 | } |
| 178 | goto again; |
| 179 | case 'q': |
| 180 | flags |= LLONG; /* deprecated */ |
| 181 | goto again; |
| 182 | case 't': |
| 183 | flags |= PTRINT; |
| 184 | goto again; |
| 185 | case 'z': |
| 186 | flags |= SIZEINT; |
| 187 | goto again; |
| 188 | |
| 189 | case '0': case '1': case '2': case '3': case '4': |
| 190 | case '5': case '6': case '7': case '8': case '9': |
| 191 | width = width * 10 + c - '0'; |
| 192 | goto again; |
| 193 | |
| 194 | /* |
| 195 | * Conversions. |
| 196 | * Those marked `compat' are for 4.[123]BSD compatibility. |
| 197 | * |
| 198 | * (According to ANSI, E and X formats are supposed |
| 199 | * to the same as e and x. Sorry about that.) |
| 200 | */ |
| 201 | case 'D': /* compat */ |
| 202 | flags |= LONG; |
| 203 | /* FALLTHROUGH */ |
| 204 | case 'd': |
| 205 | c = CT_INT; |
| 206 | base = 10; |
| 207 | break; |
| 208 | |
| 209 | case 'i': |
| 210 | c = CT_INT; |
| 211 | base = 0; |
| 212 | break; |
| 213 | |
| 214 | case 'O': /* compat */ |
| 215 | flags |= LONG; |
| 216 | /* FALLTHROUGH */ |
| 217 | case 'o': |
| 218 | c = CT_INT; |
| 219 | flags |= UNSIGNED; |
| 220 | base = 8; |
| 221 | break; |
| 222 | |
| 223 | case 'u': |
| 224 | c = CT_INT; |
| 225 | flags |= UNSIGNED; |
| 226 | base = 10; |
| 227 | break; |
| 228 | |
| 229 | case 'X': |
| 230 | case 'x': |
| 231 | flags |= PFXOK; /* enable 0x prefixing */ |
| 232 | c = CT_INT; |
| 233 | flags |= UNSIGNED; |
| 234 | base = 16; |
| 235 | break; |
| 236 | |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 237 | case 'e': case 'E': |
| 238 | case 'f': case 'F': |
| 239 | case 'g': case 'G': |
| 240 | case 'a': case 'A': |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 241 | c = CT_FLOAT; |
| 242 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 243 | |
| 244 | case 's': |
| 245 | c = CT_STRING; |
| 246 | break; |
| 247 | |
| 248 | case '[': |
| 249 | fmt = __sccl(ccltab, fmt); |
| 250 | flags |= NOSKIP; |
| 251 | c = CT_CCL; |
| 252 | break; |
| 253 | |
| 254 | case 'c': |
| 255 | flags |= NOSKIP; |
| 256 | c = CT_CHAR; |
| 257 | break; |
| 258 | |
| 259 | case 'p': /* pointer format is like hex */ |
| 260 | flags |= POINTER | PFXOK; |
| 261 | c = CT_INT; |
| 262 | flags |= UNSIGNED; |
| 263 | base = 16; |
| 264 | break; |
| 265 | |
| 266 | case 'n': |
| 267 | if (flags & SUPPRESS) |
| 268 | continue; |
| 269 | if (flags & SHORTSHORT) |
Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 270 | *va_arg(ap, signed char *) = nread; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 271 | else if (flags & SHORT) |
| 272 | *va_arg(ap, short *) = nread; |
| 273 | else if (flags & LONG) |
| 274 | *va_arg(ap, long *) = nread; |
| 275 | else if (flags & SIZEINT) |
| 276 | *va_arg(ap, ssize_t *) = nread; |
| 277 | else if (flags & PTRINT) |
| 278 | *va_arg(ap, ptrdiff_t *) = nread; |
| 279 | else if (flags & LLONG) |
| 280 | *va_arg(ap, long long *) = nread; |
| 281 | else if (flags & MAXINT) |
| 282 | *va_arg(ap, intmax_t *) = nread; |
| 283 | else |
| 284 | *va_arg(ap, int *) = nread; |
| 285 | continue; |
| 286 | |
| 287 | /* |
| 288 | * Disgusting backwards compatibility hacks. XXX |
| 289 | */ |
| 290 | case '\0': /* compat */ |
| 291 | return (EOF); |
| 292 | |
| 293 | default: /* compat */ |
| 294 | if (isupper(c)) |
| 295 | flags |= LONG; |
| 296 | c = CT_INT; |
| 297 | base = 10; |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * We have a conversion that requires input. |
| 303 | */ |
| 304 | if (fp->_r <= 0 && __srefill(fp)) |
| 305 | goto input_failure; |
| 306 | |
| 307 | /* |
| 308 | * Consume leading white space, except for formats |
| 309 | * that suppress this. |
| 310 | */ |
| 311 | if ((flags & NOSKIP) == 0) { |
| 312 | while (isspace(*fp->_p)) { |
| 313 | nread++; |
| 314 | if (--fp->_r > 0) |
| 315 | fp->_p++; |
| 316 | else if (__srefill(fp)) |
| 317 | goto input_failure; |
| 318 | } |
| 319 | /* |
| 320 | * Note that there is at least one character in |
| 321 | * the buffer, so conversions that do not set NOSKIP |
| 322 | * ca no longer result in an input failure. |
| 323 | */ |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Do the conversion. |
| 328 | */ |
| 329 | switch (c) { |
| 330 | |
| 331 | case CT_CHAR: |
| 332 | /* scan arbitrary characters (sets NOSKIP) */ |
| 333 | if (width == 0) |
| 334 | width = 1; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 335 | if (flags & LONG) { |
| 336 | if ((flags & SUPPRESS) == 0) |
| 337 | wcp = va_arg(ap, wchar_t *); |
| 338 | else |
| 339 | wcp = NULL; |
| 340 | n = 0; |
| 341 | while (width != 0) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 342 | if (n == (int)MB_CUR_MAX) { |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 343 | fp->_flags |= __SERR; |
| 344 | goto input_failure; |
| 345 | } |
| 346 | buf[n++] = *fp->_p; |
| 347 | fp->_p++; |
| 348 | fp->_r--; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 349 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 350 | nconv = mbrtowc(wcp, buf, n, &mbs); |
| 351 | if (nconv == (size_t)-1) { |
| 352 | fp->_flags |= __SERR; |
| 353 | goto input_failure; |
| 354 | } |
| 355 | if (nconv == 0 && !(flags & SUPPRESS)) |
| 356 | *wcp = L'\0'; |
| 357 | if (nconv != (size_t)-2) { |
| 358 | nread += n; |
| 359 | width--; |
| 360 | if (!(flags & SUPPRESS)) |
| 361 | wcp++; |
| 362 | n = 0; |
| 363 | } |
| 364 | if (fp->_r <= 0 && __srefill(fp)) { |
| 365 | if (n != 0) { |
| 366 | fp->_flags |= __SERR; |
| 367 | goto input_failure; |
| 368 | } |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | if (!(flags & SUPPRESS)) |
| 373 | nassigned++; |
Elliott Hughes | d9a7de1 | 2017-10-31 09:55:40 -0700 | [diff] [blame] | 374 | } else if (flags & SUPPRESS) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 375 | size_t sum = 0; |
| 376 | for (;;) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 377 | if ((n = fp->_r) < (int)width) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 378 | sum += n; |
| 379 | width -= n; |
| 380 | fp->_p += n; |
| 381 | if (__srefill(fp)) { |
| 382 | if (sum == 0) |
| 383 | goto input_failure; |
| 384 | break; |
| 385 | } |
| 386 | } else { |
| 387 | sum += width; |
| 388 | fp->_r -= width; |
| 389 | fp->_p += width; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | nread += sum; |
| 394 | } else { |
| 395 | size_t r = fread((void *)va_arg(ap, char *), 1, |
| 396 | width, fp); |
| 397 | |
| 398 | if (r == 0) |
| 399 | goto input_failure; |
| 400 | nread += r; |
| 401 | nassigned++; |
| 402 | } |
| 403 | break; |
| 404 | |
| 405 | case CT_CCL: |
| 406 | /* scan a (nonempty) character class (sets NOSKIP) */ |
| 407 | if (width == 0) |
| 408 | width = (size_t)~0; /* `infinity' */ |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 409 | /* take only those things in the class */ |
| 410 | if (flags & LONG) { |
| 411 | wchar_t twc; |
| 412 | int nchars; |
| 413 | |
| 414 | if ((flags & SUPPRESS) == 0) |
| 415 | wcp = va_arg(ap, wchar_t *); |
| 416 | else |
| 417 | wcp = &twc; |
| 418 | n = 0; |
| 419 | nchars = 0; |
| 420 | while (width != 0) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 421 | if (n == (int)MB_CUR_MAX) { |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 422 | fp->_flags |= __SERR; |
| 423 | goto input_failure; |
| 424 | } |
| 425 | buf[n++] = *fp->_p; |
| 426 | fp->_p++; |
| 427 | fp->_r--; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 428 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 429 | nconv = mbrtowc(wcp, buf, n, &mbs); |
| 430 | if (nconv == (size_t)-1) { |
| 431 | fp->_flags |= __SERR; |
| 432 | goto input_failure; |
| 433 | } |
| 434 | if (nconv == 0) |
| 435 | *wcp = L'\0'; |
| 436 | if (nconv != (size_t)-2) { |
| 437 | if (wctob(*wcp) != EOF && |
| 438 | !ccltab[wctob(*wcp)]) { |
| 439 | while (n != 0) { |
| 440 | n--; |
| 441 | ungetc(buf[n], |
| 442 | fp); |
| 443 | } |
| 444 | break; |
| 445 | } |
| 446 | nread += n; |
| 447 | width--; |
| 448 | if (!(flags & SUPPRESS)) |
| 449 | wcp++; |
| 450 | nchars++; |
| 451 | n = 0; |
| 452 | } |
| 453 | if (fp->_r <= 0 && __srefill(fp)) { |
| 454 | if (n != 0) { |
| 455 | fp->_flags |= __SERR; |
| 456 | goto input_failure; |
| 457 | } |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | if (n != 0) { |
| 462 | fp->_flags |= __SERR; |
| 463 | goto input_failure; |
| 464 | } |
| 465 | n = nchars; |
| 466 | if (n == 0) |
| 467 | goto match_failure; |
| 468 | if (!(flags & SUPPRESS)) { |
| 469 | *wcp = L'\0'; |
| 470 | nassigned++; |
| 471 | } |
| 472 | } else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 473 | /* take only those things in the class */ |
| 474 | if (flags & SUPPRESS) { |
| 475 | n = 0; |
| 476 | while (ccltab[*fp->_p]) { |
| 477 | n++, fp->_r--, fp->_p++; |
| 478 | if (--width == 0) |
| 479 | break; |
| 480 | if (fp->_r <= 0 && __srefill(fp)) { |
| 481 | if (n == 0) |
| 482 | goto input_failure; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | if (n == 0) |
| 487 | goto match_failure; |
| 488 | } else { |
| 489 | p0 = p = va_arg(ap, char *); |
| 490 | while (ccltab[*fp->_p]) { |
| 491 | fp->_r--; |
| 492 | *p++ = *fp->_p++; |
| 493 | if (--width == 0) |
| 494 | break; |
| 495 | if (fp->_r <= 0 && __srefill(fp)) { |
| 496 | if (p == p0) |
| 497 | goto input_failure; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | n = p - p0; |
| 502 | if (n == 0) |
| 503 | goto match_failure; |
| 504 | *p = '\0'; |
| 505 | nassigned++; |
| 506 | } |
| 507 | nread += n; |
| 508 | break; |
| 509 | |
| 510 | case CT_STRING: |
| 511 | /* like CCL, but zero-length string OK, & no NOSKIP */ |
| 512 | if (width == 0) |
| 513 | width = (size_t)~0; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 514 | if (flags & LONG) { |
| 515 | wchar_t twc; |
| 516 | |
| 517 | if ((flags & SUPPRESS) == 0) |
| 518 | wcp = va_arg(ap, wchar_t *); |
| 519 | else |
| 520 | wcp = &twc; |
| 521 | n = 0; |
| 522 | while (!isspace(*fp->_p) && width != 0) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 523 | if (n == (int)MB_CUR_MAX) { |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 524 | fp->_flags |= __SERR; |
| 525 | goto input_failure; |
| 526 | } |
| 527 | buf[n++] = *fp->_p; |
| 528 | fp->_p++; |
| 529 | fp->_r--; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 530 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 531 | nconv = mbrtowc(wcp, buf, n, &mbs); |
| 532 | if (nconv == (size_t)-1) { |
| 533 | fp->_flags |= __SERR; |
| 534 | goto input_failure; |
| 535 | } |
| 536 | if (nconv == 0) |
| 537 | *wcp = L'\0'; |
| 538 | if (nconv != (size_t)-2) { |
| 539 | if (iswspace(*wcp)) { |
| 540 | while (n != 0) { |
| 541 | n--; |
| 542 | ungetc(buf[n], |
| 543 | fp); |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | nread += n; |
| 548 | width--; |
| 549 | if (!(flags & SUPPRESS)) |
| 550 | wcp++; |
| 551 | n = 0; |
| 552 | } |
| 553 | if (fp->_r <= 0 && __srefill(fp)) { |
| 554 | if (n != 0) { |
| 555 | fp->_flags |= __SERR; |
| 556 | goto input_failure; |
| 557 | } |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | if (!(flags & SUPPRESS)) { |
| 562 | *wcp = L'\0'; |
| 563 | nassigned++; |
| 564 | } |
Elliott Hughes | d9a7de1 | 2017-10-31 09:55:40 -0700 | [diff] [blame] | 565 | } else if (flags & SUPPRESS) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 566 | n = 0; |
| 567 | while (!isspace(*fp->_p)) { |
| 568 | n++, fp->_r--, fp->_p++; |
| 569 | if (--width == 0) |
| 570 | break; |
| 571 | if (fp->_r <= 0 && __srefill(fp)) |
| 572 | break; |
| 573 | } |
| 574 | nread += n; |
| 575 | } else { |
| 576 | p0 = p = va_arg(ap, char *); |
| 577 | while (!isspace(*fp->_p)) { |
| 578 | fp->_r--; |
| 579 | *p++ = *fp->_p++; |
| 580 | if (--width == 0) |
| 581 | break; |
| 582 | if (fp->_r <= 0 && __srefill(fp)) |
| 583 | break; |
| 584 | } |
| 585 | *p = '\0'; |
| 586 | nread += p - p0; |
| 587 | nassigned++; |
| 588 | } |
| 589 | continue; |
| 590 | |
| 591 | case CT_INT: |
| 592 | /* scan an integer as if by strtoimax/strtoumax */ |
| 593 | #ifdef hardway |
| 594 | if (width == 0 || width > sizeof(buf) - 1) |
| 595 | width = sizeof(buf) - 1; |
| 596 | #else |
| 597 | /* size_t is unsigned, hence this optimisation */ |
| 598 | if (--width > sizeof(buf) - 2) |
| 599 | width = sizeof(buf) - 2; |
| 600 | width++; |
| 601 | #endif |
| 602 | flags |= SIGNOK | NDIGITS | NZDIGITS; |
| 603 | for (p = buf; width; width--) { |
| 604 | c = *fp->_p; |
| 605 | /* |
| 606 | * Switch on the character; `goto ok' |
| 607 | * if we accept it as a part of number. |
| 608 | */ |
| 609 | switch (c) { |
| 610 | |
| 611 | /* |
| 612 | * The digit 0 is always legal, but is |
| 613 | * special. For %i conversions, if no |
| 614 | * digits (zero or nonzero) have been |
| 615 | * scanned (only signs), we will have |
| 616 | * base==0. In that case, we should set |
| 617 | * it to 8 and enable 0x prefixing. |
| 618 | * Also, if we have not scanned zero digits |
| 619 | * before this, do not turn off prefixing |
| 620 | * (someone else will turn it off if we |
| 621 | * have scanned any nonzero digits). |
| 622 | */ |
| 623 | case '0': |
| 624 | if (base == 0) { |
| 625 | base = 8; |
| 626 | flags |= PFXOK; |
| 627 | } |
| 628 | if (flags & NZDIGITS) |
| 629 | flags &= ~(SIGNOK|NZDIGITS|NDIGITS); |
| 630 | else |
| 631 | flags &= ~(SIGNOK|PFXOK|NDIGITS); |
| 632 | goto ok; |
| 633 | |
| 634 | /* 1 through 7 always legal */ |
| 635 | case '1': case '2': case '3': |
| 636 | case '4': case '5': case '6': case '7': |
| 637 | base = basefix[base]; |
| 638 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 639 | goto ok; |
| 640 | |
| 641 | /* digits 8 and 9 ok iff decimal or hex */ |
| 642 | case '8': case '9': |
| 643 | base = basefix[base]; |
| 644 | if (base <= 8) |
| 645 | break; /* not legal here */ |
| 646 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 647 | goto ok; |
| 648 | |
| 649 | /* letters ok iff hex */ |
| 650 | case 'A': case 'B': case 'C': |
| 651 | case 'D': case 'E': case 'F': |
| 652 | case 'a': case 'b': case 'c': |
| 653 | case 'd': case 'e': case 'f': |
| 654 | /* no need to fix base here */ |
| 655 | if (base <= 10) |
| 656 | break; /* not legal here */ |
| 657 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 658 | goto ok; |
| 659 | |
| 660 | /* sign ok only as first character */ |
| 661 | case '+': case '-': |
| 662 | if (flags & SIGNOK) { |
| 663 | flags &= ~SIGNOK; |
| 664 | flags |= HAVESIGN; |
| 665 | goto ok; |
| 666 | } |
| 667 | break; |
| 668 | |
| 669 | /* |
| 670 | * x ok iff flag still set and 2nd char (or |
| 671 | * 3rd char if we have a sign). |
| 672 | */ |
| 673 | case 'x': case 'X': |
| 674 | if ((flags & PFXOK) && p == |
| 675 | buf + 1 + !!(flags & HAVESIGN)) { |
| 676 | base = 16; /* if %i */ |
| 677 | flags &= ~PFXOK; |
| 678 | goto ok; |
| 679 | } |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * If we got here, c is not a legal character |
| 685 | * for a number. Stop accumulating digits. |
| 686 | */ |
| 687 | break; |
| 688 | ok: |
| 689 | /* |
| 690 | * c is legal: store it and look at the next. |
| 691 | */ |
| 692 | *p++ = c; |
| 693 | if (--fp->_r > 0) |
| 694 | fp->_p++; |
| 695 | else if (__srefill(fp)) |
| 696 | break; /* EOF */ |
| 697 | } |
| 698 | /* |
| 699 | * If we had only a sign, it is no good; push |
| 700 | * back the sign. If the number ends in `x', |
| 701 | * it was [sign] '0' 'x', so push back the x |
| 702 | * and treat it as [sign] '0'. |
| 703 | */ |
| 704 | if (flags & NDIGITS) { |
| 705 | if (p > buf) |
| 706 | (void) ungetc(*(u_char *)--p, fp); |
| 707 | goto match_failure; |
| 708 | } |
| 709 | c = ((u_char *)p)[-1]; |
| 710 | if (c == 'x' || c == 'X') { |
| 711 | --p; |
| 712 | (void) ungetc(c, fp); |
| 713 | } |
| 714 | if ((flags & SUPPRESS) == 0) { |
| 715 | uintmax_t res; |
| 716 | |
| 717 | *p = '\0'; |
| 718 | if (flags & UNSIGNED) |
| 719 | res = strtoumax(buf, NULL, base); |
| 720 | else |
| 721 | res = strtoimax(buf, NULL, base); |
| 722 | if (flags & POINTER) |
| 723 | *va_arg(ap, void **) = |
| 724 | (void *)(uintptr_t)res; |
| 725 | else if (flags & MAXINT) |
| 726 | *va_arg(ap, intmax_t *) = res; |
| 727 | else if (flags & LLONG) |
| 728 | *va_arg(ap, long long *) = res; |
| 729 | else if (flags & SIZEINT) |
| 730 | *va_arg(ap, ssize_t *) = res; |
| 731 | else if (flags & PTRINT) |
| 732 | *va_arg(ap, ptrdiff_t *) = res; |
| 733 | else if (flags & LONG) |
| 734 | *va_arg(ap, long *) = res; |
| 735 | else if (flags & SHORT) |
| 736 | *va_arg(ap, short *) = res; |
| 737 | else if (flags & SHORTSHORT) |
Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 738 | *va_arg(ap, signed char *) = res; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 739 | else |
| 740 | *va_arg(ap, int *) = res; |
| 741 | nassigned++; |
| 742 | } |
| 743 | nread += p - buf; |
| 744 | break; |
| 745 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 746 | case CT_FLOAT: |
| 747 | /* scan a floating point number as if by strtod */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 748 | if (width == 0 || width > sizeof(buf) - 1) |
| 749 | width = sizeof(buf) - 1; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 750 | if ((width = parsefloat(fp, buf, buf + width)) == 0) |
| 751 | goto match_failure; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 752 | if ((flags & SUPPRESS) == 0) { |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 753 | if (flags & LONGDBL) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 754 | long double res = strtold(buf, &p); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 755 | *va_arg(ap, long double *) = res; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 756 | } else if (flags & LONG) { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 757 | double res = strtod(buf, &p); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 758 | *va_arg(ap, double *) = res; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 759 | } else { |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 760 | float res = strtof(buf, &p); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 761 | *va_arg(ap, float *) = res; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 762 | } |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 763 | if ((size_t)(p - buf) != width) abort(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 764 | nassigned++; |
| 765 | } |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 766 | nread += width; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 767 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | input_failure: |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 771 | if (nassigned == 0) |
| 772 | nassigned = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 773 | match_failure: |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 774 | return (nassigned); |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * Fill in the given table from the scanset at the given format |
| 779 | * (just after `['). Return a pointer to the character past the |
| 780 | * closing `]'. The table has a 1 wherever characters should be |
| 781 | * considered part of the scanset. |
| 782 | */ |
| 783 | static u_char * |
| 784 | __sccl(char *tab, u_char *fmt) |
| 785 | { |
| 786 | int c, n, v; |
| 787 | |
| 788 | /* first `clear' the whole table */ |
| 789 | c = *fmt++; /* first char hat => negated scanset */ |
| 790 | if (c == '^') { |
| 791 | v = 1; /* default => accept */ |
| 792 | c = *fmt++; /* get new first char */ |
| 793 | } else |
| 794 | v = 0; /* default => reject */ |
| 795 | /* should probably use memset here */ |
| 796 | for (n = 0; n < 256; n++) |
| 797 | tab[n] = v; |
| 798 | if (c == 0) |
| 799 | return (fmt - 1);/* format ended before closing ] */ |
| 800 | |
| 801 | /* |
| 802 | * Now set the entries corresponding to the actual scanset |
| 803 | * to the opposite of the above. |
| 804 | * |
| 805 | * The first character may be ']' (or '-') without being special; |
| 806 | * the last character may be '-'. |
| 807 | */ |
| 808 | v = 1 - v; |
| 809 | for (;;) { |
| 810 | tab[c] = v; /* take character c */ |
| 811 | doswitch: |
| 812 | n = *fmt++; /* and examine the next */ |
| 813 | switch (n) { |
| 814 | |
| 815 | case 0: /* format ended too soon */ |
| 816 | return (fmt - 1); |
| 817 | |
| 818 | case '-': |
| 819 | /* |
| 820 | * A scanset of the form |
| 821 | * [01+-] |
| 822 | * is defined as `the digit 0, the digit 1, |
| 823 | * the character +, the character -', but |
| 824 | * the effect of a scanset such as |
| 825 | * [a-zA-Z0-9] |
| 826 | * is implementation defined. The V7 Unix |
| 827 | * scanf treats `a-z' as `the letters a through |
| 828 | * z', but treats `a-a' as `the letter a, the |
| 829 | * character -, and the letter a'. |
| 830 | * |
| 831 | * For compatibility, the `-' is not considerd |
| 832 | * to define a range if the character following |
| 833 | * it is either a close bracket (required by ANSI) |
| 834 | * or is not numerically greater than the character |
| 835 | * we just stored in the table (c). |
| 836 | */ |
| 837 | n = *fmt; |
| 838 | if (n == ']' || n < c) { |
| 839 | c = '-'; |
| 840 | break; /* resume the for(;;) */ |
| 841 | } |
| 842 | fmt++; |
| 843 | do { /* fill in the range */ |
| 844 | tab[++c] = v; |
| 845 | } while (c < n); |
| 846 | #if 1 /* XXX another disgusting compatibility hack */ |
| 847 | /* |
| 848 | * Alas, the V7 Unix scanf also treats formats |
| 849 | * such as [a-c-e] as `the letters a through e'. |
| 850 | * This too is permitted by the standard.... |
| 851 | */ |
| 852 | goto doswitch; |
| 853 | #else |
| 854 | c = *fmt++; |
| 855 | if (c == 0) |
| 856 | return (fmt - 1); |
| 857 | if (c == ']') |
| 858 | return (fmt); |
| 859 | #endif |
| 860 | break; |
| 861 | |
| 862 | case ']': /* end of scanset */ |
| 863 | return (fmt); |
| 864 | |
| 865 | default: /* just another character */ |
| 866 | c = n; |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | /* NOTREACHED */ |
| 871 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 872 | |
| 873 | int |
| 874 | vfscanf(FILE *fp, const char *fmt0, __va_list ap) |
| 875 | { |
| 876 | int r; |
| 877 | |
| 878 | FLOCKFILE(fp); |
| 879 | r = __svfscanf(fp, fmt0, ap); |
| 880 | FUNLOCKFILE(fp); |
| 881 | return (r); |
| 882 | } |