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++; |
| 226 | while (is_digit(c)) { |
| 227 | APPEND_DIGIT(size, c); |
| 228 | c = *fmt++; |
| 229 | } |
| 230 | flags |= w_to_flag(size, fast); |
| 231 | goto reswitch; |
| 232 | } |
| 233 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 234 | case 'X': |
| 235 | case 'x': |
| 236 | flags |= PFXOK; /* enable 0x prefixing */ |
| 237 | c = CT_INT; |
| 238 | flags |= UNSIGNED; |
| 239 | base = 16; |
| 240 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 241 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 242 | case 'e': |
| 243 | case 'E': |
| 244 | case 'f': |
| 245 | case 'F': |
| 246 | case 'g': |
| 247 | case 'G': |
| 248 | case 'a': |
| 249 | case 'A': |
| 250 | c = CT_FLOAT; |
| 251 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 253 | case 's': |
| 254 | c = CT_STRING; |
| 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 '[': |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 258 | ccl = fmt; |
| 259 | if (*fmt == '^') fmt++; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 260 | if (*fmt == ']') fmt++; |
| 261 | while (*fmt != '\0' && *fmt != ']') fmt++; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 262 | fmt++; |
| 263 | flags |= NOSKIP; |
| 264 | c = CT_CCL; |
| 265 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 266 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 267 | case 'c': |
| 268 | flags |= NOSKIP; |
| 269 | c = CT_CHAR; |
| 270 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 271 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 272 | case 'p': /* pointer format is like hex */ |
| 273 | flags |= POINTER | PFXOK; |
| 274 | c = CT_INT; |
| 275 | flags |= UNSIGNED; |
| 276 | base = 16; |
| 277 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 278 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 279 | case 'n': |
| 280 | nconversions++; |
| 281 | if (flags & SUPPRESS) continue; |
| 282 | if (flags & SHORTSHORT) |
| 283 | *va_arg(ap, signed char*) = nread; |
| 284 | else if (flags & SHORT) |
| 285 | *va_arg(ap, short*) = nread; |
| 286 | else if (flags & LONG) |
| 287 | *va_arg(ap, long*) = nread; |
| 288 | else if (flags & SIZEINT) |
| 289 | *va_arg(ap, ssize_t*) = nread; |
| 290 | else if (flags & PTRINT) |
| 291 | *va_arg(ap, ptrdiff_t*) = nread; |
| 292 | else if (flags & LLONG) |
| 293 | *va_arg(ap, long long*) = nread; |
| 294 | else if (flags & MAXINT) |
| 295 | *va_arg(ap, intmax_t*) = nread; |
| 296 | else |
| 297 | *va_arg(ap, int*) = nread; |
| 298 | continue; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 299 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 300 | /* |
| 301 | * Disgusting backwards compatibility hacks. XXX |
| 302 | */ |
| 303 | case '\0': /* compat */ |
| 304 | return (EOF); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 305 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 306 | default: /* compat */ |
| 307 | if (iswupper(c)) flags |= LONG; |
| 308 | c = CT_INT; |
| 309 | base = 10; |
| 310 | break; |
| 311 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 312 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 313 | /* |
| 314 | * Consume leading white space, except for formats |
| 315 | * that suppress this. |
| 316 | */ |
| 317 | if ((flags & NOSKIP) == 0) { |
| 318 | while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi)) nread++; |
| 319 | if (wi == WEOF) goto input_failure; |
| 320 | __ungetwc(wi, fp); |
| 321 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 322 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Do the conversion. |
| 325 | */ |
| 326 | switch (c) { |
| 327 | case CT_CHAR: |
| 328 | /* scan arbitrary characters (sets NOSKIP) */ |
| 329 | if (width == 0) width = 1; |
| 330 | if (flags & LONG) { |
| 331 | if (!(flags & SUPPRESS)) p = va_arg(ap, wchar_t*); |
| 332 | n = 0; |
| 333 | while (width-- != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) { |
| 334 | if (!(flags & SUPPRESS)) *p++ = (wchar_t)wi; |
| 335 | n++; |
| 336 | } |
| 337 | if (n == 0) goto input_failure; |
| 338 | nread += n; |
| 339 | if (!(flags & SUPPRESS)) nassigned++; |
| 340 | } else { |
| 341 | if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*); |
| 342 | n = 0; |
| 343 | memset(&mbs, 0, sizeof(mbs)); |
| 344 | while (width != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) { |
| 345 | if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) { |
| 346 | nconv = wcrtomb(mbp, wi, &mbs); |
| 347 | if (nconv == (size_t)-1) goto input_failure; |
| 348 | } else { |
| 349 | nconv = wcrtomb(mbbuf, wi, &mbs); |
| 350 | if (nconv == (size_t)-1) goto input_failure; |
| 351 | if (nconv > width) { |
| 352 | __ungetwc(wi, fp); |
| 353 | break; |
| 354 | } |
| 355 | if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv); |
| 356 | } |
| 357 | if (!(flags & SUPPRESS)) mbp += nconv; |
| 358 | width -= nconv; |
| 359 | n++; |
| 360 | } |
| 361 | if (n == 0) goto input_failure; |
| 362 | nread += n; |
| 363 | if (!(flags & SUPPRESS)) nassigned++; |
| 364 | } |
| 365 | nconversions++; |
| 366 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 367 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 368 | case CT_CCL: |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 369 | case CT_STRING: |
| 370 | // CT_CCL: scan a (nonempty) character class (sets NOSKIP). |
| 371 | // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP. |
| 372 | if (width == 0) width = (size_t)~0; // 'infinity'. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 373 | if ((flags & SUPPRESS) && (flags & LONG)) { |
| 374 | n = 0; |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 375 | 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] | 376 | if (wi != WEOF) __ungetwc(wi, fp); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 377 | } else if (flags & LONG) { |
| 378 | p0 = p = va_arg(ap, wchar_t*); |
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)))) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 380 | *p++ = (wchar_t)wi; |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 381 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 382 | if (wi != WEOF) __ungetwc(wi, fp); |
| 383 | n = p - p0; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 384 | } else { |
| 385 | if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*); |
| 386 | n = 0; |
| 387 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 388 | 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] | 389 | if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) { |
| 390 | nconv = wcrtomb(mbp, wi, &mbs); |
| 391 | if (nconv == (size_t)-1) goto input_failure; |
| 392 | } else { |
| 393 | nconv = wcrtomb(mbbuf, wi, &mbs); |
| 394 | if (nconv == (size_t)-1) goto input_failure; |
| 395 | if (nconv > width) break; |
| 396 | if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv); |
| 397 | } |
| 398 | if (!(flags & SUPPRESS)) mbp += nconv; |
| 399 | width -= nconv; |
| 400 | n++; |
| 401 | } |
| 402 | if (wi != WEOF) __ungetwc(wi, fp); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 403 | } |
| 404 | if (c == CT_CCL && n == 0) goto match_failure; |
| 405 | if (!(flags & SUPPRESS)) { |
| 406 | if (flags & LONG) { |
| 407 | *p = L'\0'; |
| 408 | } else { |
| 409 | *mbp = '\0'; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 410 | } |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 411 | ++nassigned; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 412 | } |
| 413 | nread += n; |
| 414 | nconversions++; |
| 415 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 416 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 417 | case CT_INT: |
| 418 | /* scan an integer as if by strtoimax/strtoumax */ |
| 419 | if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1) |
| 420 | width = sizeof(buf) / sizeof(*buf) - 1; |
| 421 | flags |= SIGNOK | NDIGITS | NZDIGITS; |
| 422 | for (p = buf; width; width--) { |
| 423 | c = __fgetwc_unlock(fp); |
| 424 | /* |
| 425 | * Switch on the character; `goto ok' |
| 426 | * if we accept it as a part of number. |
| 427 | */ |
| 428 | switch (c) { |
| 429 | /* |
| 430 | * The digit 0 is always legal, but is |
| 431 | * special. For %i conversions, if no |
| 432 | * digits (zero or nonzero) have been |
| 433 | * scanned (only signs), we will have |
| 434 | * base==0. In that case, we should set |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 435 | * it to 8 and enable 0b/0x prefixing. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 436 | * Also, if we have not scanned zero digits |
| 437 | * before this, do not turn off prefixing |
| 438 | * (someone else will turn it off if we |
| 439 | * have scanned any nonzero digits). |
| 440 | */ |
| 441 | case '0': |
| 442 | if (base == 0) { |
| 443 | base = 8; |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 444 | flags |= PFBOK | PFXOK; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 445 | } |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 446 | if (flags & NZDIGITS) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 447 | flags &= ~(SIGNOK | NZDIGITS | NDIGITS); |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 448 | } else { |
| 449 | flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS); |
| 450 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 451 | goto ok; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 452 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 453 | /* 1 through 7 always legal */ |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 454 | case 'B': |
| 455 | case 'b': |
| 456 | // Is this 'b' potentially part of an "0b" prefix? |
| 457 | if ((flags & PFBOK) && p == buf + 1 + !!(flags & HAVESIGN)) { |
| 458 | base = 2; |
| 459 | flags &= ~PFBOK; |
| 460 | goto ok; |
| 461 | } |
| 462 | // No? Fall through and see if it's a hex digit instead then... |
| 463 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 464 | case '1': |
| 465 | case '2': |
| 466 | case '3': |
| 467 | case '4': |
| 468 | case '5': |
| 469 | case '6': |
| 470 | case '7': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 471 | case '8': |
| 472 | case '9': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 473 | case 'A': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 474 | case 'C': |
| 475 | case 'D': |
| 476 | case 'E': |
| 477 | case 'F': |
| 478 | case 'a': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 479 | case 'c': |
| 480 | case 'd': |
| 481 | case 'e': |
| 482 | case 'f': |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 483 | if (base == 0) base = 10; |
| 484 | if (base != 16 && (int)(c - '0') >= base) break; /* not legal here */ |
| 485 | flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 486 | goto ok; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 487 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 488 | /* sign ok only as first character */ |
| 489 | case '+': |
| 490 | case '-': |
| 491 | if (flags & SIGNOK) { |
| 492 | flags &= ~SIGNOK; |
| 493 | flags |= HAVESIGN; |
| 494 | goto ok; |
| 495 | } |
| 496 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 497 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 498 | /* |
| 499 | * x ok iff flag still set and 2nd char (or |
| 500 | * 3rd char if we have a sign). |
| 501 | */ |
| 502 | case 'x': |
| 503 | case 'X': |
| 504 | if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) { |
| 505 | base = 16; /* if %i */ |
| 506 | flags &= ~PFXOK; |
| 507 | goto ok; |
| 508 | } |
| 509 | break; |
| 510 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 511 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 512 | /* |
| 513 | * If we got here, c is not a legal character |
| 514 | * for a number. Stop accumulating digits. |
| 515 | */ |
| 516 | if (c != WEOF) __ungetwc(c, fp); |
| 517 | break; |
| 518 | ok: |
| 519 | /* |
| 520 | * c is legal: store it and look at the next. |
| 521 | */ |
| 522 | *p++ = (wchar_t)c; |
| 523 | } |
| 524 | /* |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 525 | * If we had only a sign, it is no good; push back the sign. |
| 526 | * If the number was `[-+]0[BbXx]`, push back and treat it |
| 527 | * as `[-+]0`. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 528 | */ |
| 529 | if (flags & NDIGITS) { |
| 530 | if (p > buf) __ungetwc(*--p, fp); |
| 531 | goto match_failure; |
| 532 | } |
| 533 | c = p[-1]; |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 534 | if ((base == 2 && (c == 'b' || c == 'B')) || c == 'x' || c == 'X') { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 535 | --p; |
| 536 | __ungetwc(c, fp); |
| 537 | } |
| 538 | if ((flags & SUPPRESS) == 0) { |
| 539 | uintmax_t res; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 540 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 541 | *p = '\0'; |
| 542 | if (flags & UNSIGNED) |
| 543 | res = wcstoimax(buf, NULL, base); |
| 544 | else |
| 545 | res = wcstoumax(buf, NULL, base); |
| 546 | if (flags & POINTER) |
| 547 | *va_arg(ap, void**) = (void*)(uintptr_t)res; |
| 548 | else if (flags & MAXINT) |
| 549 | *va_arg(ap, intmax_t*) = res; |
| 550 | else if (flags & LLONG) |
| 551 | *va_arg(ap, long long*) = res; |
| 552 | else if (flags & SIZEINT) |
| 553 | *va_arg(ap, ssize_t*) = res; |
| 554 | else if (flags & PTRINT) |
| 555 | *va_arg(ap, ptrdiff_t*) = res; |
| 556 | else if (flags & LONG) |
| 557 | *va_arg(ap, long*) = res; |
| 558 | else if (flags & SHORT) |
| 559 | *va_arg(ap, short*) = res; |
| 560 | else if (flags & SHORTSHORT) |
| 561 | *va_arg(ap, signed char*) = res; |
| 562 | else |
| 563 | *va_arg(ap, int*) = res; |
| 564 | nassigned++; |
| 565 | } |
| 566 | nread += p - buf; |
| 567 | nconversions++; |
| 568 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 569 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 570 | case CT_FLOAT: |
| 571 | /* scan a floating point number as if by strtod */ |
| 572 | if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1) |
| 573 | width = sizeof(buf) / sizeof(*buf) - 1; |
| 574 | if ((width = wparsefloat(fp, buf, buf + width)) == 0) goto match_failure; |
| 575 | if ((flags & SUPPRESS) == 0) { |
| 576 | if (flags & LONGDBL) { |
| 577 | long double res = wcstold(buf, &p); |
| 578 | *va_arg(ap, long double*) = res; |
| 579 | } else if (flags & LONG) { |
| 580 | double res = wcstod(buf, &p); |
| 581 | *va_arg(ap, double*) = res; |
| 582 | } else { |
| 583 | float res = wcstof(buf, &p); |
| 584 | *va_arg(ap, float*) = res; |
| 585 | } |
| 586 | if (p - buf != (ptrdiff_t)width) abort(); |
| 587 | nassigned++; |
| 588 | } |
| 589 | nread += width; |
| 590 | nconversions++; |
| 591 | break; |
| 592 | } |
| 593 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 594 | input_failure: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 595 | return (nconversions != 0 ? nassigned : EOF); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 596 | match_failure: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 597 | return (nassigned); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 598 | } |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 599 | #pragma GCC diagnostic pop |