| Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 1 | /*	$OpenBSD: vfwscanf.c,v 1.4 2014/03/19 05:17:01 guenther Exp $ */ | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [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 |  | 
| zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 34 | #include "scanf_common.h" | 
| Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 35 | // An interpretive version of __sccl from vfscanf.c --- a table of all wchar_t values would | 
 | 36 | // be a little too expensive, and some kind of compressed version isn't worth the trouble. | 
 | 37 | static inline bool in_ccl(wchar_t wc, const wchar_t* ccl) { | 
 | 38 |   // Is this a negated set? | 
 | 39 |   bool member_result = true; | 
 | 40 |   if (*ccl == '^') { | 
 | 41 |     member_result = false; | 
 | 42 |     ++ccl; | 
 | 43 |   } | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 44 |  | 
| Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 45 |   // The first character may be ']' or '-' without being special. | 
 | 46 |   if (*ccl == '-' || *ccl == ']') { | 
 | 47 |     // A literal match? | 
 | 48 |     if (*ccl == wc) return member_result; | 
 | 49 |     ++ccl; | 
 | 50 |   } | 
 | 51 |  | 
 | 52 |   while (*ccl && *ccl != ']') { | 
 | 53 |     // The last character may be '-' without being special. | 
 | 54 |     if (*ccl == '-' && ccl[1] != '\0' && ccl[1] != ']') { | 
 | 55 |       wchar_t first = *(ccl - 1); | 
 | 56 |       wchar_t last = *(ccl + 1); | 
 | 57 |       if (first <= last) { | 
 | 58 |         // In the range? | 
 | 59 |         if (wc >= first && wc <= last) return member_result; | 
 | 60 |         ccl += 2; | 
 | 61 |         continue; | 
 | 62 |       } | 
 | 63 |       // A '-' is not considered to be part of a range if the character after | 
 | 64 |       // is not greater than the character before, so fall through... | 
 | 65 |     } | 
 | 66 |     // A literal match? | 
 | 67 |     if (*ccl == wc) return member_result; | 
 | 68 |     ++ccl; | 
 | 69 |   } | 
 | 70 |   return !member_result; | 
 | 71 | } | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 72 |  | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 73 | #pragma GCC diagnostic push | 
 | 74 | #pragma GCC diagnostic ignored "-Wframe-larger-than=" | 
 | 75 |  | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 76 | /* | 
 | 77 |  * vfwscanf | 
 | 78 |  */ | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 79 | int __vfwscanf(FILE* __restrict fp, const wchar_t* __restrict fmt, __va_list ap) { | 
 | 80 |   wint_t c;               /* character from format, or conversion */ | 
 | 81 |   size_t width;           /* field width, or 0 */ | 
 | 82 |   wchar_t* p;             /* points into all kinds of strings */ | 
 | 83 |   int n;                  /* handy integer */ | 
 | 84 |   int flags;              /* flags as defined above */ | 
 | 85 |   wchar_t* p0;            /* saves original value of p when necessary */ | 
 | 86 |   int nassigned;          /* number of fields assigned */ | 
 | 87 |   int nconversions;       /* number of conversions */ | 
 | 88 |   int nread;              /* number of characters consumed from fp */ | 
 | 89 |   int base;               /* base argument to strtoimax/strtouimax */ | 
 | 90 |   wchar_t buf[BUF];       /* buffer for numeric conversions */ | 
| Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 91 |   const wchar_t* ccl; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 92 |   wint_t wi;              /* handy wint_t */ | 
 | 93 |   char* mbp;              /* multibyte string pointer for %c %s %[ */ | 
 | 94 |   size_t nconv;           /* number of bytes in mb. conversion */ | 
 | 95 |   char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */ | 
 | 96 |   mbstate_t mbs; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 97 |  | 
| Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 98 |   _SET_ORIENTATION(fp, ORIENT_CHARS); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 99 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 100 |   nassigned = 0; | 
 | 101 |   nconversions = 0; | 
 | 102 |   nread = 0; | 
 | 103 |   base = 0; /* XXX just to keep gcc happy */ | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 104 |   for (;;) { | 
 | 105 |     c = *fmt++; | 
 | 106 |     if (c == 0) { | 
 | 107 |       return (nassigned); | 
 | 108 |     } | 
 | 109 |     if (iswspace(c)) { | 
 | 110 |       while ((c = __fgetwc_unlock(fp)) != WEOF && iswspace(c)) | 
 | 111 |         ; | 
 | 112 |       if (c != WEOF) __ungetwc(c, fp); | 
 | 113 |       continue; | 
 | 114 |     } | 
 | 115 |     if (c != '%') goto literal; | 
 | 116 |     width = 0; | 
 | 117 |     flags = 0; | 
 | 118 |     /* | 
 | 119 |      * switch on the format.  continue if done; | 
 | 120 |      * break once format type is derived. | 
 | 121 |      */ | 
 | 122 |   again: | 
 | 123 |     c = *fmt++; | 
| zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 124 |   reswitch: | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 125 |     switch (c) { | 
 | 126 |       case '%': | 
 | 127 |       literal: | 
 | 128 |         if ((wi = __fgetwc_unlock(fp)) == WEOF) goto input_failure; | 
 | 129 |         if (wi != c) { | 
 | 130 |           __ungetwc(wi, fp); | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 131 |           goto match_failure; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 132 |         } | 
 | 133 |         nread++; | 
 | 134 |         continue; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 135 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 136 |       case '*': | 
 | 137 |         flags |= SUPPRESS; | 
 | 138 |         goto again; | 
 | 139 |       case 'j': | 
 | 140 |         flags |= MAXINT; | 
 | 141 |         goto again; | 
 | 142 |       case 'L': | 
 | 143 |         flags |= LONGDBL; | 
 | 144 |         goto again; | 
 | 145 |       case 'h': | 
 | 146 |         if (*fmt == 'h') { | 
 | 147 |           fmt++; | 
 | 148 |           flags |= SHORTSHORT; | 
 | 149 |         } else { | 
 | 150 |           flags |= SHORT; | 
 | 151 |         } | 
 | 152 |         goto again; | 
 | 153 |       case 'l': | 
 | 154 |         if (*fmt == 'l') { | 
 | 155 |           fmt++; | 
 | 156 |           flags |= LLONG; | 
 | 157 |         } else { | 
 | 158 |           flags |= LONG; | 
 | 159 |         } | 
 | 160 |         goto again; | 
 | 161 |       case 'q': | 
 | 162 |         flags |= LLONG; /* deprecated */ | 
 | 163 |         goto again; | 
 | 164 |       case 't': | 
 | 165 |         flags |= PTRINT; | 
 | 166 |         goto again; | 
 | 167 |       case 'z': | 
 | 168 |         flags |= SIZEINT; | 
 | 169 |         goto again; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 170 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 171 |       case '0': | 
 | 172 |       case '1': | 
 | 173 |       case '2': | 
 | 174 |       case '3': | 
 | 175 |       case '4': | 
 | 176 |       case '5': | 
 | 177 |       case '6': | 
 | 178 |       case '7': | 
 | 179 |       case '8': | 
 | 180 |       case '9': | 
 | 181 |         width = width * 10 + c - '0'; | 
 | 182 |         goto again; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 183 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 184 |       /* | 
 | 185 |        * Conversions. | 
 | 186 |        * Those marked `compat' are for 4.[123]BSD compatibility. | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 187 |        */ | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 188 |       case 'b': | 
 | 189 |         c = CT_INT; | 
 | 190 |         base = 2; | 
 | 191 |         flags |= PFBOK; /* enable 0b prefixing */ | 
 | 192 |         break; | 
 | 193 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 194 |       case 'D': /* compat */ | 
 | 195 |         flags |= LONG; | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 196 |         __BIONIC_FALLTHROUGH; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 197 |       case 'd': | 
 | 198 |         c = CT_INT; | 
 | 199 |         base = 10; | 
 | 200 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 201 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 202 |       case 'i': | 
 | 203 |         c = CT_INT; | 
 | 204 |         base = 0; | 
 | 205 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 206 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 207 |       case 'O': /* compat */ | 
 | 208 |         flags |= LONG; | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 209 |         __BIONIC_FALLTHROUGH; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 210 |       case 'o': | 
 | 211 |         c = CT_INT; | 
 | 212 |         flags |= UNSIGNED; | 
 | 213 |         base = 8; | 
 | 214 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 215 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 216 |       case 'u': | 
 | 217 |         c = CT_INT; | 
 | 218 |         flags |= UNSIGNED; | 
 | 219 |         base = 10; | 
 | 220 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 221 |  | 
| zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 222 |       case 'w': { | 
 | 223 |         int size = 0; | 
 | 224 |         bool fast = false; | 
 | 225 |         c = *fmt++; | 
| zijunzhao | cc475cf | 2023-06-13 00:34:13 +0000 | [diff] [blame] | 226 |         if (c == 'f') { | 
 | 227 |           fast = true; | 
 | 228 |           c = *fmt++; | 
 | 229 |         } | 
| zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 230 |         while (is_digit(c)) { | 
 | 231 |           APPEND_DIGIT(size, c); | 
 | 232 |           c = *fmt++; | 
 | 233 |         } | 
 | 234 |         flags |= w_to_flag(size, fast); | 
 | 235 |         goto reswitch; | 
 | 236 |       } | 
 | 237 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 238 |       case 'X': | 
 | 239 |       case 'x': | 
 | 240 |         flags |= PFXOK; /* enable 0x prefixing */ | 
 | 241 |         c = CT_INT; | 
 | 242 |         flags |= UNSIGNED; | 
 | 243 |         base = 16; | 
 | 244 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 245 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 246 |       case 'e': | 
 | 247 |       case 'E': | 
 | 248 |       case 'f': | 
 | 249 |       case 'F': | 
 | 250 |       case 'g': | 
 | 251 |       case 'G': | 
 | 252 |       case 'a': | 
 | 253 |       case 'A': | 
 | 254 |         c = CT_FLOAT; | 
 | 255 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 256 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 257 |       case 's': | 
 | 258 |         c = CT_STRING; | 
 | 259 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 260 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 261 |       case '[': | 
| Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 262 |         ccl = fmt; | 
 | 263 |         if (*fmt == '^') fmt++; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 264 |         if (*fmt == ']') fmt++; | 
 | 265 |         while (*fmt != '\0' && *fmt != ']') fmt++; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 266 |         fmt++; | 
 | 267 |         flags |= NOSKIP; | 
 | 268 |         c = CT_CCL; | 
 | 269 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 270 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 271 |       case 'c': | 
 | 272 |         flags |= NOSKIP; | 
 | 273 |         c = CT_CHAR; | 
 | 274 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 275 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 276 |       case 'p': /* pointer format is like hex */ | 
 | 277 |         flags |= POINTER | PFXOK; | 
 | 278 |         c = CT_INT; | 
 | 279 |         flags |= UNSIGNED; | 
 | 280 |         base = 16; | 
 | 281 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 282 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 283 |       case 'n': | 
 | 284 |         nconversions++; | 
 | 285 |         if (flags & SUPPRESS) continue; | 
 | 286 |         if (flags & SHORTSHORT) | 
 | 287 |           *va_arg(ap, signed char*) = nread; | 
 | 288 |         else if (flags & SHORT) | 
 | 289 |           *va_arg(ap, short*) = nread; | 
 | 290 |         else if (flags & LONG) | 
 | 291 |           *va_arg(ap, long*) = nread; | 
 | 292 |         else if (flags & SIZEINT) | 
 | 293 |           *va_arg(ap, ssize_t*) = nread; | 
 | 294 |         else if (flags & PTRINT) | 
 | 295 |           *va_arg(ap, ptrdiff_t*) = nread; | 
 | 296 |         else if (flags & LLONG) | 
 | 297 |           *va_arg(ap, long long*) = nread; | 
 | 298 |         else if (flags & MAXINT) | 
 | 299 |           *va_arg(ap, intmax_t*) = nread; | 
 | 300 |         else | 
 | 301 |           *va_arg(ap, int*) = nread; | 
 | 302 |         continue; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 303 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 304 |       /* | 
 | 305 |        * Disgusting backwards compatibility hacks.	XXX | 
 | 306 |        */ | 
 | 307 |       case '\0': /* compat */ | 
 | 308 |         return (EOF); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 309 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 310 |       default: /* compat */ | 
 | 311 |         if (iswupper(c)) flags |= LONG; | 
 | 312 |         c = CT_INT; | 
 | 313 |         base = 10; | 
 | 314 |         break; | 
 | 315 |     } | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 316 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 317 |     /* | 
 | 318 |      * Consume leading white space, except for formats | 
 | 319 |      * that suppress this. | 
 | 320 |      */ | 
 | 321 |     if ((flags & NOSKIP) == 0) { | 
 | 322 |       while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi)) nread++; | 
 | 323 |       if (wi == WEOF) goto input_failure; | 
 | 324 |       __ungetwc(wi, fp); | 
 | 325 |     } | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 326 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 327 |     /* | 
 | 328 |      * Do the conversion. | 
 | 329 |      */ | 
 | 330 |     switch (c) { | 
 | 331 |       case CT_CHAR: | 
 | 332 |         /* scan arbitrary characters (sets NOSKIP) */ | 
 | 333 |         if (width == 0) width = 1; | 
 | 334 |         if (flags & LONG) { | 
 | 335 |           if (!(flags & SUPPRESS)) p = va_arg(ap, wchar_t*); | 
 | 336 |           n = 0; | 
 | 337 |           while (width-- != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) { | 
 | 338 |             if (!(flags & SUPPRESS)) *p++ = (wchar_t)wi; | 
 | 339 |             n++; | 
 | 340 |           } | 
 | 341 |           if (n == 0) goto input_failure; | 
 | 342 |           nread += n; | 
 | 343 |           if (!(flags & SUPPRESS)) nassigned++; | 
 | 344 |         } else { | 
 | 345 |           if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*); | 
 | 346 |           n = 0; | 
 | 347 |           memset(&mbs, 0, sizeof(mbs)); | 
 | 348 |           while (width != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) { | 
 | 349 |             if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) { | 
 | 350 |               nconv = wcrtomb(mbp, wi, &mbs); | 
 | 351 |               if (nconv == (size_t)-1) goto input_failure; | 
 | 352 |             } else { | 
 | 353 |               nconv = wcrtomb(mbbuf, wi, &mbs); | 
 | 354 |               if (nconv == (size_t)-1) goto input_failure; | 
 | 355 |               if (nconv > width) { | 
 | 356 |                 __ungetwc(wi, fp); | 
 | 357 |                 break; | 
 | 358 |               } | 
 | 359 |               if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv); | 
 | 360 |             } | 
 | 361 |             if (!(flags & SUPPRESS)) mbp += nconv; | 
 | 362 |             width -= nconv; | 
 | 363 |             n++; | 
 | 364 |           } | 
 | 365 |           if (n == 0) goto input_failure; | 
 | 366 |           nread += n; | 
 | 367 |           if (!(flags & SUPPRESS)) nassigned++; | 
 | 368 |         } | 
 | 369 |         nconversions++; | 
 | 370 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 371 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 372 |       case CT_CCL: | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 373 |       case CT_STRING: | 
 | 374 |         // CT_CCL: scan a (nonempty) character class (sets NOSKIP). | 
 | 375 |         // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP. | 
 | 376 |         if (width == 0) width = (size_t)~0; // 'infinity'. | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 377 |         if ((flags & SUPPRESS) && (flags & LONG)) { | 
 | 378 |           n = 0; | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 379 |           while ((wi = __fgetwc_unlock(fp)) != WEOF && width-- != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) n++; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 380 |           if (wi != WEOF) __ungetwc(wi, fp); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 381 |         } else if (flags & LONG) { | 
 | 382 |           p0 = p = va_arg(ap, wchar_t*); | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 383 |           while ((wi = __fgetwc_unlock(fp)) != WEOF && width-- != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 384 |             *p++ = (wchar_t)wi; | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 385 |           } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 386 |           if (wi != WEOF) __ungetwc(wi, fp); | 
 | 387 |           n = p - p0; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 388 |         } else { | 
 | 389 |           if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*); | 
 | 390 |           n = 0; | 
 | 391 |           memset(&mbs, 0, sizeof(mbs)); | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 392 |           while ((wi = __fgetwc_unlock(fp)) != WEOF && width != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 393 |             if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) { | 
 | 394 |               nconv = wcrtomb(mbp, wi, &mbs); | 
 | 395 |               if (nconv == (size_t)-1) goto input_failure; | 
 | 396 |             } else { | 
 | 397 |               nconv = wcrtomb(mbbuf, wi, &mbs); | 
 | 398 |               if (nconv == (size_t)-1) goto input_failure; | 
 | 399 |               if (nconv > width) break; | 
 | 400 |               if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv); | 
 | 401 |             } | 
 | 402 |             if (!(flags & SUPPRESS)) mbp += nconv; | 
 | 403 |             width -= nconv; | 
 | 404 |             n++; | 
 | 405 |           } | 
 | 406 |           if (wi != WEOF) __ungetwc(wi, fp); | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 407 |         } | 
 | 408 |         if (c == CT_CCL && n == 0) goto match_failure; | 
 | 409 |         if (!(flags & SUPPRESS)) { | 
 | 410 |           if (flags & LONG) { | 
 | 411 |             *p = L'\0'; | 
 | 412 |           } else { | 
 | 413 |             *mbp = '\0'; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 414 |           } | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 415 |           ++nassigned; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 416 |         } | 
 | 417 |         nread += n; | 
 | 418 |         nconversions++; | 
 | 419 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 420 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 421 |       case CT_INT: | 
 | 422 |         /* scan an integer as if by strtoimax/strtoumax */ | 
 | 423 |         if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1) | 
 | 424 |           width = sizeof(buf) / sizeof(*buf) - 1; | 
 | 425 |         flags |= SIGNOK | NDIGITS | NZDIGITS; | 
 | 426 |         for (p = buf; width; width--) { | 
 | 427 |           c = __fgetwc_unlock(fp); | 
 | 428 |           /* | 
 | 429 |            * Switch on the character; `goto ok' | 
 | 430 |            * if we accept it as a part of number. | 
 | 431 |            */ | 
 | 432 |           switch (c) { | 
 | 433 |             /* | 
 | 434 |              * The digit 0 is always legal, but is | 
 | 435 |              * special.  For %i conversions, if no | 
 | 436 |              * digits (zero or nonzero) have been | 
 | 437 |              * scanned (only signs), we will have | 
 | 438 |              * base==0.  In that case, we should set | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 439 |              * it to 8 and enable 0b/0x prefixing. | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 440 |              * Also, if we have not scanned zero digits | 
 | 441 |              * before this, do not turn off prefixing | 
 | 442 |              * (someone else will turn it off if we | 
 | 443 |              * have scanned any nonzero digits). | 
 | 444 |              */ | 
 | 445 |             case '0': | 
 | 446 |               if (base == 0) { | 
 | 447 |                 base = 8; | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 448 |                 flags |= PFBOK | PFXOK; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 449 |               } | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 450 |               if (flags & NZDIGITS) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 451 |                 flags &= ~(SIGNOK | NZDIGITS | NDIGITS); | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 452 |               } else { | 
 | 453 |                 flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS); | 
 | 454 |               } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 455 |               goto ok; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 456 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 457 |             /* 1 through 7 always legal */ | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 458 |             case 'B': | 
 | 459 |             case 'b': | 
 | 460 |               // Is this 'b' potentially part of an "0b" prefix? | 
 | 461 |               if ((flags & PFBOK) && p == buf + 1 + !!(flags & HAVESIGN)) { | 
 | 462 |                 base = 2; | 
 | 463 |                 flags &= ~PFBOK; | 
 | 464 |                 goto ok; | 
 | 465 |               } | 
 | 466 |               // No? Fall through and see if it's a hex digit instead then... | 
 | 467 |               __BIONIC_FALLTHROUGH; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 468 |             case '1': | 
 | 469 |             case '2': | 
 | 470 |             case '3': | 
 | 471 |             case '4': | 
 | 472 |             case '5': | 
 | 473 |             case '6': | 
 | 474 |             case '7': | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 475 |             case '8': | 
 | 476 |             case '9': | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 477 |             case 'A': | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 478 |             case 'C': | 
 | 479 |             case 'D': | 
 | 480 |             case 'E': | 
 | 481 |             case 'F': | 
 | 482 |             case 'a': | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 483 |             case 'c': | 
 | 484 |             case 'd': | 
 | 485 |             case 'e': | 
 | 486 |             case 'f': | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 487 |               if (base == 0) base = 10; | 
 | 488 |               if (base != 16 && (int)(c - '0') >= base) break; /* not legal here */ | 
 | 489 |               flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 490 |               goto ok; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 491 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 492 |             /* sign ok only as first character */ | 
 | 493 |             case '+': | 
 | 494 |             case '-': | 
 | 495 |               if (flags & SIGNOK) { | 
 | 496 |                 flags &= ~SIGNOK; | 
 | 497 |                 flags |= HAVESIGN; | 
 | 498 |                 goto ok; | 
 | 499 |               } | 
 | 500 |               break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 501 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 502 |             /* | 
 | 503 |              * x ok iff flag still set and 2nd char (or | 
 | 504 |              * 3rd char if we have a sign). | 
 | 505 |              */ | 
 | 506 |             case 'x': | 
 | 507 |             case 'X': | 
 | 508 |               if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) { | 
 | 509 |                 base = 16; /* if %i */ | 
 | 510 |                 flags &= ~PFXOK; | 
 | 511 |                 goto ok; | 
 | 512 |               } | 
 | 513 |               break; | 
 | 514 |           } | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 515 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 516 |           /* | 
 | 517 |            * If we got here, c is not a legal character | 
 | 518 |            * for a number.  Stop accumulating digits. | 
 | 519 |            */ | 
 | 520 |           if (c != WEOF) __ungetwc(c, fp); | 
 | 521 |           break; | 
 | 522 |         ok: | 
 | 523 |           /* | 
 | 524 |            * c is legal: store it and look at the next. | 
 | 525 |            */ | 
 | 526 |           *p++ = (wchar_t)c; | 
 | 527 |         } | 
 | 528 |         /* | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 529 |          * If we had only a sign, it is no good; push back the sign. | 
 | 530 |          * If the number was `[-+]0[BbXx]`, push back and treat it | 
 | 531 |          * as `[-+]0`. | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 532 |          */ | 
 | 533 |         if (flags & NDIGITS) { | 
 | 534 |           if (p > buf) __ungetwc(*--p, fp); | 
 | 535 |           goto match_failure; | 
 | 536 |         } | 
 | 537 |         c = p[-1]; | 
| Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 538 |         if ((base == 2 && (c == 'b' || c == 'B')) || c == 'x' || c == 'X') { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 539 |           --p; | 
 | 540 |           __ungetwc(c, fp); | 
 | 541 |         } | 
 | 542 |         if ((flags & SUPPRESS) == 0) { | 
 | 543 |           uintmax_t res; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 544 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 545 |           *p = '\0'; | 
 | 546 |           if (flags & UNSIGNED) | 
 | 547 |             res = wcstoimax(buf, NULL, base); | 
 | 548 |           else | 
 | 549 |             res = wcstoumax(buf, NULL, base); | 
 | 550 |           if (flags & POINTER) | 
 | 551 |             *va_arg(ap, void**) = (void*)(uintptr_t)res; | 
 | 552 |           else if (flags & MAXINT) | 
 | 553 |             *va_arg(ap, intmax_t*) = res; | 
 | 554 |           else if (flags & LLONG) | 
 | 555 |             *va_arg(ap, long long*) = res; | 
 | 556 |           else if (flags & SIZEINT) | 
 | 557 |             *va_arg(ap, ssize_t*) = res; | 
 | 558 |           else if (flags & PTRINT) | 
 | 559 |             *va_arg(ap, ptrdiff_t*) = res; | 
 | 560 |           else if (flags & LONG) | 
 | 561 |             *va_arg(ap, long*) = res; | 
 | 562 |           else if (flags & SHORT) | 
 | 563 |             *va_arg(ap, short*) = res; | 
 | 564 |           else if (flags & SHORTSHORT) | 
 | 565 |             *va_arg(ap, signed char*) = res; | 
 | 566 |           else | 
 | 567 |             *va_arg(ap, int*) = res; | 
 | 568 |           nassigned++; | 
 | 569 |         } | 
 | 570 |         nread += p - buf; | 
 | 571 |         nconversions++; | 
 | 572 |         break; | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 573 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 574 |       case CT_FLOAT: | 
 | 575 |         /* scan a floating point number as if by strtod */ | 
 | 576 |         if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1) | 
 | 577 |           width = sizeof(buf) / sizeof(*buf) - 1; | 
 | 578 |         if ((width = wparsefloat(fp, buf, buf + width)) == 0) goto match_failure; | 
 | 579 |         if ((flags & SUPPRESS) == 0) { | 
 | 580 |           if (flags & LONGDBL) { | 
 | 581 |             long double res = wcstold(buf, &p); | 
 | 582 |             *va_arg(ap, long double*) = res; | 
 | 583 |           } else if (flags & LONG) { | 
 | 584 |             double res = wcstod(buf, &p); | 
 | 585 |             *va_arg(ap, double*) = res; | 
 | 586 |           } else { | 
 | 587 |             float res = wcstof(buf, &p); | 
 | 588 |             *va_arg(ap, float*) = res; | 
 | 589 |           } | 
 | 590 |           if (p - buf != (ptrdiff_t)width) abort(); | 
 | 591 |           nassigned++; | 
 | 592 |         } | 
 | 593 |         nread += width; | 
 | 594 |         nconversions++; | 
 | 595 |         break; | 
 | 596 |     } | 
 | 597 |   } | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 598 | input_failure: | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 599 |   return (nconversions != 0 ? nassigned : EOF); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 600 | match_failure: | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 601 |   return (nassigned); | 
| Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 602 | } | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 603 | #pragma GCC diagnostic pop |