diff options
author | Jakub Jelinek <jakub@redhat.com> | 2007-07-12 18:26:36 +0000 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2007-07-12 18:26:36 +0000 |
commit | 0ecb606cb6cf65de1d9fc8a919bceb4be476c602 (patch) | |
tree | 2ea1f8305970753e4a657acb2ccc15ca3eec8e2c /stdio-common/vfscanf.c | |
parent | 7d58530341304d403a6626d7f7a1913165fe2f32 (diff) | |
download | glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.gz glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.xz glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.zip |
2.5-18.1
Diffstat (limited to 'stdio-common/vfscanf.c')
-rw-r--r-- | stdio-common/vfscanf.c | 183 |
1 files changed, 97 insertions, 86 deletions
diff --git a/stdio-common/vfscanf.c b/stdio-common/vfscanf.c index c641d2d371..6671602291 100644 --- a/stdio-common/vfscanf.c +++ b/stdio-common/vfscanf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991-2002, 2003, 2004 Free Software Foundation, Inc. +/* Copyright (C) 1991-2006, 2007 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -144,11 +144,6 @@ if (done == 0) done = EOF; \ goto errout; \ } while (0) -#define memory_error() do { \ - __set_errno (ENOMEM); \ - done = EOF; \ - goto errout; \ - } while (0) #define ARGCHECK(s, format) \ do \ { \ @@ -178,18 +173,12 @@ Return the number of assignments made, or -1 for an input error. */ #ifdef COMPILE_WSCANF int -_IO_vfwscanf (s, format, argptr, errp) - _IO_FILE *s; - const wchar_t *format; - _IO_va_list argptr; - int *errp; +_IO_vfwscanf (_IO_FILE *s, const wchar_t *format, _IO_va_list argptr, + int *errp) #else int -_IO_vfscanf (s, format, argptr, errp) - _IO_FILE *s; - const char *format; - _IO_va_list argptr; - int *errp; +_IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr, + int *errp) #endif { va_list arg; @@ -360,7 +349,7 @@ _IO_vfscanf (s, format, argptr, errp) do { c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); else if (c != (unsigned char) *f++) { @@ -388,7 +377,7 @@ _IO_vfscanf (s, format, argptr, errp) c = inchar (); /* Characters other than format specs must just match. */ - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); /* We saw white space char as the last character in the format @@ -396,12 +385,12 @@ _IO_vfscanf (s, format, argptr, errp) if (skip_space) { while (ISSPACE (c)) - if (inchar () == EOF) + if (__builtin_expect (inchar () == EOF, 0)) input_error (); skip_space = 0; } - if (c != fc) + if (__builtin_expect (c != fc, 0)) { ungetc (c, s); conv_error (); @@ -537,7 +526,7 @@ _IO_vfscanf (s, format, argptr, errp) } /* End of the format string? */ - if (*f == L_('\0')) + if (__builtin_expect (*f == L_('\0'), 0)) conv_error (); /* Find the conversion specifier. */ @@ -549,7 +538,7 @@ _IO_vfscanf (s, format, argptr, errp) int save_errno = errno; errno = 0; do - if (inchar () == EOF && errno == EINTR) + if (__builtin_expect (inchar () == EOF && errno == EINTR, 0)) input_error (); while (ISSPACE (c)); errno = save_errno; @@ -561,9 +550,9 @@ _IO_vfscanf (s, format, argptr, errp) { case L_('%'): /* Must match a literal '%'. */ c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); - if (c != fc) + if (__builtin_expect (c != fc, 0)) { ungetc_not_eof (c, s); conv_error (); @@ -629,7 +618,7 @@ _IO_vfscanf (s, format, argptr, errp) } c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); if (width == -1) @@ -645,7 +634,7 @@ _IO_vfscanf (s, format, argptr, errp) size_t n; n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state); - if (n == (size_t) -1) + if (__builtin_expect (n == (size_t) -1, 0)) /* No valid wide character. */ input_error (); @@ -680,7 +669,7 @@ _IO_vfscanf (s, format, argptr, errp) } c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); #ifdef COMPILE_WSCANF @@ -718,14 +707,14 @@ _IO_vfscanf (s, format, argptr, errp) { /* Possibly correct character, just not enough input. */ - if (inchar () == EOF) + if (__builtin_expect (inchar () == EOF, 0)) encode_error (); buf[0] = c; continue; } - if (n != 1) + if (__builtin_expect (n != 1, 0)) encode_error (); /* We have a match. */ @@ -769,7 +758,7 @@ _IO_vfscanf (s, format, argptr, errp) STRING_ARG (str, char); c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); #ifdef COMPILE_WSCANF @@ -832,7 +821,7 @@ _IO_vfscanf (s, format, argptr, errp) n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state); - if (n == (size_t) -1) + if (__builtin_expect (n == (size_t) -1, 0)) encode_error (); assert (n <= MB_CUR_MAX); @@ -940,7 +929,7 @@ _IO_vfscanf (s, format, argptr, errp) STRING_ARG (wstr, wchar_t); c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); #ifndef COMPILE_WSCANF @@ -1015,14 +1004,14 @@ _IO_vfscanf (s, format, argptr, errp) { /* Possibly correct character, just not enough input. */ - if (inchar () == EOF) + if (__builtin_expect (inchar () == EOF, 0)) encode_error (); buf[0] = c; continue; } - if (n != 1) + if (__builtin_expect (n != 1, 0)) encode_error (); /* We have a match. */ @@ -1117,7 +1106,7 @@ _IO_vfscanf (s, format, argptr, errp) number: c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); /* Check for a sign. */ @@ -1156,7 +1145,7 @@ _IO_vfscanf (s, format, argptr, errp) if (base == 0) base = 10; - if (base == 10 && (flags & I18N) != 0) + if (base == 10 && __builtin_expect ((flags & I18N) != 0, 0)) { int from_level; int to_level; @@ -1516,12 +1505,14 @@ _IO_vfscanf (s, format, argptr, errp) { /* There was no number. If we are supposed to read a pointer we must recognize "(nil)" as well. */ - if (wpsize == 0 && read_pointer && (width < 0 || width >= 0) - && c == '(' - && TOLOWER (inchar ()) == L_('n') - && TOLOWER (inchar ()) == L_('i') - && TOLOWER (inchar ()) == L_('l') - && inchar () == L_(')')) + if (__builtin_expect (wpsize == 0 + && read_pointer + && (width < 0 || width >= 0) + && c == '(' + && TOLOWER (inchar ()) == L_('n') + && TOLOWER (inchar ()) == L_('i') + && TOLOWER (inchar ()) == L_('l') + && inchar () == L_(')'), 1)) /* We must produce the value of a NULL pointer. A single '0' digit is enough. */ ADDW (L_('0')); @@ -1554,7 +1545,7 @@ _IO_vfscanf (s, format, argptr, errp) else num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP); } - if (wp == tw) + if (__builtin_expect (wp == tw, 0)) conv_error (); if (!(flags & SUPPRESS)) @@ -1599,21 +1590,23 @@ _IO_vfscanf (s, format, argptr, errp) case L_('a'): case L_('A'): c = inchar (); - if (c == EOF) + if (__builtin_expect (c == EOF, 0)) input_error (); + got_dot = got_e = 0; + /* Check for a sign. */ if (c == L_('-') || c == L_('+')) { negative = c == L_('-'); - if (width == 0 || inchar () == EOF) + if (__builtin_expect (width == 0 || inchar () == EOF, 0)) /* EOF is only an input error before we read any chars. */ conv_error (); if (! ISDIGIT (c) && TOLOWER (c) != L_('i') && TOLOWER (c) != L_('n')) { #ifdef COMPILE_WSCANF - if (c != decimal) + if (__builtin_expect (c != decimal, 0)) { /* This is no valid number. */ ungetc (c, s); @@ -1629,17 +1622,16 @@ _IO_vfscanf (s, format, argptr, errp) const char *cmpp = decimal; int avail = width > 0 ? width : INT_MAX; - while ((unsigned char) *cmpp == c && avail > 0) + while ((unsigned char) *cmpp == c && avail-- > 0) if (*++cmpp == '\0') break; else { if (inchar () == EOF) break; - --avail; } - if (*cmpp != '\0') + if (__builtin_expect (*cmpp != '\0', 0)) { /* This is no valid number. */ while (1) @@ -1652,6 +1644,17 @@ _IO_vfscanf (s, format, argptr, errp) conv_error (); } + else + { + /* Add all the characters. */ + for (cmpp = decimal; *cmpp != '\0'; ++cmpp) + ADDW ((unsigned char) *cmpp); + if (width > 0) + width = avail; + got_dot = 1; + + c = inchar (); + } if (width > 0) width = avail; #endif @@ -1667,12 +1670,16 @@ _IO_vfscanf (s, format, argptr, errp) { /* Maybe "nan". */ ADDW (c); - if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('a')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('a'), 0)) conv_error (); if (width > 0) --width; ADDW (c); - if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('n'), 0)) conv_error (); if (width > 0) --width; @@ -1684,12 +1691,16 @@ _IO_vfscanf (s, format, argptr, errp) { /* Maybe "inf" or "infinity". */ ADDW (c); - if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('n'), 0)) conv_error (); if (width > 0) --width; ADDW (c); - if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('f')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('f'), 0)) conv_error (); if (width > 0) --width; @@ -1703,26 +1714,30 @@ _IO_vfscanf (s, format, argptr, errp) --width; /* Now we have to read the rest as well. */ ADDW (c); - if (width == 0 || inchar () == EOF - || TOLOWER (c) != L_('n')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('n'), 0)) conv_error (); if (width > 0) --width; ADDW (c); - if (width == 0 || inchar () == EOF - || TOLOWER (c) != L_('i')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('i'), 0)) conv_error (); if (width > 0) --width; ADDW (c); - if (width == 0 || inchar () == EOF - || TOLOWER (c) != L_('t')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('t'), 0)) conv_error (); if (width > 0) --width; ADDW (c); - if (width == 0 || inchar () == EOF - || TOLOWER (c) != L_('y')) + if (__builtin_expect (width == 0 + || inchar () == EOF + || TOLOWER (c) != L_('y'), 0)) conv_error (); if (width > 0) --width; @@ -1759,7 +1774,6 @@ _IO_vfscanf (s, format, argptr, errp) } } - got_dot = got_e = 0; do { if (ISDIGIT (c)) @@ -1873,20 +1887,20 @@ _IO_vfscanf (s, format, argptr, errp) /* Have we read any character? If we try to read a number in hexadecimal notation and we have read only the `0x' - prefix or no exponent this is an error. */ - if (wpsize == 0 || (is_hexa && (wpsize == 2 || ! got_e))) + prefix this is an error. */ + if (__builtin_expect (wpsize == 0 || (is_hexa && wpsize == 2), 0)) conv_error (); scan_float: /* Convert the number. */ ADDW (L_('\0')); - if (flags & LONGDBL) + if ((flags & LONGDBL) && !__ldbl_is_dbl) { long double d = __strtold_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (long double *) = negative ? -d : d; } - else if (flags & LONG) + else if (flags & (LONG | LONGDBL)) { double d = __strtod_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) @@ -1899,7 +1913,7 @@ _IO_vfscanf (s, format, argptr, errp) *ARG (float *) = negative ? -d : d; } - if (tw == wp) + if (__builtin_expect (tw == wp, 0)) conv_error (); if (!(flags & SUPPRESS)) @@ -1939,7 +1953,7 @@ _IO_vfscanf (s, format, argptr, errp) while ((fc = *f++) != L'\0' && fc != L']'); - if (fc == L'\0') + if (__builtin_expect (fc == L'\0', 0)) conv_error (); wp = (wchar_t *) f - 1; #else @@ -1975,7 +1989,7 @@ _IO_vfscanf (s, format, argptr, errp) /* Add the character to the flag map. */ wp[fc] = 1; - if (fc == '\0') + if (__builtin_expect (fc == '\0', 0)) conv_error(); #endif @@ -1983,7 +1997,7 @@ _IO_vfscanf (s, format, argptr, errp) { size_t now = read_in; #ifdef COMPILE_WSCANF - if (inchar () == WEOF) + if (__builtin_expect (inchar () == WEOF, 0)) input_error (); do @@ -2088,7 +2102,7 @@ _IO_vfscanf (s, format, argptr, errp) size_t cnt = 0; mbstate_t cstate; - if (inchar () == EOF) + if (__builtin_expect (inchar () == EOF, 0)) input_error (); memset (&cstate, '\0', sizeof (cstate)); @@ -2165,13 +2179,13 @@ _IO_vfscanf (s, format, argptr, errp) } while (inchar () != EOF); - if (cnt != 0) + if (__builtin_expect (cnt != 0, 0)) /* We stopped in the middle of recognizing another character. That's a problem. */ encode_error (); #endif - if (now == read_in) + if (__builtin_expect (now == read_in, 0)) /* We haven't succesfully read any character. */ conv_error (); @@ -2196,7 +2210,7 @@ _IO_vfscanf (s, format, argptr, errp) { size_t now = read_in; - if (inchar () == EOF) + if (__builtin_expect (inchar () == EOF, 0)) input_error (); #ifdef COMPILE_WSCANF @@ -2298,7 +2312,7 @@ _IO_vfscanf (s, format, argptr, errp) } n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state); - if (n == (size_t) -1) + if (__builtin_expect (n == (size_t) -1, 0)) encode_error (); assert (n <= MB_CUR_MAX); @@ -2355,7 +2369,7 @@ _IO_vfscanf (s, format, argptr, errp) while (--width > 0 && inchar () != EOF); #endif - if (now == read_in) + if (__builtin_expect (now == read_in, 0)) /* We haven't succesfully read any character. */ conv_error (); @@ -2449,18 +2463,15 @@ __vfwscanf (FILE *s, const wchar_t *format, va_list argptr) { return _IO_vfwscanf (s, format, argptr, NULL); } +ldbl_weak_alias (__vfwscanf, vfwscanf) #else int -__vfscanf (FILE *s, const char *format, va_list argptr) +___vfscanf (FILE *s, const char *format, va_list argptr) { - return INTUSE(_IO_vfscanf) (s, format, argptr, NULL); + return _IO_vfscanf_internal (s, format, argptr, NULL); } -libc_hidden_def (__vfscanf) -#endif - -#ifdef COMPILE_WSCANF -weak_alias (__vfwscanf, vfwscanf) -#else -weak_alias (__vfscanf, vfscanf) -INTDEF(_IO_vfscanf) +ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf) +ldbl_strong_alias (___vfscanf, __vfscanf) +ldbl_hidden_def (___vfscanf, __vfscanf) +ldbl_weak_alias (___vfscanf, vfscanf) #endif |