void __ctype_init (void) { const uint16_t **bp = __libc_tsd_address (const uint16_t *, CTYPE_B); *bp = (const uint16_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_CLASS) + 128; const int32_t **up = __libc_tsd_address (const int32_t *, CTYPE_TOUPPER); *up = ((int32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TOUPPER) + 128); const int32_t **lp = __libc_tsd_address (const int32_t *, CTYPE_TOLOWER); *lp = ((int32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TOLOWER) + 128); }
/* Return monetary and numeric information about the current locale. */ struct lconv * __localeconv (void) { static struct lconv result; result.decimal_point = (char *) _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); result.thousands_sep = (char *) _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); result.grouping = (char *) _NL_CURRENT (LC_NUMERIC, GROUPING); if (*result.grouping == '\177' || *result.grouping == '\377') result.grouping = (char *) ""; result.int_curr_symbol = (char *) _NL_CURRENT (LC_MONETARY, INT_CURR_SYMBOL); result.currency_symbol = (char *) _NL_CURRENT (LC_MONETARY, CURRENCY_SYMBOL); result.mon_decimal_point = (char *) _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT); result.mon_thousands_sep = (char *) _NL_CURRENT (LC_MONETARY, MON_THOUSANDS_SEP); result.mon_grouping = (char *) _NL_CURRENT (LC_MONETARY, MON_GROUPING); if (*result.mon_grouping == '\177' || *result.mon_grouping == '\377') result.mon_grouping = (char *) ""; result.positive_sign = (char *) _NL_CURRENT (LC_MONETARY, POSITIVE_SIGN); result.negative_sign = (char *) _NL_CURRENT (LC_MONETARY, NEGATIVE_SIGN); #define INT_ELEM(member, element) \ result.member = *(char *) _NL_CURRENT (LC_MONETARY, element); \ if (result.member == '\377') result.member = CHAR_MAX INT_ELEM (int_frac_digits, INT_FRAC_DIGITS); INT_ELEM (frac_digits, FRAC_DIGITS); INT_ELEM (p_cs_precedes, P_CS_PRECEDES); INT_ELEM (p_sep_by_space, P_SEP_BY_SPACE); INT_ELEM (n_cs_precedes, N_CS_PRECEDES); INT_ELEM (n_sep_by_space, N_SEP_BY_SPACE); INT_ELEM (p_sign_posn, P_SIGN_POSN); INT_ELEM (n_sign_posn, N_SIGN_POSN); INT_ELEM (int_p_cs_precedes, INT_P_CS_PRECEDES); INT_ELEM (int_p_sep_by_space, INT_P_SEP_BY_SPACE); INT_ELEM (int_n_cs_precedes, INT_N_CS_PRECEDES); INT_ELEM (int_n_sep_by_space, INT_N_SEP_BY_SPACE); INT_ELEM (int_p_sign_posn, INT_P_SIGN_POSN); INT_ELEM (int_n_sign_posn, INT_N_SIGN_POSN); return &result; }
int _nl_parse_alt_digit (const char **strp) { const char *str = *strp; int result = -1; size_t cnt; size_t maxlen = 0; __libc_lock_lock (__libc_setlocale_lock); if (alt_digits_initialized == 0) { alt_digits_initialized = 1; if (alt_digits == NULL) alt_digits = malloc (100 * sizeof (const char *)); if (alt_digits != NULL) { const char *ptr = _NL_CURRENT (LC_TIME, ALT_DIGITS); if (alt_digits != NULL) for (cnt = 0; cnt < 100; ++cnt) { alt_digits[cnt] = ptr; /* Skip digit format. */ ptr = strchr (ptr, '\0') + 1; } } } /* Matching is not unambiguos. The alternative digits could be like I, II, III, ... and the first one is a substring of the second and third. Therefore we must keep on searching until we found the longest possible match. Note that this is not specified in the standard. */ for (cnt = 0; cnt < 100; ++cnt) { size_t len = strlen (alt_digits[cnt]); if (len > maxlen && strncmp (alt_digits[cnt], str, len) == 0) { maxlen = len; result = (int) cnt; } } __libc_lock_unlock (__libc_setlocale_lock); if (result != -1) *strp += maxlen; return result; }
const char * _nl_get_alt_digit (unsigned int number) { const char *result; __libc_lock_lock (__libc_setlocale_lock); if (alt_digits_initialized == 0) { size_t new_num_alt_digits = _NL_CURRENT_WORD (LC_TIME, _NL_TIME_NUM_ALT_DIGITS); if (alt_digits != NULL && new_num_alt_digits == 0) { free (alt_digits); alt_digits = NULL; } else if (new_num_alt_digits != 0) { if (num_alt_digits != new_num_alt_digits) alt_digits = realloc (alt_digits, (new_num_alt_digits * sizeof (const char *))); if (alt_digits == NULL) num_alt_digits = 0; else { const char *ptr = _NL_CURRENT (LC_TIME, ALT_DIGITS); size_t cnt; num_alt_digits = new_num_alt_digits; for (cnt = 0; cnt < num_alt_digits; ++cnt) { alt_digits[cnt] = ptr; /* Skip digit format. */ ptr = strchr (ptr, '\0') + 1; } } } alt_digits_initialized = 1; } result = number < num_alt_digits ? alt_digits[number] : NULL; __libc_lock_unlock (__libc_setlocale_lock); return result; }
wctype_t __wctype (const char *property) { const char *names; unsigned int result; size_t proplen = strlen (property); size_t i; names = _NL_CURRENT (LC_CTYPE, _NL_CTYPE_CLASS_NAMES); for (result = 0; ; result++) { size_t nameslen = strlen (names); if (proplen == nameslen && memcmp (property, names, proplen) == 0) break; names += nameslen + 1; if (names[0] == '\0') return 0; } i = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_CLASS_OFFSET) + result; return (wctype_t) _NL_CURRENT_DATA (LC_CTYPE)->values[i].string; }
wctrans_t __wctrans (const char *property) { const char *names; size_t cnt; size_t i; names = _NL_CURRENT (LC_CTYPE, _NL_CTYPE_MAP_NAMES); cnt = 0; while (names[0] != '\0') { if (strcmp (property, names) == 0) break; names = strchr (names, '\0') + 1; ++cnt; } if (names[0] == '\0') return 0; i = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_OFFSET) + cnt; return (wctrans_t) _NL_CURRENT_DATA (LC_CTYPE)->values[i].string; }
const char * _nl_get_alt_digit (unsigned int number) { const char *result; __libc_lock_lock (__libc_setlocale_lock); if (alt_digits_initialized == 0) { alt_digits_initialized = 1; if (alt_digits == NULL) alt_digits = malloc (100 * sizeof (const char *)); if (alt_digits != NULL) { const char *ptr = _NL_CURRENT (LC_TIME, ALT_DIGITS); size_t cnt; if (alt_digits != NULL) for (cnt = 0; cnt < 100; ++cnt) { alt_digits[cnt] = ptr; /* Skip digit format. */ ptr = strchr (ptr, '\0') + 1; } } } result = alt_digits != NULL && number < 100 ? alt_digits[number] : NULL; __libc_lock_unlock (__libc_setlocale_lock); return result; }
INT INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr, int base, int group LOCALE_PARAM_PROTO) { int negative; register unsigned LONG int cutoff; register unsigned int cutlim; register unsigned LONG int i; register const STRING_TYPE *s; register UCHAR_TYPE c; const STRING_TYPE *save, *end; int overflow; #ifdef USE_NUMBER_GROUPING # ifdef USE_IN_EXTENDED_LOCALE_MODEL struct locale_data *current = loc->__locales[LC_NUMERIC]; # endif /* The thousands character of the current locale. */ wchar_t thousands = L'\0'; /* The numeric grouping specification of the current locale, in the format described in <locale.h>. */ const char *grouping; if (group) { grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); if (*grouping <= 0 || *grouping == CHAR_MAX) grouping = NULL; else { /* Figure out the thousands separator character. */ # if defined _LIBC || defined _HAVE_BTOWC thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)); if (thousands == WEOF) thousands = L'\0'; # endif if (thousands == L'\0') grouping = NULL; } } else grouping = NULL; #endif if (base < 0 || base == 1 || base > 36) { __set_errno (EINVAL); return 0; } save = s = nptr; /* Skip white space. */ while (ISSPACE (*s)) ++s; if (*s == L_('\0')) goto noconv; /* Check for a sign. */ if (*s == L_('-')) { negative = 1; ++s; } else if (*s == L_('+')) { negative = 0; ++s; } else negative = 0; /* Recognize number prefix and if BASE is zero, figure it out ourselves. */ if (*s == L_('0')) { if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X')) { s += 2; base = 16; } else if (base == 0) base = 8; } else if (base == 0) base = 10; /* Save the pointer so we can check later if anything happened. */ save = s; #ifdef USE_NUMBER_GROUPING if (group) { /* Find the end of the digit string and check its grouping. */ end = s; for (c = *end; c != L_('\0'); c = *++end) if ((wchar_t) c != thousands && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9')) && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base)) break; if (*s == thousands) end = s; else end = correctly_grouped_prefix (s, end, thousands, grouping); } else #endif end = NULL; cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base; cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base; overflow = 0; i = 0; for (c = *s; c != L_('\0'); c = *++s) { if (s == end) break; if (c >= L_('0') && c <= L_('9')) c -= L_('0'); else if (ISALPHA (c)) c = TOUPPER (c) - L_('A') + 10; else break; if ((int) c >= base) break; /* Check for overflow. */ if (i > cutoff || (i == cutoff && c > cutlim)) overflow = 1; else { i *= (unsigned LONG int) base; i += c; } } /* Check if anything actually happened. */ if (s == save) goto noconv; /* Store in ENDPTR the address of one character past the last character we converted. */ if (endptr != NULL) *endptr = (STRING_TYPE *) s; #if !UNSIGNED /* Check for a value that is within the range of `unsigned LONG int', but outside the range of `LONG int'. */ if (overflow == 0 && i > (negative ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1 : (unsigned LONG int) STRTOL_LONG_MAX)) overflow = 1; #endif if (overflow) { __set_errno (ERANGE); #if UNSIGNED return STRTOL_ULONG_MAX; #else return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX; #endif } /* Return the result of the appropriate sign. */ return negative ? -i : i; noconv: /* We must handle a special case here: the base is 0 or 16 and the first two characters are '0' and 'x', but the rest are no hexadecimal digits. This is no error case. We return 0 and ENDPTR points to the `x`. */ if (endptr != NULL) { if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X') && save[-2] == L_('0')) *endptr = (STRING_TYPE *) &save[-1]; else /* There was no number to convert. */ *endptr = (STRING_TYPE *) nptr; } return 0L; }
# ifdef _LIBC if (nrules != 0) { uint32_t collseqval; const char *collseq = (const char *) _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC); collseqval = collseq_table_lookup (collseq, c); for (; workp < p - chars_length ;) { uint32_t start_val, end_val; /* We already compute the collation sequence value of the characters (or collating symbols). */ start_val = (uint32_t) *workp++; /* range_start */ end_val = (uint32_t) *workp++; /* range_end */ if (start_val <= collseqval && collseqval <= end_val) goto char_set_matched; } } else # endif { /* We set range_start_char at str_buf[0], range_end_char at str_buf[4], and compared char at str_buf[2]. */ str_buf[1] = 0; str_buf[2] = c; str_buf[3] = 0; str_buf[5] = 0;
/* We have to overcome some problems with this implementation. On the one hand the strfmon() function is specified in XPG4 and of course it has to follow this. But on the other hand POSIX.2 specifies some information in the LC_MONETARY category which should be used, too. Some of the information contradicts the information which can be specified in format string. */ ssize_t __vstrfmon_l (char *s, size_t maxsize, __locale_t loc, const char *format, va_list ap) { struct __locale_data *current = loc->__locales[LC_MONETARY]; _IO_strfile f; struct printf_info info; char *dest; /* Pointer so copy the output. */ const char *fmt; /* Pointer that walks through format. */ dest = s; fmt = format; /* Loop through the format-string. */ while (*fmt != '\0') { /* The floating-point value to output. */ union { double dbl; __long_double_t ldbl; } fpnum; int int_format; int print_curr_symbol; int left_prec; int left_pad; int right_prec; int group; char pad; int is_long_double; int p_sign_posn; int n_sign_posn; int sign_posn; int other_sign_posn; int left; int is_negative; int sep_by_space; int other_sep_by_space; int cs_precedes; int other_cs_precedes; const char *sign_string; const char *other_sign_string; int done; const char *currency_symbol; size_t currency_symbol_len; long int width; char *startp; const void *ptr; char space_char; /* Process all character which do not introduce a format specification. */ if (*fmt != '%') { out_char (*fmt++); continue; } /* "%%" means a single '%' character. */ if (fmt[1] == '%') { out_char (*++fmt); ++fmt; continue; } /* Defaults for formatting. */ int_format = 0; /* Use international curr. symbol */ print_curr_symbol = 1; /* Print the currency symbol. */ left_prec = -1; /* No left precision specified. */ right_prec = -1; /* No right precision specified. */ group = 1; /* Print digits grouped. */ pad = ' '; /* Fill character is <SP>. */ is_long_double = 0; /* Double argument by default. */ p_sign_posn = -2; /* This indicates whether the */ n_sign_posn = -2; /* '(' flag is given. */ width = -1; /* No width specified so far. */ left = 0; /* Right justified by default. */ /* Parse group characters. */ while (1) { switch (*++fmt) { case '=': /* Set fill character. */ pad = *++fmt; if (pad == '\0') { /* Premature EOS. */ __set_errno (EINVAL); return -1; } continue; case '^': /* Don't group digits. */ group = 0; continue; case '+': /* Use +/- for sign of number. */ if (n_sign_posn != -2) { __set_errno (EINVAL); return -1; } p_sign_posn = *_NL_CURRENT (LC_MONETARY, P_SIGN_POSN); n_sign_posn = *_NL_CURRENT (LC_MONETARY, N_SIGN_POSN); continue; case '(': /* Use ( ) for negative sign. */ if (n_sign_posn != -2) { __set_errno (EINVAL); return -1; } p_sign_posn = 0; n_sign_posn = 0; continue; case '!': /* Don't print the currency symbol. */ print_curr_symbol = 0; continue; case '-': /* Print left justified. */ left = 1; continue; default: /* Will stop the loop. */; } break; } if (isdigit (*fmt)) { /* Parse field width. */ width = to_digit (*fmt); while (isdigit (*++fmt)) { int val = to_digit (*fmt); if (width > LONG_MAX / 10 || (width == LONG_MAX && val > LONG_MAX % 10)) { __set_errno (E2BIG); return -1; } width = width * 10 + val; } /* If we don't have enough room for the demanded width we can stop now and return an error. */ if (width >= maxsize - (dest - s)) { __set_errno (E2BIG); return -1; } } /* Recognize left precision. */ if (*fmt == '#') { if (!isdigit (*++fmt)) { __set_errno (EINVAL); return -1; } left_prec = to_digit (*fmt); while (isdigit (*++fmt)) { left_prec *= 10; left_prec += to_digit (*fmt); } } /* Recognize right precision. */ if (*fmt == '.') { if (!isdigit (*++fmt)) { __set_errno (EINVAL); return -1; } right_prec = to_digit (*fmt); while (isdigit (*++fmt)) { right_prec *= 10; right_prec += to_digit (*fmt); } } /* Handle modifier. This is an extension. */ if (*fmt == 'L') { ++fmt; if (!__ldbl_is_dbl) is_long_double = 1; } /* Handle format specifier. */ char int_symbol[4]; switch (*fmt++) { case 'i': { /* Use international currency symbol. */ const char *int_curr_symbol; int_curr_symbol = _NL_CURRENT (LC_MONETARY, INT_CURR_SYMBOL); strncpy(int_symbol, int_curr_symbol, 3); int_symbol[3] = '\0'; currency_symbol_len = 3; currency_symbol = &int_symbol[0]; space_char = int_curr_symbol[3]; int_format = 1; break; } case 'n': /* Use national currency symbol. */ currency_symbol = _NL_CURRENT (LC_MONETARY, CURRENCY_SYMBOL); currency_symbol_len = strlen (currency_symbol); space_char = ' '; int_format = 0; break; default: /* Any unrecognized format is an error. */ __set_errno (EINVAL); return -1; } /* If not specified by the format string now find the values for the format specification. */ if (p_sign_posn == -2) p_sign_posn = *_NL_CURRENT (LC_MONETARY, int_format ? INT_P_SIGN_POSN : P_SIGN_POSN); if (n_sign_posn == -2) n_sign_posn = *_NL_CURRENT (LC_MONETARY, int_format ? INT_N_SIGN_POSN : N_SIGN_POSN); if (right_prec == -1) { right_prec = *_NL_CURRENT (LC_MONETARY, int_format ? INT_FRAC_DIGITS : FRAC_DIGITS); if (right_prec == '\377') right_prec = 2; } /* If we have to print the digits grouped determine how many extra characters this means. */ if (group && left_prec != -1) left_prec += __guess_grouping (left_prec, _NL_CURRENT (LC_MONETARY, MON_GROUPING), *_NL_CURRENT (LC_MONETARY, MON_THOUSANDS_SEP)); /* Now it's time to get the value. */ if (is_long_double == 1) { fpnum.ldbl = va_arg (ap, long double); is_negative = fpnum.ldbl < 0; if (is_negative) fpnum.ldbl = -fpnum.ldbl; } else {
int __printf_fphex (FILE *fp, const struct printf_info *info, const void *const *args) { /* The floating-point value to output. */ union { union ieee754_double dbl; long double ldbl; } fpnum; /* Locale-dependent representation of decimal point. */ const char *decimal; wchar_t decimalwc; /* "NaN" or "Inf" for the special cases. */ const char *special = NULL; const wchar_t *wspecial = NULL; /* Buffer for the generated number string for the mantissa. The maximal size for the mantissa is 128 bits. */ char numbuf[32]; char *numstr; char *numend; wchar_t wnumbuf[32]; wchar_t *wnumstr; wchar_t *wnumend; int negative; /* The maximal exponent of two in decimal notation has 5 digits. */ char expbuf[5]; char *expstr; wchar_t wexpbuf[5]; wchar_t *wexpstr; int expnegative; int exponent; /* Non-zero is mantissa is zero. */ int zero_mantissa; /* The leading digit before the decimal point. */ char leading; /* Precision. */ int precision = info->prec; /* Width. */ int width = info->width; /* Number of characters written. */ int done = 0; /* Nonzero if this is output on a wide character stream. */ #if __OPTION_POSIX_C_LANG_WIDE_CHAR int wide = info->wide; #else /* This should never be called on a wide-oriented stream when OPTION_POSIX_C_LANG_WIDE_CHAR is disabled, but the compiler can't be trusted to figure that out. */ const int wide = 0; #endif /* Figure out the decimal point character. */ #if __OPTION_EGLIBC_LOCALE_CODE if (info->extra == 0) { decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC); } else { decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT); decimalwc = _NL_CURRENT_WORD (LC_MONETARY, _NL_MONETARY_DECIMAL_POINT_WC); } /* The decimal point character must never be zero. */ assert (*decimal != '\0' && decimalwc != L'\0'); #else decimal = "."; decimalwc = L'.'; #endif /* Fetch the argument value. */ #ifndef __NO_LONG_DOUBLE_MATH if (info->is_long_double && sizeof (long double) > sizeof (double)) { fpnum.ldbl = *(const long double *) args[0]; /* Check for special values: not a number or infinity. */ if (__isnanl (fpnum.ldbl)) { if (isupper (info->spec)) { special = "NAN"; wspecial = L"NAN"; } else { special = "nan"; wspecial = L"nan"; } } else { if (__isinfl (fpnum.ldbl)) { if (isupper (info->spec)) { special = "INF"; wspecial = L"INF"; } else { special = "inf"; wspecial = L"inf"; } } } negative = signbit (fpnum.ldbl); } else #endif /* no long double */ { fpnum.dbl.d = *(const double *) args[0]; /* Check for special values: not a number or infinity. */ if (__isnan (fpnum.dbl.d)) { negative = fpnum.dbl.ieee.negative != 0; if (isupper (info->spec)) { special = "NAN"; wspecial = L"NAN"; } else { special = "nan"; wspecial = L"nan"; } } else { int res = __isinf (fpnum.dbl.d); if (res) { if (isupper (info->spec)) { special = "INF"; wspecial = L"INF"; } else { special = "inf"; wspecial = L"inf"; } negative = res < 0; } else negative = signbit (fpnum.dbl.d); } } if (special) { int width = info->width; if (negative || info->showsign || info->space) --width; width -= 3; if (!info->left && width > 0) PADN (' ', width); if (negative) outchar ('-'); else if (info->showsign) outchar ('+'); else if (info->space) outchar (' '); PRINT (special, wspecial, 3); if (info->left && width > 0) PADN (' ', width); return done; } if (info->is_long_double == 0 || sizeof (double) == sizeof (long double)) { /* We have 52 bits of mantissa plus one implicit digit. Since 52 bits are representable without rest using hexadecimal digits we use only the implicit digits for the number before the decimal point. */ unsigned long long int num; num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32 | fpnum.dbl.ieee.mantissa1); zero_mantissa = num == 0; if (sizeof (unsigned long int) > 6) { wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16, info->spec == 'A'); numstr = _itoa_word (num, numbuf + sizeof numbuf, 16, info->spec == 'A'); } else { wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16, info->spec == 'A'); numstr = _itoa (num, numbuf + sizeof numbuf, 16, info->spec == 'A'); } /* Fill with zeroes. */ while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t)) { *--wnumstr = L'0'; *--numstr = '0'; } leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1'; exponent = fpnum.dbl.ieee.exponent; if (exponent == 0) { if (zero_mantissa) expnegative = 0; else { /* This is a denormalized number. */ expnegative = 1; exponent = IEEE754_DOUBLE_BIAS - 1; } } else if (exponent >= IEEE754_DOUBLE_BIAS) { expnegative = 0; exponent -= IEEE754_DOUBLE_BIAS; } else { expnegative = 1; exponent = -(exponent - IEEE754_DOUBLE_BIAS); } } #ifdef PRINT_FPHEX_LONG_DOUBLE else PRINT_FPHEX_LONG_DOUBLE; #endif /* Look for trailing zeroes. */ if (! zero_mantissa) { wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]]; numend = &numbuf[sizeof numbuf / sizeof numbuf[0]]; while (wnumend[-1] == L'0') { --wnumend; --numend; } bool do_round_away = false; if (precision != -1 && precision < numend - numstr) { char last_digit = precision > 0 ? numstr[precision - 1] : leading; char next_digit = numstr[precision]; int last_digit_value = (last_digit >= 'A' && last_digit <= 'F' ? last_digit - 'A' + 10 : (last_digit >= 'a' && last_digit <= 'f' ? last_digit - 'a' + 10 : last_digit - '0')); int next_digit_value = (next_digit >= 'A' && next_digit <= 'F' ? next_digit - 'A' + 10 : (next_digit >= 'a' && next_digit <= 'f' ? next_digit - 'a' + 10 : next_digit - '0')); bool more_bits = ((next_digit_value & 7) != 0 || precision + 1 < numend - numstr); int rounding_mode = get_rounding_mode (); do_round_away = round_away (negative, last_digit_value & 1, next_digit_value >= 8, more_bits, rounding_mode); } if (precision == -1) precision = numend - numstr; else if (do_round_away) { /* Round up. */ int cnt = precision; while (--cnt >= 0) { char ch = numstr[cnt]; /* We assume that the digits and the letters are ordered like in ASCII. This is true for the rest of GNU, too. */ if (ch == '9') { wnumstr[cnt] = (wchar_t) info->spec; numstr[cnt] = info->spec; /* This is tricky, think about it! */ break; } else if (tolower (ch) < 'f') { ++numstr[cnt]; ++wnumstr[cnt]; break; } else { numstr[cnt] = '0'; wnumstr[cnt] = L'0'; } } if (cnt < 0) { /* The mantissa so far was fff...f Now increment the leading digit. Here it is again possible that we get an overflow. */ if (leading == '9') leading = info->spec; else if (tolower (leading) < 'f') ++leading; else { leading = '1'; if (expnegative) { exponent -= 4; if (exponent <= 0) { exponent = -exponent; expnegative = 0; } } else exponent += 4; } } } } else { if (precision == -1) precision = 0; numend = numstr; wnumend = wnumstr; } /* Now we can compute the exponent string. */ expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0); wexpstr = _itowa_word (exponent, wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0); /* Now we have all information to compute the size. */ width -= ((negative || info->showsign || info->space) /* Sign. */ + 2 + 1 + 0 + precision + 1 + 1 /* 0x h . hhh P ExpoSign. */ + ((expbuf + sizeof expbuf) - expstr)); /* Exponent. */ /* Count the decimal point. A special case when the mantissa or the precision is zero and the `#' is not given. In this case we must not print the decimal point. */ if (precision > 0 || info->alt) width -= wide ? 1 : strlen (decimal); if (!info->left && info->pad != '0' && width > 0) PADN (' ', width); if (negative) outchar ('-'); else if (info->showsign) outchar ('+'); else if (info->space) outchar (' '); outchar ('0'); if ('X' - 'A' == 'x' - 'a') outchar (info->spec + ('x' - 'a')); else outchar (info->spec == 'A' ? 'X' : 'x'); if (!info->left && info->pad == '0' && width > 0) PADN ('0', width); outchar (leading); if (precision > 0 || info->alt) { const wchar_t *wtmp = &decimalwc; PRINT (decimal, wtmp, wide ? 1 : strlen (decimal)); } if (precision > 0) { ssize_t tofill = precision - (numend - numstr); PRINT (numstr, wnumstr, MIN (numend - numstr, precision)); if (tofill > 0) PADN ('0', tofill); } if ('P' - 'A' == 'p' - 'a') outchar (info->spec + ('p' - 'a')); else outchar (info->spec == 'A' ? 'P' : 'p'); outchar (expnegative ? '-' : '+'); PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr); if (info->left && info->pad != '0' && width > 0) PADN (info->pad, width); return done; }
int __gconv_transliterate (struct __gconv_step *step, struct __gconv_step_data *step_data, const unsigned char *inbufstart, const unsigned char **inbufp, const unsigned char *inbufend, unsigned char **outbufstart, size_t *irreversible) { /* Find out about the locale's transliteration. */ uint_fast32_t size; const uint32_t *from_idx; const uint32_t *from_tbl; const uint32_t *to_idx; const uint32_t *to_tbl; const uint32_t *winbuf; const uint32_t *winbufend; uint_fast32_t low; uint_fast32_t high; /* The input buffer. There are actually 4-byte values. */ winbuf = (const uint32_t *) *inbufp; winbufend = (const uint32_t *) inbufend; __gconv_fct fct = step->__fct; #ifdef PTR_DEMANGLE if (step->__shlib_handle != NULL) PTR_DEMANGLE (fct); #endif /* If there is no transliteration information in the locale don't do anything and return the error. */ size = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_TAB_SIZE); if (size == 0) goto no_rules; /* Get the rest of the values. */ from_idx = (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_FROM_IDX); from_tbl = (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_FROM_TBL); to_idx = (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_TO_IDX); to_tbl = (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_TO_TBL); /* Test whether there is enough input. */ if (winbuf + 1 > winbufend) return (winbuf == winbufend ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); /* The array starting at FROM_IDX contains indeces to the string table in FROM_TBL. The indeces are sorted wrt to the strings. I.e., we are doing binary search. */ low = 0; high = size; while (low < high) { uint_fast32_t med = (low + high) / 2; uint32_t idx; int cnt; /* Compare the string at this index with the string at the current position in the input buffer. */ idx = from_idx[med]; cnt = 0; do { if (from_tbl[idx + cnt] != winbuf[cnt]) /* Does not match. */ break; ++cnt; } while (from_tbl[idx + cnt] != L'\0' && winbuf + cnt < winbufend); if (cnt > 0 && from_tbl[idx + cnt] == L'\0') { /* Found a matching input sequence. Now try to convert the possible replacements. */ uint32_t idx2 = to_idx[med]; do { /* Determine length of replacement. */ uint_fast32_t len = 0; int res; const unsigned char *toinptr; unsigned char *outptr; while (to_tbl[idx2 + len] != L'\0') ++len; /* Try this input text. */ toinptr = (const unsigned char *) &to_tbl[idx2]; outptr = *outbufstart; res = DL_CALL_FCT (fct, (step, step_data, &toinptr, (const unsigned char *) &to_tbl[idx2 + len], &outptr, NULL, 0, 0)); if (res != __GCONV_ILLEGAL_INPUT) { /* If the conversion succeeds we have to increment the input buffer. */ if (res == __GCONV_EMPTY_INPUT) { *inbufp += cnt * sizeof (uint32_t); ++*irreversible; res = __GCONV_OK; } /* Do not increment the output pointer if we could not store the entire output. */ if (res != __GCONV_FULL_OUTPUT) *outbufstart = outptr; return res; } /* Next replacement. */ idx2 += len + 1; } while (to_tbl[idx2] != L'\0'); /* Nothing found, continue searching. */ } else if (cnt > 0) /* This means that the input buffer contents matches a prefix of an entry. Since we cannot match it unless we get more input, we will tell the caller about it. */ return __GCONV_INCOMPLETE_INPUT; if (winbuf + cnt >= winbufend || from_tbl[idx + cnt] < winbuf[cnt]) low = med + 1; else high = med; } no_rules: /* Maybe the character is supposed to be ignored. */ if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE_LEN) != 0) { int n = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE_LEN); const uint32_t *ranges = (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_IGNORE); const uint32_t wc = *(const uint32_t *) (*inbufp); int i; /* Test whether there is enough input. */ if (winbuf + 1 > winbufend) return (winbuf == winbufend ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); for (i = 0; i < n; ranges += 3, ++i) if (ranges[0] <= wc && wc <= ranges[1] && (wc - ranges[0]) % ranges[2] == 0) { /* Matches the range. Ignore it. */ *inbufp += 4; ++*irreversible; return __GCONV_OK; } else if (wc < ranges[0]) /* There cannot be any other matching range since they are sorted. */ break; } /* One last chance: use the default replacement. */ if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN) != 0) { const uint32_t *default_missing = (const uint32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING); const unsigned char *toinptr = (const unsigned char *) default_missing; uint32_t len = _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_TRANSLIT_DEFAULT_MISSING_LEN); unsigned char *outptr; int res; /* Test whether there is enough input. */ if (winbuf + 1 > winbufend) return (winbuf == winbufend ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT); outptr = *outbufstart; res = DL_CALL_FCT (fct, (step, step_data, &toinptr, (const unsigned char *) (default_missing + len), &outptr, NULL, 0, 0)); if (res != __GCONV_ILLEGAL_INPUT) { /* If the conversion succeeds we have to increment the input buffer. */ if (res == __GCONV_EMPTY_INPUT) { /* This worked but is not reversible. */ ++*irreversible; *inbufp += 4; res = __GCONV_OK; } *outbufstart = outptr; return res; } } /* Haven't found a match. */ return __GCONV_ILLEGAL_INPUT; }
int internal_function __gconv_open (const char *toset, const char *fromset, __gconv_t *handle, int flags) { struct __gconv_step *steps; size_t nsteps; __gconv_t result = NULL; size_t cnt = 0; int res; int conv_flags = 0; const char *errhand; const char *ignore; struct trans_struct *trans = NULL; /* Find out whether any error handling method is specified. */ errhand = strchr (toset, '/'); if (errhand != NULL) errhand = strchr (errhand + 1, '/'); if (__builtin_expect (errhand != NULL, 1)) { if (*++errhand == '\0') errhand = NULL; else { /* Make copy without the error handling description. */ char *newtoset = (char *) alloca (errhand - toset + 1); char *tok; char *ptr; newtoset[errhand - toset] = '\0'; toset = memcpy (newtoset, toset, errhand - toset); /* Find the appropriate transliteration handlers. */ tok = strdupa (errhand); tok = __strtok_r (tok, ",", &ptr); while (tok != NULL) { if (__strcasecmp_l (tok, "TRANSLIT", &_nl_C_locobj) == 0) { /* It's the builtin transliteration handling. We only support it for working on the internal encoding. */ static const char *internal_trans_names[1] = { "INTERNAL" }; struct trans_struct *lastp = NULL; struct trans_struct *runp; for (runp = trans; runp != NULL; runp = runp->next) if (runp->trans_fct == __gconv_transliterate) break; else lastp = runp; if (runp == NULL) { struct trans_struct *newp; newp = (struct trans_struct *) alloca (sizeof (*newp)); memset (newp, '\0', sizeof (*newp)); /* We leave the `name' field zero to signal that this is an internal transliteration step. */ newp->csnames = internal_trans_names; newp->ncsnames = 1; newp->trans_fct = __gconv_transliterate; if (lastp == NULL) trans = newp; else lastp->next = newp; } } else if (__strcasecmp_l (tok, "IGNORE", &_nl_C_locobj) == 0) /* Set the flag to ignore all errors. */ conv_flags |= __GCONV_IGNORE_ERRORS; else { /* `tok' is possibly a module name. We'll see later whether we can find it. But first see that we do not already a module of this name. */ struct trans_struct *lastp = NULL; struct trans_struct *runp; for (runp = trans; runp != NULL; runp = runp->next) if (runp->name != NULL && __strcasecmp_l (tok, runp->name, &_nl_C_locobj) == 0) break; else lastp = runp; if (runp == NULL) { struct trans_struct *newp; newp = (struct trans_struct *) alloca (sizeof (*newp)); memset (newp, '\0', sizeof (*newp)); newp->name = tok; if (lastp == NULL) trans = newp; else lastp->next = newp; } } tok = __strtok_r (NULL, ",", &ptr); } } } /* For the source character set we ignore the error handler specification. XXX Is this really always the best? */ ignore = strchr (fromset, '/'); if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL && *++ignore != '\0') { char *newfromset = (char *) alloca (ignore - fromset + 1); newfromset[ignore - fromset] = '\0'; fromset = memcpy (newfromset, fromset, ignore - fromset); } /* If the string is empty define this to mean the charset of the currently selected locale. */ if (strcmp (toset, "//") == 0) { const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET); size_t len = strlen (codeset); char *dest; toset = dest = (char *) alloca (len + 3); memcpy (__mempcpy (dest, codeset, len), "//", 3); } if (strcmp (fromset, "//") == 0) { const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET); size_t len = strlen (codeset); char *dest; fromset = dest = (char *) alloca (len + 3); memcpy (__mempcpy (dest, codeset, len), "//", 3); } res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags); if (res == __GCONV_OK) { /* Find the modules. */ struct trans_struct *lastp = NULL; struct trans_struct *runp; for (runp = trans; runp != NULL; runp = runp->next) { if (runp->name == NULL || __builtin_expect (__gconv_translit_find (runp), 0) == 0) lastp = runp; else { /* This means we haven't found the module. Remove it. */ if (lastp == NULL) trans = runp->next; else lastp->next = runp->next; } } /* Allocate room for handle. */ result = (__gconv_t) malloc (sizeof (struct __gconv_info) + (nsteps * sizeof (struct __gconv_step_data))); if (result == NULL) res = __GCONV_NOMEM; else { size_t n; /* Remember the list of steps. */ result->__steps = steps; result->__nsteps = nsteps; /* Clear the array for the step data. */ memset (result->__data, '\0', nsteps * sizeof (struct __gconv_step_data)); /* Call all initialization functions for the transformation step implementations. */ for (cnt = 0; cnt < nsteps; ++cnt) { size_t size; /* Would have to be done if we would not clear the whole array above. */ #if 0 /* Reset the counter. */ result->__data[cnt].__invocation_counter = 0; /* It's a regular use. */ result->__data[cnt].__internal_use = 0; #endif /* We use the `mbstate_t' member in DATA. */ result->__data[cnt].__statep = &result->__data[cnt].__state; /* Now see whether we can use any of the transliteration modules for this step. */ for (runp = trans; runp != NULL; runp = runp->next) for (n = 0; n < runp->ncsnames; ++n) if (__strcasecmp_l (steps[cnt].__from_name, runp->csnames[n], &_nl_C_locobj) == 0) { void *data = NULL; /* Match! Now try the initializer. */ if (runp->trans_init_fct == NULL || (runp->trans_init_fct (&data, steps[cnt].__to_name) == __GCONV_OK)) { /* Append at the end of the list. */ struct __gconv_trans_data *newp; struct __gconv_trans_data **lastp; newp = (struct __gconv_trans_data *) malloc (sizeof (struct __gconv_trans_data)); if (newp == NULL) { res = __GCONV_NOMEM; goto bail; } newp->__trans_fct = runp->trans_fct; newp->__trans_context_fct = runp->trans_context_fct; newp->__trans_end_fct = runp->trans_end_fct; newp->__data = data; newp->__next = NULL; lastp = &result->__data[cnt].__trans; while (*lastp != NULL) lastp = &(*lastp)->__next; *lastp = newp; } break; } /* If this is the last step we must not allocate an output buffer. */ if (cnt < nsteps - 1) { result->__data[cnt].__flags = conv_flags; /* Allocate the buffer. */ size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to); result->__data[cnt].__outbuf = (char *) malloc (size); if (result->__data[cnt].__outbuf == NULL) { res = __GCONV_NOMEM; goto bail; } result->__data[cnt].__outbufend = result->__data[cnt].__outbuf + size; } else { /* Handle the last entry. */ result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST; break; } } } if (res != __GCONV_OK) { /* Something went wrong. Free all the resources. */ int serrno; bail: serrno = errno; if (result != NULL) { while (cnt-- > 0) { struct __gconv_trans_data *transp; transp = result->__data[cnt].__trans; while (transp != NULL) { struct __gconv_trans_data *curp = transp; transp = transp->__next; if (__builtin_expect (curp->__trans_end_fct != NULL, 0)) curp->__trans_end_fct (curp->__data); free (curp); } free (result->__data[cnt].__outbuf); } free (result); result = NULL; } __gconv_close_transform (steps, nsteps); __set_errno (serrno); } } *handle = result; return res; }
int __vfscanf (FILE *s, const char *format, va_list argptr) #endif { va_list arg = (va_list) argptr; register const char *f = format; register unsigned char fc; /* Current character of the format. */ register size_t done = 0; /* Assignments done. */ register size_t read_in = 0; /* Chars read in. */ register int c = 0; /* Last char read. */ register int width; /* Maximum field width. */ register int flags; /* Modifiers for current format element. */ /* Status for reading F-P nums. */ char got_dot, got_e, negative; /* If a [...] is a [^...]. */ char not_in; /* Base for integral numbers. */ int base; /* Signedness for integral numbers. */ int number_signed; /* Decimal point character. */ wchar_t decimal; /* The thousands character of the current locale. */ wchar_t thousands; /* Integral holding variables. */ union { long long int q; unsigned long long int uq; long int l; unsigned long int ul; } num; /* Character-buffer pointer. */ char *str = NULL; wchar_t *wstr = NULL; char **strptr = NULL; size_t strsize = 0; /* We must not react on white spaces immediately because they can possibly be matched even if in the input stream no character is available anymore. */ int skip_space = 0; /* Workspace. */ char *tw; /* Temporary pointer. */ char *wp = NULL; /* Workspace. */ size_t wpmax = 0; /* Maximal size of workspace. */ size_t wpsize; /* Currently used bytes in workspace. */ #define ADDW(Ch) \ do \ { \ if (wpsize == wpmax) \ { \ char *old = wp; \ wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \ wp = (char *) alloca (wpmax); \ if (old != NULL) \ memcpy (wp, old, wpsize); \ } \ wp[wpsize++] = (Ch); \ } \ while (0) ARGCHECK (s, format); /* Figure out the decimal point character. */ if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT), strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0) decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); /* Figure out the thousands separator character. */ if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP), strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0) thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); /* Lock the stream. */ LOCK_STREAM (s); /* Run through the format string. */ while (*f != '\0') { unsigned int argpos; /* Extract the next argument, which is of type TYPE. For a %N$... spec, this is the Nth argument from the beginning; otherwise it is the next argument after the state now in ARG. */ #if 0 /* XXX Possible optimization. */ # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \ ({ va_list arg = (va_list) argptr; \ arg = (va_list) ((char *) arg \ + (argpos - 1) \ * __va_rounded_size (void *)); \ va_arg (arg, type); \ })) #else # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \ ({ unsigned int pos = argpos; \ va_list arg = (va_list) argptr; \ while (--pos > 0) \ (void) va_arg (arg, void *); \ va_arg (arg, type); \ })) #endif if (!isascii (*f)) { /* Non-ASCII, may be a multibyte. */ int len = mblen (f, strlen (f)); if (len > 0) { do { c = inchar (); if (c == EOF) input_error (); else if (c != *f++) { ungetc (c, s); conv_error (); } } while (--len > 0); continue; } } fc = *f++; if (fc != '%') { /* Remember to skip spaces. */ if (isspace (fc)) { skip_space = 1; continue; } /* Read a character. */ c = inchar (); /* Characters other than format specs must just match. */ if (c == EOF) input_error (); /* We saw white space char as the last character in the format string. Now it's time to skip all leading white space. */ if (skip_space) { while (isspace (c)) if (inchar () == EOF && errno == EINTR) conv_error (); skip_space = 0; } if (c != fc) { ungetc (c, s); conv_error (); } continue; } /* This is the start of the conversion string. */ flags = 0; /* Initialize state of modifiers. */ argpos = 0; /* Prepare temporary buffer. */ wpsize = 0; /* Check for a positional parameter specification. */ if (isdigit (*f)) { argpos = *f++ - '0'; while (isdigit (*f)) argpos = argpos * 10 + (*f++ - '0'); if (*f == '$') ++f; else { /* Oops; that was actually the field width. */ width = argpos; flags |= WIDTH; argpos = 0; goto got_width; } } /* Check for the assignment-suppressing and the number grouping flag. */ while (*f == '*' || *f == '\'') switch (*f++) { case '*': flags |= SUPPRESS; break; case '\'': flags |= GROUP; break; } /* We have seen width. */ if (isdigit (*f)) flags |= WIDTH; /* Find the maximum field width. */ width = 0; while (isdigit (*f)) { width *= 10; width += *f++ - '0'; } got_width: if (width == 0) width = -1; /* Check for type modifiers. */ while (*f == 'h' || *f == 'l' || *f == 'L' || *f == 'a' || *f == 'q') switch (*f++) { case 'h': /* int's are short int's. */ if (flags & TYPEMOD) /* Signal illegal format element. */ conv_error (); flags |= SHORT; break; case 'l': if (flags & (SHORT|LONGDBL)) conv_error (); else if (flags & LONG) { /* A double `l' is equivalent to an `L'. */ flags &= ~LONG; flags |= LONGDBL; } else /* int's are long int's. */ flags |= LONG; break; case 'q': case 'L': /* double's are long double's, and int's are long long int's. */ if (flags & TYPEMOD) /* Signal illegal format element. */ conv_error (); flags |= LONGDBL; break; case 'a': if (flags & TYPEMOD) /* Signal illegal format element. */ conv_error (); /* String conversions (%s, %[) take a `char **' arg and fill it in with a malloc'd pointer. */ flags |= MALLOC; break; } /* End of the format string? */ if (*f == '\0') conv_error (); /* We must take care for EINTR errors. */ if (c == EOF && errno == EINTR) input_error (); /* Find the conversion specifier. */ fc = *f++; if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n')) { /* Eat whitespace. */ do if (inchar () == EOF && errno == EINTR) input_error (); while (isspace (c)); ungetc (c, s); skip_space = 0; } switch (fc) { case '%': /* Must match a literal '%'. */ c = inchar (); if (c != fc) { ungetc (c, s); conv_error (); } break; case 'n': /* Answer number of assignments done. */ /* Corrigendum 1 to ISO C 1990 describes the allowed flags with the 'n' conversion specifier. */ if (!(flags & SUPPRESS)) { /* Don't count the read-ahead. */ if (flags & LONGDBL) *ARG (long long int *) = read_in; else if (flags & LONG) *ARG (long int *) = read_in; else if (flags & SHORT) *ARG (short int *) = read_in; else *ARG (int *) = read_in; #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1 /* We have a severe problem here. The ISO C standard contradicts itself in explaining the effect of the %n format in `scanf'. While in ISO C:1990 and the ISO C Amendement 1:1995 the result is described as Execution of a %n directive does not effect the assignment count returned at the completion of execution of the f(w)scanf function. in ISO C Corrigendum 1:1994 the following was added: Subclause 7.9.6.2 Add the following fourth example: In: #include <stdio.h> int d1, d2, n1, n2, i; i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2); the value 123 is assigned to d1 and the value3 to n1. Because %n can never get an input failure the value of 3 is also assigned to n2. The value of d2 is not affected. The value 3 is assigned to i. We go for now with the historically correct code fro ISO C, i.e., we don't count the %n assignments. When it ever should proof to be wrong just remove the #ifdef above. */ ++done; #endif } break; case 'c': /* Match characters. */ if ((flags & LONG) == 0) { if (!(flags & SUPPRESS)) { str = ARG (char *); if (str == NULL) conv_error (); } c = inchar (); if (c == EOF) input_error (); if (width == -1) width = 1; if (!(flags & SUPPRESS)) { do *str++ = c; while (--width > 0 && inchar () != EOF); } else while (--width > 0 && inchar () != EOF); if (width > 0) /* I.e., EOF was read. */ --read_in; if (!(flags & SUPPRESS)) ++done; break; } /* FALLTHROUGH */ case 'C': /* Get UTF-8 encoded wide character. Here we assume (as in other parts of the libc) that we only have to handle UTF-8. */ { wint_t val; size_t cnt = 0; int first = 1; if (!(flags & SUPPRESS)) { wstr = ARG (wchar_t *); if (str == NULL) conv_error (); } do { #define NEXT_WIDE_CHAR(First) \ c = inchar (); \ if (c == EOF) \ /* EOF is only an error for the first character. */ \ if (First) \ input_error (); \ else \ { \ --read_in; \ break; \ } \ val = c; \ if (val >= 0x80) \ { \ if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \ encode_error (); \ if ((c & 0xe0) == 0xc0) \ { \ /* We expect two bytes. */ \ cnt = 1; \ val &= 0x1f; \ } \ else if ((c & 0xf0) == 0xe0) \ { \ /* We expect three bytes. */ \ cnt = 2; \ val &= 0x0f; \ } \ else if ((c & 0xf8) == 0xf0) \ { \ /* We expect four bytes. */ \ cnt = 3; \ val &= 0x07; \ } \ else if ((c & 0xfc) == 0xf8) \ { \ /* We expect five bytes. */ \ cnt = 4; \ val &= 0x03; \ } \ else \ { \ /* We expect six bytes. */ \ cnt = 5; \ val &= 0x01; \ } \ \ do \ { \ c = inchar (); \ if (c == EOF \ || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \ encode_error (); \ val <<= 6; \ val |= c & 0x3f; \ } \ while (--cnt > 0); \ } \ \ if (!(flags & SUPPRESS)) \ *wstr++ = val; \ first = 0 NEXT_WIDE_CHAR (first); } while (--width > 0); if (width > 0) /* I.e., EOF was read. */ --read_in; if (!(flags & SUPPRESS)) ++done; } break; case 's': /* Read a string. */ if (flags & LONG) /* We have to process a wide character string. */ goto wide_char_string; #define STRING_ARG(Str, Type) \ if (!(flags & SUPPRESS)) \ { \ if (flags & MALLOC) \ { \ /* The string is to be stored in a malloc'd buffer. */ \ strptr = ARG (char **); \ if (strptr == NULL) \ conv_error (); \ /* Allocate an initial buffer. */ \ strsize = 100; \ *strptr = malloc (strsize * sizeof (Type)); \ Str = (Type *) *strptr; \ } \ else \ Str = ARG (Type *); \ if (Str == NULL) \ conv_error (); \ } STRING_ARG (str, char); c = inchar (); if (c == EOF) input_error (); do { if (isspace (c)) { ungetc (c, s); break; } #define STRING_ADD_CHAR(Str, c, Type) \ if (!(flags & SUPPRESS)) \ { \ *Str++ = c; \ if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \ { \ /* Enlarge the buffer. */ \ Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \ if (Str == NULL) \ { \ /* Can't allocate that much. Last-ditch effort. */\ Str = realloc (*strptr, \ (strsize + 1) * sizeof (Type)); \ if (Str == NULL) \ { \ /* We lose. Oh well. \ Terminate the string and stop converting, \ so at least we don't skip any input. */ \ ((Type *) (*strptr))[strsize] = '\0'; \ ++done; \ conv_error (); \ } \ else \ { \ *strptr = (char *) Str; \ Str = ((Type *) *strptr) + strsize; \ ++strsize; \ } \ } \ else \ { \ *strptr = (char *) Str; \ Str = ((Type *) *strptr) + strsize; \ strsize *= 2; \ } \ } \ } STRING_ADD_CHAR (str, c, char); } while ((width <= 0 || --width > 0) && inchar () != EOF); if (c == EOF) --read_in; if (!(flags & SUPPRESS)) { *str = '\0'; ++done; } break; case 'S': /* Wide character string. */ wide_char_string: { wint_t val; int first = 1; STRING_ARG (wstr, wchar_t); do { size_t cnt = 0; NEXT_WIDE_CHAR (first); if (iswspace (val)) { /* XXX We would have to push back the whole wide char with possibly many bytes. But since scanf does not make a difference for white space characters we can simply push back a simple <SP> which is guaranteed to be in the [:space:] class. */ ungetc (' ', s); break; } STRING_ADD_CHAR (wstr, val, wchar_t); first = 0; } while (width <= 0 || --width > 0); if (!(flags & SUPPRESS)) { *wstr = L'\0'; ++done; } } break; case 'x': /* Hexadecimal integer. */ case 'X': /* Ditto. */ base = 16; number_signed = 0; goto number; case 'o': /* Octal integer. */ base = 8; number_signed = 0; goto number; case 'u': /* Unsigned decimal integer. */ base = 10; number_signed = 0; goto number; case 'd': /* Signed decimal integer. */ base = 10; number_signed = 1; goto number; case 'i': /* Generic number. */ base = 0; number_signed = 1; number: c = inchar (); if (c == EOF) input_error (); /* Check for a sign. */ if (c == '-' || c == '+') { ADDW (c); if (width > 0) --width; c = inchar (); } /* Look for a leading indication of base. */ if (width != 0 && c == '0') { if (width > 0) --width; ADDW (c); c = inchar (); if (width != 0 && tolower (c) == 'x') { if (base == 0) base = 16; if (base == 16) { if (width > 0) --width; c = inchar (); } } else if (base == 0) base = 8; } if (base == 0) base = 10; /* Read the number into workspace. */ while (c != EOF && width != 0) { if (base == 16 ? !isxdigit (c) : ((!isdigit (c) || c - '0' >= base) && !((flags & GROUP) && base == 10 && c == thousands))) break; ADDW (c); if (width > 0) --width; c = inchar (); } /* The just read character is not part of the number anymore. */ ungetc (c, s); if (wpsize == 0 || (wpsize == 1 && (wp[0] == '+' || wp[0] == '-'))) /* There was no number. */ conv_error (); /* Convert the number. */ ADDW ('\0'); if (flags & LONGDBL) { if (number_signed) num.q = __strtoq_internal (wp, &tw, base, flags & GROUP); else num.uq = __strtouq_internal (wp, &tw, base, flags & GROUP); } else { if (number_signed) num.l = __strtol_internal (wp, &tw, base, flags & GROUP); else num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP); } if (wp == tw) conv_error (); if (!(flags & SUPPRESS)) { if (! number_signed) { if (flags & LONGDBL) *ARG (unsigned LONGLONG int *) = num.uq; else if (flags & LONG) *ARG (unsigned long int *) = num.ul; else if (flags & SHORT) *ARG (unsigned short int *) = (unsigned short int) num.ul; else *ARG (unsigned int *) = (unsigned int) num.ul; } else { if (flags & LONGDBL) *ARG (LONGLONG int *) = num.q; else if (flags & LONG) *ARG (long int *) = num.l; else if (flags & SHORT) *ARG (short int *) = (short int) num.l; else *ARG (int *) = (int) num.l; } ++done; } break; case 'e': /* Floating-point numbers. */ case 'E': case 'f': case 'g': case 'G': c = inchar (); if (c == EOF) input_error (); /* Check for a sign. */ if (c == '-' || c == '+') { negative = c == '-'; if (inchar () == EOF) /* EOF is only an input error before we read any chars. */ conv_error (); if (width > 0) --width; } else negative = 0; got_dot = got_e = 0; do { if (isdigit (c)) ADDW (c); else if (got_e && wp[wpsize - 1] == 'e' && (c == '-' || c == '+')) ADDW (c); else if (wpsize > 0 && !got_e && tolower (c) == 'e') { ADDW ('e'); got_e = got_dot = 1; } else if (c == decimal && !got_dot) { ADDW (c); got_dot = 1; } else if ((flags & GROUP) && c == thousands && !got_dot) ADDW (c); else break; if (width > 0) --width; } while (inchar () != EOF && width != 0); /* The last read character is not part of the number anymore. */ ungetc (c, s); if (wpsize == 0) conv_error (); /* Convert the number. */ ADDW ('\0'); if (flags & LONGDBL) { long double d = __strtold_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (long double *) = negative ? -d : d; } else if (flags & LONG) { double d = __strtod_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (double *) = negative ? -d : d; } else { float d = __strtof_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (float *) = negative ? -d : d; } if (tw == wp) conv_error (); if (!(flags & SUPPRESS)) ++done; break; case '[': /* Character class. */ if (flags & LONG) { STRING_ARG (wstr, wchar_t); c = '\0'; /* This is to keep gcc quiet. */ } else { STRING_ARG (str, char); c = inchar (); if (c == EOF) input_error (); } if (*f == '^') { ++f; not_in = 1; } else not_in = 0; /* Fill WP with byte flags indexed by character. We will use this flag map for matching input characters. */ if (wpmax < UCHAR_MAX) { wpmax = UCHAR_MAX; wp = (char *) alloca (wpmax); } memset (wp, 0, UCHAR_MAX); fc = *f; if (fc == ']' || fc == '-') { /* If ] or - appears before any char in the set, it is not the terminator or separator, but the first char in the set. */ wp[fc] = 1; ++f; } while ((fc = *f++) != '\0' && fc != ']') { if (fc == '-' && *f != '\0' && *f != ']' && (unsigned char) f[-2] <= (unsigned char) *f) { /* Add all characters from the one before the '-' up to (but not including) the next format char. */ for (fc = f[-2]; fc < *f; ++fc) wp[fc] = 1; } else /* Add the character to the flag map. */ wp[fc] = 1; } if (fc == '\0') { if (!(flags & LONG)) ungetc (c, s); conv_error(); } if (flags & LONG) { wint_t val; int first = 1; do { size_t cnt = 0; NEXT_WIDE_CHAR (first); if (val > 255 || wp[val] == not_in) { /* XXX We have a problem here. We read a wide character and this possibly took several bytes. But we can only push back one single character. To be sure we don't create wrong input we push it back only in case it is representable within one byte. */ if (val < 0x80) ungetc (val, s); break; } STRING_ADD_CHAR (wstr, val, wchar_t); if (width > 0) --width; first = 0; } while (width != 0); if (first) conv_error (); if (!(flags & SUPPRESS)) { *wstr = L'\0'; ++done; } } else { num.ul = read_in - 1; /* -1 because we already read one char. */ do { if (wp[c] == not_in) { ungetc (c, s); break; } STRING_ADD_CHAR (str, c, char); if (width > 0) --width; } while (width != 0 && inchar () != EOF); if (read_in == num.ul) conv_error (); if (!(flags & SUPPRESS)) { *str = '\0'; ++done; } } break; case 'p': /* Generic pointer. */ base = 16; /* A PTR must be the same size as a `long int'. */ flags &= ~(SHORT|LONGDBL); flags |= LONG; number_signed = 0; goto number; }
static void _nl_init_era_entries (void) { size_t cnt; __libc_lock_lock (__libc_setlocale_lock); if (era_initialized == 0) { size_t new_num_eras = _NL_CURRENT_WORD (LC_TIME, _NL_TIME_ERA_NUM_ENTRIES); if (new_num_eras == 0) { free (eras); eras = NULL; } else { if (num_eras != new_num_eras) eras = (struct era_entry *) realloc (eras, new_num_eras * sizeof (struct era_entry)); if (eras == NULL) { num_eras = 0; eras = NULL; } else { const char *ptr = _NL_CURRENT (LC_TIME, _NL_TIME_ERA_ENTRIES); num_eras = new_num_eras; for (cnt = 0; cnt < num_eras; ++cnt) { const char *base_ptr = ptr; memcpy ((void *) (eras + cnt), (const void *) ptr, sizeof (uint32_t) * 8); if (ERA_DATE_CMP(eras[cnt].start_date, eras[cnt].stop_date)) if (eras[cnt].direction == (uint32_t) '+') eras[cnt].absolute_direction = 1; else eras[cnt].absolute_direction = -1; else if (eras[cnt].direction == (uint32_t) '+') eras[cnt].absolute_direction = -1; else eras[cnt].absolute_direction = 1; /* Skip numeric values. */ ptr += sizeof (uint32_t) * 8; /* Set and skip era name. */ eras[cnt].era_name = ptr; ptr = strchr (ptr, '\0') + 1; /* Set and skip era format. */ eras[cnt].era_format = ptr; ptr = strchr (ptr, '\0') + 1; ptr += 3 - (((ptr - (const char *) base_ptr) + 3) & 3); /* Set and skip wide era name. */ eras[cnt].era_wname = (wchar_t *) ptr; ptr = (char *) (wcschr ((wchar_t *) ptr, L'\0') + 1); /* Set and skip wide era format. */ eras[cnt].era_wformat = (wchar_t *) ptr; ptr = (char *) (wcschr ((wchar_t *) ptr, L'\0') + 1); } } } era_initialized = 1; } __libc_lock_unlock (__libc_setlocale_lock); }
struct era_entry * _nl_get_era_entry (const struct tm *tp) { struct era_entry *result; size_t cnt; __libc_lock_lock (__libc_setlocale_lock); if (era_initialized == 0) { size_t new_num_eras = _NL_CURRENT_WORD (LC_TIME, _NL_TIME_ERA_NUM_ENTRIES); if (eras != NULL && new_num_eras == 0) { free (eras); eras = NULL; } else if (new_num_eras != 0) { if (num_eras != new_num_eras) eras = realloc (eras, new_num_eras * sizeof (struct era_entry *)); if (eras == NULL) num_eras = 0; else { #if __BYTE_ORDER == __LITTLE_ENDIAN const char *ptr = _NL_CURRENT (LC_TIME, _NL_TIME_ERA_ENTRIES_EL); #else const char *ptr = _NL_CURRENT (LC_TIME, _NL_TIME_ERA_ENTRIES_EB); #endif num_eras = new_num_eras; for (cnt = 0; cnt < num_eras; ++cnt) { eras[cnt] = (struct era_entry *) ptr; /* Skip numeric values. */ ptr += sizeof (struct era_entry); /* Skip era name. */ ptr = strchr (ptr, '\0') + 1; /* Skip era format. */ ptr = strchr (ptr, '\0') + 1; ptr += 3 - (((ptr - (const char *) eras[cnt]) + 3) & 3); } } } era_initialized = 1; } /* Now compare date with the available eras. */ for (cnt = 0; cnt < num_eras; ++cnt) if ((eras[cnt]->start_date[0] < tp->tm_year || (eras[cnt]->start_date[0] == tp->tm_year && (eras[cnt]->start_date[1] < tp->tm_mon || (eras[cnt]->start_date[1] == tp->tm_mon && eras[cnt]->start_date[2] <= tp->tm_mday)))) && (eras[cnt]->stop_date[0] > tp->tm_year || (eras[cnt]->stop_date[0] == tp->tm_year && (eras[cnt]->stop_date[1] > tp->tm_mon || (eras[cnt]->stop_date[1] == tp->tm_mon && eras[cnt]->stop_date[2] >= tp->tm_mday))))) break; result = cnt < num_eras ? eras[cnt] : NULL; __libc_lock_unlock (__libc_setlocale_lock); return result; }
static char * __strptime_internal (const char *rp, const char *fmt, struct tm *tmp, void *statep) { const char *rp_backup; const char *rp_longest; int cnt; int cnt_longest; size_t val; #ifdef _NL_CURRENT size_t num_eras; struct era_entry *era = NULL; #endif enum ptime_locale_status { nott, loc, raw } decided_longest; struct __strptime_state { unsigned int have_I : 1; unsigned int have_wday : 1; unsigned int have_yday : 1; unsigned int have_mon : 1; unsigned int have_mday : 1; unsigned int have_uweek : 1; unsigned int have_wweek : 1; unsigned int is_pm : 1; unsigned int want_century : 1; unsigned int want_era : 1; unsigned int want_xday : 1; enum ptime_locale_status decided : 2; signed char week_no; signed char century; int era_cnt; } s; struct tm tmb; struct tm *tm; if (statep == NULL) { memset (&s, 0, sizeof (s)); s.century = -1; s.era_cnt = -1; #ifdef _NL_CURRENT s.decided = nott; #else s.decided = raw; #endif tm = tmp; } else { s = *(struct __strptime_state *) statep; tmb = *tmp; tm = &tmb; } while (*fmt != '\0') { /* A white space in the format string matches 0 more or white space in the input string. */ if (isspace (*fmt)) { while (isspace (*rp)) ++rp; ++fmt; continue; } /* Any character but `%' must be matched by the same character in the iput string. */ if (*fmt != '%') { match_char (*fmt++, *rp++); continue; } ++fmt; if (statep != NULL) { /* In recursive calls silently discard strftime modifiers. */ while (*fmt == '-' || *fmt == '_' || *fmt == '0' || *fmt == '^' || *fmt == '#') ++fmt; /* And field width. */ while (*fmt >= '0' && *fmt <= '9') ++fmt; } #ifndef _NL_CURRENT /* We need this for handling the `E' modifier. */ start_over: #endif /* Make back up of current processing pointer. */ rp_backup = rp; switch (*fmt++) { case '%': /* Match the `%' character itself. */ match_char ('%', *rp++); break; case 'a': case 'A': /* Match day of week. */ rp_longest = NULL; decided_longest = s.decided; cnt_longest = -1; for (cnt = 0; cnt < 7; ++cnt) { const char *trp; #ifdef _NL_CURRENT if (s.decided !=raw) { trp = rp; if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), trp) && trp > rp_longest) { rp_longest = trp; cnt_longest = cnt; if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt), weekday_name[cnt])) decided_longest = loc; } trp = rp; if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), trp) && trp > rp_longest) { rp_longest = trp; cnt_longest = cnt; if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), ab_weekday_name[cnt])) decided_longest = loc; } } #endif if (s.decided != loc && (((trp = rp, match_string (weekday_name[cnt], trp)) && trp > rp_longest) || ((trp = rp, match_string (ab_weekday_name[cnt], rp)) && trp > rp_longest))) { rp_longest = trp; cnt_longest = cnt; decided_longest = raw; } } if (rp_longest == NULL) /* Does not match a weekday name. */ return NULL; rp = rp_longest; s.decided = decided_longest; tm->tm_wday = cnt_longest; s.have_wday = 1; break; case 'b': case 'B': case 'h': /* Match month name. */ rp_longest = NULL; decided_longest = s.decided; cnt_longest = -1; for (cnt = 0; cnt < 12; ++cnt) { const char *trp; #ifdef _NL_CURRENT if (s.decided !=raw) { trp = rp; if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), trp) && trp > rp_longest) { rp_longest = trp; cnt_longest = cnt; if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt), month_name[cnt])) decided_longest = loc; } trp = rp; if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), trp) && trp > rp_longest) { rp_longest = trp; cnt_longest = cnt; if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), ab_month_name[cnt])) decided_longest = loc; } } #endif if (s.decided != loc && (((trp = rp, match_string (month_name[cnt], trp)) && trp > rp_longest) || ((trp = rp, match_string (ab_month_name[cnt], trp)) && trp > rp_longest))) { rp_longest = trp; cnt_longest = cnt; decided_longest = raw; } } if (rp_longest == NULL) /* Does not match a month name. */ return NULL; rp = rp_longest; s.decided = decided_longest; tm->tm_mon = cnt_longest; s.have_mon = 1; s.want_xday = 1; break; case 'c': /* Match locale's date and time format. */ #ifdef _NL_CURRENT if (s.decided != raw) { if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT))) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT)) s.decided = loc; s.want_xday = 1; break; } s.decided = raw; } #endif if (!recursive (HERE_D_T_FMT)) return NULL; s.want_xday = 1; break; case 'C': /* Match century number. */ #ifdef _NL_CURRENT match_century: #endif get_number (0, 99, 2); s.century = val; s.want_xday = 1; break; case 'd': case 'e': /* Match day of month. */ get_number (1, 31, 2); tm->tm_mday = val; s.have_mday = 1; s.want_xday = 1; break; case 'F': if (!recursive ("%Y-%m-%d")) return NULL; s.want_xday = 1; break; case 'x': #ifdef _NL_CURRENT if (s.decided != raw) { if (!recursive (_NL_CURRENT (LC_TIME, D_FMT))) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT)) s.decided = loc; s.want_xday = 1; break; } s.decided = raw; } #endif /* Fall through. */ case 'D': /* Match standard day format. */ if (!recursive (HERE_D_FMT)) return NULL; s.want_xday = 1; break; case 'k': case 'H': /* Match hour in 24-hour clock. */ get_number (0, 23, 2); tm->tm_hour = val; s.have_I = 0; break; case 'l': /* Match hour in 12-hour clock. GNU extension. */ case 'I': /* Match hour in 12-hour clock. */ get_number (1, 12, 2); tm->tm_hour = val % 12; s.have_I = 1; break; case 'j': /* Match day number of year. */ get_number (1, 366, 3); tm->tm_yday = val - 1; s.have_yday = 1; break; case 'm': /* Match number of month. */ get_number (1, 12, 2); tm->tm_mon = val - 1; s.have_mon = 1; s.want_xday = 1; break; case 'M': /* Match minute. */ get_number (0, 59, 2); tm->tm_min = val; break; case 'n': case 't': /* Match any white space. */ while (isspace (*rp)) ++rp; break; case 'p': /* Match locale's equivalent of AM/PM. */ #ifdef _NL_CURRENT if (s.decided != raw) { if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp)) { if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR)) s.decided = loc; s.is_pm = 0; break; } if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp)) { if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR)) s.decided = loc; s.is_pm = 1; break; } s.decided = raw; } #endif if (!match_string (HERE_AM_STR, rp)) { if (match_string (HERE_PM_STR, rp)) s.is_pm = 1; else return NULL; } else s.is_pm = 0; break; case 'r': #ifdef _NL_CURRENT if (s.decided != raw) { if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM))) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (s.decided == nott && strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM), HERE_T_FMT_AMPM)) s.decided = loc; break; } s.decided = raw; } #endif if (!recursive (HERE_T_FMT_AMPM)) return NULL; break; case 'R': if (!recursive ("%H:%M")) return NULL; break; case 's': { /* The number of seconds may be very high so we cannot use the `get_number' macro. Instead read the number character for character and construct the result while doing this. */ time_t secs = 0; if (*rp < '0' || *rp > '9') /* We need at least one digit. */ return NULL; do { secs *= 10; secs += *rp++ - '0'; } while (*rp >= '0' && *rp <= '9'); if (localtime_r (&secs, tm) == NULL) /* Error in function. */ return NULL; } break; case 'S': get_number (0, 61, 2); tm->tm_sec = val; break; case 'X': #ifdef _NL_CURRENT if (s.decided != raw) { if (!recursive (_NL_CURRENT (LC_TIME, T_FMT))) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT)) s.decided = loc; break; } s.decided = raw; } #endif /* Fall through. */ case 'T': if (!recursive (HERE_T_FMT)) return NULL; break; case 'u': get_number (1, 7, 1); tm->tm_wday = val % 7; s.have_wday = 1; break; case 'g': get_number (0, 99, 2); /* XXX This cannot determine any field in TM. */ break; case 'G': if (*rp < '0' || *rp > '9') return NULL; /* XXX Ignore the number since we would need some more information to compute a real date. */ do ++rp; while (*rp >= '0' && *rp <= '9'); break; case 'U': get_number (0, 53, 2); s.week_no = val; s.have_uweek = 1; break; case 'W': get_number (0, 53, 2); s.week_no = val; s.have_wweek = 1; break; case 'V': get_number (0, 53, 2); /* XXX This cannot determine any field in TM without some information. */ break; case 'w': /* Match number of weekday. */ get_number (0, 6, 1); tm->tm_wday = val; s.have_wday = 1; break; case 'y': #ifdef _NL_CURRENT match_year_in_century: #endif /* Match year within century. */ get_number (0, 99, 2); /* The "Year 2000: The Millennium Rollover" paper suggests that values in the range 69-99 refer to the twentieth century. */ tm->tm_year = val >= 69 ? val : val + 100; /* Indicate that we want to use the century, if specified. */ s.want_century = 1; s.want_xday = 1; break; case 'Y': /* Match year including century number. */ get_number (0, 9999, 4); tm->tm_year = val - 1900; s.want_century = 0; s.want_xday = 1; break; case 'Z': /* XXX How to handle this? */ break; case 'z': /* We recognize two formats: if two digits are given, these specify hours. If fours digits are used, minutes are also specified. */ { val = 0; while (*rp == ' ') ++rp; if (*rp != '+' && *rp != '-') return NULL; rp++; int n = 0; while (n < 4 && *rp >= '0' && *rp <= '9') { val = val * 10 + *rp++ - '0'; ++n; } if (n == 2) val *= 100; else if (n != 4) /* Only two or four digits recognized. */ return NULL; else { /* We have to convert the minutes into decimal. */ if (val % 100 >= 60) return NULL; val = (val / 100) * 100 + ((val % 100) * 50) / 30; } if (val > 1200) return NULL; } break; case 'E': #ifdef _NL_CURRENT switch (*fmt++) { case 'c': /* Match locale's alternate date and time format. */ if (s.decided != raw) { const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT); if (*fmt == '\0') fmt = _NL_CURRENT (LC_TIME, D_T_FMT); if (!recursive (fmt)) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (strcmp (fmt, HERE_D_T_FMT)) s.decided = loc; s.want_xday = 1; break; } s.decided = raw; } /* The C locale has no era information, so use the normal representation. */ if (!recursive (HERE_D_T_FMT)) return NULL; s.want_xday = 1; break; case 'C': if (s.decided != raw) { if (s.era_cnt >= 0) { era = _nl_select_era_entry (s.era_cnt); if (era != NULL && match_string (era->era_name, rp)) { s.decided = loc; break; } else return NULL; } num_eras = _NL_CURRENT_WORD (LC_TIME, _NL_TIME_ERA_NUM_ENTRIES); for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt, rp = rp_backup) { era = _nl_select_era_entry (s.era_cnt); if (era != NULL && match_string (era->era_name, rp)) { s.decided = loc; break; } } if (s.era_cnt != (int) num_eras) break; s.era_cnt = -1; if (s.decided == loc) return NULL; s.decided = raw; } /* The C locale has no era information, so use the normal representation. */ goto match_century; case 'y': if (s.decided != raw) { get_number(0, 9999, 4); tm->tm_year = val; s.want_era = 1; s.want_xday = 1; s.want_century = 1; if (s.era_cnt >= 0) { assert (s.decided == loc); era = _nl_select_era_entry (s.era_cnt); bool match = false; if (era != NULL) { int delta = ((tm->tm_year - era->offset) * era->absolute_direction); match = (delta >= 0 && delta < (((int64_t) era->stop_date[0] - (int64_t) era->start_date[0]) * era->absolute_direction)); } if (! match) return NULL; break; } num_eras = _NL_CURRENT_WORD (LC_TIME, _NL_TIME_ERA_NUM_ENTRIES); for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt) { era = _nl_select_era_entry (s.era_cnt); if (era != NULL) { int delta = ((tm->tm_year - era->offset) * era->absolute_direction); if (delta >= 0 && delta < (((int64_t) era->stop_date[0] - (int64_t) era->start_date[0]) * era->absolute_direction)) { s.decided = loc; break; } } } if (s.era_cnt != (int) num_eras) break; s.era_cnt = -1; if (s.decided == loc) return NULL; s.decided = raw; } goto match_year_in_century; case 'Y': if (s.decided != raw) { num_eras = _NL_CURRENT_WORD (LC_TIME, _NL_TIME_ERA_NUM_ENTRIES); for (s.era_cnt = 0; s.era_cnt < (int) num_eras; ++s.era_cnt, rp = rp_backup) { era = _nl_select_era_entry (s.era_cnt); if (era != NULL && recursive (era->era_format)) break; } if (s.era_cnt == (int) num_eras) { s.era_cnt = -1; if (s.decided == loc) return NULL; else rp = rp_backup; } else { s.decided = loc; s.era_cnt = -1; break; } s.decided = raw; } get_number (0, 9999, 4); tm->tm_year = val - 1900; s.want_century = 0; s.want_xday = 1; break; case 'x': if (s.decided != raw) { const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT); if (*fmt == '\0') fmt = _NL_CURRENT (LC_TIME, D_FMT); if (!recursive (fmt)) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (strcmp (fmt, HERE_D_FMT)) s.decided = loc; break; } s.decided = raw; } if (!recursive (HERE_D_FMT)) return NULL; break; case 'X': if (s.decided != raw) { const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT); if (*fmt == '\0') fmt = _NL_CURRENT (LC_TIME, T_FMT); if (!recursive (fmt)) { if (s.decided == loc) return NULL; else rp = rp_backup; } else { if (strcmp (fmt, HERE_T_FMT)) s.decided = loc; break; } s.decided = raw; } if (!recursive (HERE_T_FMT)) return NULL; break; default: return NULL; } break; #else /* We have no information about the era format. Just use the normal format. */ if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y' && *fmt != 'x' && *fmt != 'X') /* This is an illegal format. */ return NULL; goto start_over; #endif case 'O': switch (*fmt++) { case 'd': case 'e': /* Match day of month using alternate numeric symbols. */ get_alt_number (1, 31, 2); tm->tm_mday = val; s.have_mday = 1; s.want_xday = 1; break; case 'H': /* Match hour in 24-hour clock using alternate numeric symbols. */ get_alt_number (0, 23, 2); tm->tm_hour = val; s.have_I = 0; break; case 'I': /* Match hour in 12-hour clock using alternate numeric symbols. */ get_alt_number (1, 12, 2); tm->tm_hour = val % 12; s.have_I = 1; break; case 'm': /* Match month using alternate numeric symbols. */ get_alt_number (1, 12, 2); tm->tm_mon = val - 1; s.have_mon = 1; s.want_xday = 1; break; case 'M': /* Match minutes using alternate numeric symbols. */ get_alt_number (0, 59, 2); tm->tm_min = val; break; case 'S': /* Match seconds using alternate numeric symbols. */ get_alt_number (0, 61, 2); tm->tm_sec = val; break; case 'U': get_alt_number (0, 53, 2); s.week_no = val; s.have_uweek = 1; break; case 'W': get_alt_number (0, 53, 2); s.week_no = val; s.have_wweek = 1; break; case 'V': get_alt_number (0, 53, 2); /* XXX This cannot determine any field in TM without further information. */ break; case 'w': /* Match number of weekday using alternate numeric symbols. */ get_alt_number (0, 6, 1); tm->tm_wday = val; s.have_wday = 1; break; case 'y': /* Match year within century using alternate numeric symbols. */ get_alt_number (0, 99, 2); tm->tm_year = val >= 69 ? val : val + 100; s.want_xday = 1; break; default: return NULL; } break; default: return NULL; } } if (statep != NULL) { /* Recursive invocation, returning success, so update parent's struct tm and state. */ *(struct __strptime_state *) statep = s; *tmp = tmb; return (char *) rp; } if (s.have_I && s.is_pm) tm->tm_hour += 12; if (s.century != -1) { if (s.want_century) tm->tm_year = tm->tm_year % 100 + (s.century - 19) * 100; else /* Only the century, but not the year. Strange, but so be it. */ tm->tm_year = (s.century - 19) * 100; } if (s.want_xday && !s.have_wday) { if ( !(s.have_mon && s.have_mday) && s.have_yday) { /* We don't have tm_mon and/or tm_mday, compute them. */ int t_mon = 0; while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday) t_mon++; if (!s.have_mon) tm->tm_mon = t_mon - 1; if (!s.have_mday) tm->tm_mday = (tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1); s.have_mon = 1; s.have_mday = 1; } /* Don't crash in day_of_the_week if tm_mon is uninitialized. */ if (s.have_mon || (unsigned) tm->tm_mon <= 11) day_of_the_week (tm); } if (s.want_xday && !s.have_yday && (s.have_mon || (unsigned) tm->tm_mon <= 11)) day_of_the_year (tm); if ((s.have_uweek || s.have_wweek) && s.have_wday) { int save_wday = tm->tm_wday; int save_mday = tm->tm_mday; int save_mon = tm->tm_mon; int w_offset = s.have_uweek ? 0 : 1; tm->tm_mday = 1; tm->tm_mon = 0; day_of_the_week (tm); if (s.have_mday) tm->tm_mday = save_mday; if (s.have_mon) tm->tm_mon = save_mon; if (!s.have_yday) tm->tm_yday = ((7 - (tm->tm_wday - w_offset)) % 7 + (s.week_no - 1) *7 + save_wday - w_offset); if (!s.have_mday || !s.have_mon) { int t_mon = 0; while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday) t_mon++; if (!s.have_mon) tm->tm_mon = t_mon - 1; if (!s.have_mday) tm->tm_mday = (tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1); } tm->tm_wday = save_wday; } return (char *) rp; }
internal_function _nl_init_domain_conv (struct loaded_l10nfile *domain_file, struct loaded_domain *domain, struct binding *domainbinding) { /* Find out about the character set the file is encoded with. This can be found (in textual form) in the entry "". If this entry does not exist or if this does not contain the `charset=' information, we will assume the charset matches the one the current locale and we don't have to perform any conversion. */ char *nullentry; size_t nullentrylen; /* Preinitialize fields, to avoid recursion during _nl_find_msg. */ domain->codeset_cntr = (domainbinding != NULL ? domainbinding->codeset_cntr : 0); #ifdef _LIBC domain->conv = (__gconv_t) -1; #else # if HAVE_ICONV domain->conv = (iconv_t) -1; # endif #endif domain->conv_tab = NULL; /* Get the header entry. */ nullentry = _nl_find_msg (domain_file, domainbinding, "", &nullentrylen); if (nullentry != NULL) { #if defined _LIBC || HAVE_ICONV const char *charsetstr; charsetstr = strstr (nullentry, "charset="); if (charsetstr != NULL) { size_t len; char *charset; const char *outcharset; charsetstr += strlen ("charset="); len = strcspn (charsetstr, " \t\n"); charset = (char *) alloca (len + 1); # if defined _LIBC || HAVE_MEMPCPY *((char *) mempcpy (charset, charsetstr, len)) = '\0'; # else memcpy (charset, charsetstr, len); charset[len] = '\0'; # endif /* The output charset should normally be determined by the locale. But sometimes the locale is not used or not correctly set up, so we provide a possibility for the user to override this. Moreover, the value specified through bind_textdomain_codeset overrides both. */ if (domainbinding != NULL && domainbinding->codeset != NULL) outcharset = domainbinding->codeset; else { outcharset = getenv ("OUTPUT_CHARSET"); if (outcharset == NULL || outcharset[0] == '\0') { # ifdef _LIBC outcharset = _NL_CURRENT (LC_CTYPE, CODESET); # else # if HAVE_ICONV extern const char *locale_charset (void); outcharset = locale_charset (); # endif # endif } } # ifdef _LIBC /* We always want to use transliteration. */ outcharset = norm_add_slashes (outcharset, "TRANSLIT"); charset = norm_add_slashes (charset, NULL); if (__gconv_open (outcharset, charset, &domain->conv, GCONV_AVOID_NOCONV) != __GCONV_OK) domain->conv = (__gconv_t) -1; # else # if HAVE_ICONV /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5, we want to use transliteration. */ # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \ || _LIBICONV_VERSION >= 0x0105 if (strchr (outcharset, '/') == NULL) { char *tmp; len = strlen (outcharset); tmp = (char *) alloca (len + 10 + 1); memcpy (tmp, outcharset, len); memcpy (tmp + len, "//TRANSLIT", 10 + 1); outcharset = tmp; domain->conv = iconv_open (outcharset, charset); freea (outcharset); } else # endif domain->conv = iconv_open (outcharset, charset); # endif # endif freea (charset); } #endif /* _LIBC || HAVE_ICONV */ } return nullentry; }
static int internal_function FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end, bool no_leading_period, int flags) { register const CHAR *p = pattern, *n = string; register UCHAR c; #ifdef _LIBC # if WIDE_CHAR_VERSION const char *collseq = (const char *) _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC); # else const UCHAR *collseq = (const UCHAR *) _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQMB); # endif #endif while ((c = *p++) != L_('\0')) { bool new_no_leading_period = false; c = FOLD (c); switch (c) { case L_('?'): if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(') { int res; res = EXT (c, p, n, string_end, no_leading_period, flags); if (res != -1) return res; } if (n == string_end) return FNM_NOMATCH; else if (*n == L_('/') && (flags & FNM_FILE_NAME)) return FNM_NOMATCH; else if (*n == L_('.') && no_leading_period) return FNM_NOMATCH; break; case L_('\\'): if (!(flags & FNM_NOESCAPE)) { c = *p++; if (c == L_('\0')) /* Trailing \ loses. */ return FNM_NOMATCH; c = FOLD (c); } if (n == string_end || FOLD ((UCHAR) *n) != c) return FNM_NOMATCH; break; case L_('*'): if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(') { int res; res = EXT (c, p, n, string_end, no_leading_period, flags); if (res != -1) return res; } if (n != string_end && *n == L_('.') && no_leading_period) return FNM_NOMATCH; for (c = *p++; c == L_('?') || c == L_('*'); c = *p++) { if (*p == L_('(') && (flags & FNM_EXTMATCH) != 0) { const CHAR *endp = END (p); if (endp != p) { /* This is a pattern. Skip over it. */ p = endp; continue; } } if (c == L_('?')) { /* A ? needs to match one character. */ if (n == string_end) /* There isn't another character; no match. */ return FNM_NOMATCH; else if (*n == L_('/') && __builtin_expect (flags & FNM_FILE_NAME, 0)) /* A slash does not match a wildcard under FNM_FILE_NAME. */ return FNM_NOMATCH; else /* One character of the string is consumed in matching this ? wildcard, so *??? won't match if there are less than three characters. */ ++n; } } if (c == L_('\0')) /* The wildcard(s) is/are the last element of the pattern. If the name is a file name and contains another slash this means it cannot match, unless the FNM_LEADING_DIR flag is set. */ { int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH; if (flags & FNM_FILE_NAME) { if (flags & FNM_LEADING_DIR) result = 0; else { if (MEMCHR (n, L_('/'), string_end - n) == NULL) result = 0; } } return result; } else { const CHAR *endp; endp = MEMCHR (n, (flags & FNM_FILE_NAME) ? L_('/') : L_('\0'), string_end - n); if (endp == NULL) endp = string_end; if (c == L_('[') || (__builtin_expect (flags & FNM_EXTMATCH, 0) != 0 && (c == L_('@') || c == L_('+') || c == L_('!')) && *p == L_('('))) { int flags2 = ((flags & FNM_FILE_NAME) ? flags : (flags & ~FNM_PERIOD)); bool no_leading_period2 = no_leading_period; for (--p; n < endp; ++n, no_leading_period2 = false) if (FCT (p, n, string_end, no_leading_period2, flags2) == 0) return 0; } else if (c == L_('/') && (flags & FNM_FILE_NAME)) { while (n < string_end && *n != L_('/')) ++n; if (n < string_end && *n == L_('/') && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags) == 0)) return 0; } else { int flags2 = ((flags & FNM_FILE_NAME) ? flags : (flags & ~FNM_PERIOD)); int no_leading_period2 = no_leading_period; if (c == L_('\\') && !(flags & FNM_NOESCAPE)) c = *p; c = FOLD (c); for (--p; n < endp; ++n, no_leading_period2 = false) if (FOLD ((UCHAR) *n) == c && (FCT (p, n, string_end, no_leading_period2, flags2) == 0)) return 0; } } /* If we come here no match is possible with the wildcard. */ return FNM_NOMATCH; case L_('['): { /* Nonzero if the sense of the character class is inverted. */ register bool not; CHAR cold; UCHAR fn; if (posixly_correct == 0) posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1; if (n == string_end) return FNM_NOMATCH; if (*n == L_('.') && no_leading_period) return FNM_NOMATCH; if (*n == L_('/') && (flags & FNM_FILE_NAME)) /* `/' cannot be matched. */ return FNM_NOMATCH; not = (*p == L_('!') || (posixly_correct < 0 && *p == L_('^'))); if (not) ++p; fn = FOLD ((UCHAR) *n); c = *p++; for (;;) { if (!(flags & FNM_NOESCAPE) && c == L_('\\')) { if (*p == L_('\0')) return FNM_NOMATCH; c = FOLD ((UCHAR) *p); ++p; if (c == fn) goto matched; } else if (c == L_('[') && *p == L_(':')) { /* Leave room for the null. */ CHAR str[CHAR_CLASS_MAX_LENGTH + 1]; size_t c1 = 0; #if defined _LIBC || WIDE_CHAR_SUPPORT wctype_t wt; #endif const CHAR *startp = p; for (;;) { if (c1 == CHAR_CLASS_MAX_LENGTH) /* The name is too long and therefore the pattern is ill-formed. */ return FNM_NOMATCH; c = *++p; if (c == L_(':') && p[1] == L_(']')) { p += 2; break; } if (c < L_('a') || c >= L_('z')) { /* This cannot possibly be a character class name. Match it as a normal range. */ p = startp; c = L_('['); goto normal_bracket; } str[c1++] = c; } str[c1] = L_('\0'); #if defined _LIBC || WIDE_CHAR_SUPPORT wt = IS_CHAR_CLASS (str); if (wt == 0) /* Invalid character class name. */ return FNM_NOMATCH; # if defined _LIBC && ! WIDE_CHAR_VERSION /* The following code is glibc specific but does there a good job in speeding up the code since we can avoid the btowc() call. */ if (_ISCTYPE ((UCHAR) *n, wt)) goto matched; # else if (ISWCTYPE (BTOWC ((UCHAR) *n), wt)) goto matched; # endif #else if ((STREQ (str, L_("alnum")) && ISALNUM ((UCHAR) *n)) || (STREQ (str, L_("alpha")) && ISALPHA ((UCHAR) *n)) || (STREQ (str, L_("blank")) && ISBLANK ((UCHAR) *n)) || (STREQ (str, L_("cntrl")) && ISCNTRL ((UCHAR) *n)) || (STREQ (str, L_("digit")) && ISDIGIT ((UCHAR) *n)) || (STREQ (str, L_("graph")) && ISGRAPH ((UCHAR) *n)) || (STREQ (str, L_("lower")) && ISLOWER ((UCHAR) *n)) || (STREQ (str, L_("print")) && ISPRINT ((UCHAR) *n)) || (STREQ (str, L_("punct")) && ISPUNCT ((UCHAR) *n)) || (STREQ (str, L_("space")) && ISSPACE ((UCHAR) *n)) || (STREQ (str, L_("upper")) && ISUPPER ((UCHAR) *n)) || (STREQ (str, L_("xdigit")) && ISXDIGIT ((UCHAR) *n))) goto matched; #endif c = *p++; } #ifdef _LIBC else if (c == L_('[') && *p == L_('=')) { UCHAR str[1]; uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); const CHAR *startp = p; c = *++p; if (c == L_('\0')) { p = startp; c = L_('['); goto normal_bracket; } str[0] = c; c = *++p; if (c != L_('=') || p[1] != L_(']')) { p = startp; c = L_('['); goto normal_bracket; } p += 2; if (nrules == 0) { if ((UCHAR) *n == str[0]) goto matched; } else { const int32_t *table; # if WIDE_CHAR_VERSION const int32_t *weights; const int32_t *extra; # else const unsigned char *weights; const unsigned char *extra; # endif const int32_t *indirect; int32_t idx; const UCHAR *cp = (const UCHAR *) str; /* This #include defines a local function! */ # if WIDE_CHAR_VERSION # include <locale/weightwc.h> # else # include <locale/weight.h> # endif # if WIDE_CHAR_VERSION table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC); weights = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC); extra = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC); indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC); # else table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB); extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB); indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB); # endif idx = findidx (&cp); if (idx != 0) { /* We found a table entry. Now see whether the character we are currently at has the same equivalance class value. */ int len = weights[idx]; int32_t idx2; const UCHAR *np = (const UCHAR *) n; idx2 = findidx (&np); if (idx2 != 0 && len == weights[idx2]) { int cnt = 0; while (cnt < len && (weights[idx + 1 + cnt] == weights[idx2 + 1 + cnt])) ++cnt; if (cnt == len) goto matched; } } } c = *p++; } #endif else if (c == L_('\0')) /* [ (unterminated) loses. */ return FNM_NOMATCH; else { bool is_range = false; #ifdef _LIBC bool is_seqval = false; if (c == L_('[') && *p == L_('.')) { uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); const CHAR *startp = p; size_t c1 = 0; while (1) { c = *++p; if (c == L_('.') && p[1] == L_(']')) { p += 2; break; } if (c == '\0') return FNM_NOMATCH; ++c1; } /* We have to handling the symbols differently in ranges since then the collation sequence is important. */ is_range = *p == L_('-') && p[1] != L_('\0'); if (nrules == 0) { /* There are no names defined in the collation data. Therefore we only accept the trivial names consisting of the character itself. */ if (c1 != 1) return FNM_NOMATCH; if (!is_range && *n == startp[1]) goto matched; cold = startp[1]; c = *p++; } else { int32_t table_size; const int32_t *symb_table; # ifdef WIDE_CHAR_VERSION char str[c1]; size_t strcnt; # else # define str (startp + 1) # endif const unsigned char *extra; int32_t idx; int32_t elem; int32_t second; int32_t hash; # ifdef WIDE_CHAR_VERSION /* We have to convert the name to a single-byte string. This is possible since the names consist of ASCII characters and the internal representation is UCS4. */ for (strcnt = 0; strcnt < c1; ++strcnt) str[strcnt] = startp[1 + strcnt]; # endif table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB); symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_TABLEMB); extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB); /* Locate the character in the hashing table. */ hash = elem_hash (str, c1); idx = 0; elem = hash % table_size; second = hash % (table_size - 2); while (symb_table[2 * elem] != 0) { /* First compare the hashing value. */ if (symb_table[2 * elem] == hash && c1 == extra[symb_table[2 * elem + 1]] && memcmp (str, &extra[symb_table[2 * elem + 1] + 1], c1) == 0) { /* Yep, this is the entry. */ idx = symb_table[2 * elem + 1]; idx += 1 + extra[idx]; break; } /* Next entry. */ elem += second; } if (symb_table[2 * elem] != 0) { /* Compare the byte sequence but only if this is not part of a range. */ # ifdef WIDE_CHAR_VERSION int32_t *wextra; idx += 1 + extra[idx]; /* Adjust for the alignment. */ idx = (idx + 3) & ~3; wextra = (int32_t *) &extra[idx + 4]; # endif if (! is_range) { # ifdef WIDE_CHAR_VERSION for (c1 = 0; (int32_t) c1 < wextra[idx]; ++c1) if (n[c1] != wextra[1 + c1]) break; if ((int32_t) c1 == wextra[idx]) goto matched; # else for (c1 = 0; c1 < extra[idx]; ++c1) if (n[c1] != extra[1 + c1]) break; if (c1 == extra[idx]) goto matched; # endif } /* Get the collation sequence value. */ is_seqval = true; # ifdef WIDE_CHAR_VERSION cold = wextra[1 + wextra[idx]]; # else /* Adjust for the alignment. */ idx += 1 + extra[idx]; idx = (idx + 3) & ~4; cold = *((int32_t *) &extra[idx]); # endif c = *p++; } else if (c1 == 1) { /* No valid character. Match it as a single byte. */ if (!is_range && *n == str[0]) goto matched; cold = str[0]; c = *p++; } else return FNM_NOMATCH; } } else # undef str #endif { c = FOLD (c); normal_bracket: /* We have to handling the symbols differently in ranges since then the collation sequence is important. */ is_range = (*p == L_('-') && p[1] != L_('\0') && p[1] != L_(']')); if (!is_range && c == fn) goto matched; cold = c; c = *p++; } if (c == L_('-') && *p != L_(']')) { #if _LIBC /* We have to find the collation sequence value for C. Collation sequence is nothing we can regularly access. The sequence value is defined by the order in which the definitions of the collation values for the various characters appear in the source file. A strange concept, nowhere documented. */ uint32_t fcollseq; uint32_t lcollseq; UCHAR cend = *p++; # ifdef WIDE_CHAR_VERSION /* Search in the `names' array for the characters. */ fcollseq = __collseq_table_lookup (collseq, fn); if (fcollseq == ~((uint32_t) 0)) /* XXX We don't know anything about the character we are supposed to match. This means we are failing. */ goto range_not_matched; if (is_seqval) lcollseq = cold; else lcollseq = __collseq_table_lookup (collseq, cold); # else fcollseq = collseq[fn]; lcollseq = is_seqval ? cold : collseq[(UCHAR) cold]; # endif is_seqval = false; if (cend == L_('[') && *p == L_('.')) { uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); const CHAR *startp = p; size_t c1 = 0; while (1) { c = *++p; if (c == L_('.') && p[1] == L_(']')) { p += 2; break; } if (c == '\0') return FNM_NOMATCH; ++c1; } if (nrules == 0) { /* There are no names defined in the collation data. Therefore we only accept the trivial names consisting of the character itself. */ if (c1 != 1) return FNM_NOMATCH; cend = startp[1]; } else { int32_t table_size; const int32_t *symb_table; # ifdef WIDE_CHAR_VERSION char str[c1]; size_t strcnt; # else # define str (startp + 1) # endif const unsigned char *extra; int32_t idx; int32_t elem; int32_t second; int32_t hash; # ifdef WIDE_CHAR_VERSION /* We have to convert the name to a single-byte string. This is possible since the names consist of ASCII characters and the internal representation is UCS4. */ for (strcnt = 0; strcnt < c1; ++strcnt) str[strcnt] = startp[1 + strcnt]; # endif table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB); symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_TABLEMB); extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB); /* Locate the character in the hashing table. */ hash = elem_hash (str, c1); idx = 0; elem = hash % table_size; second = hash % (table_size - 2); while (symb_table[2 * elem] != 0) { /* First compare the hashing value. */ if (symb_table[2 * elem] == hash && (c1 == extra[symb_table[2 * elem + 1]]) && memcmp (str, &extra[symb_table[2 * elem + 1] + 1], c1) == 0) { /* Yep, this is the entry. */ idx = symb_table[2 * elem + 1]; idx += 1 + extra[idx]; break; } /* Next entry. */ elem += second; } if (symb_table[2 * elem] != 0) { /* Compare the byte sequence but only if this is not part of a range. */ # ifdef WIDE_CHAR_VERSION int32_t *wextra; idx += 1 + extra[idx]; /* Adjust for the alignment. */ idx = (idx + 3) & ~4; wextra = (int32_t *) &extra[idx + 4]; # endif /* Get the collation sequence value. */ is_seqval = true; # ifdef WIDE_CHAR_VERSION cend = wextra[1 + wextra[idx]]; # else /* Adjust for the alignment. */ idx += 1 + extra[idx]; idx = (idx + 3) & ~4; cend = *((int32_t *) &extra[idx]); # endif } else if (symb_table[2 * elem] != 0 && c1 == 1) { cend = str[0]; c = *p++; } else return FNM_NOMATCH; } # undef str } else { if (!(flags & FNM_NOESCAPE) && cend == L_('\\')) cend = *p++; if (cend == L_('\0')) return FNM_NOMATCH; cend = FOLD (cend); } /* XXX It is not entirely clear to me how to handle characters which are not mentioned in the collation specification. */ if ( # ifdef WIDE_CHAR_VERSION lcollseq == 0xffffffff || # endif lcollseq <= fcollseq) { /* We have to look at the upper bound. */ uint32_t hcollseq; if (is_seqval) hcollseq = cend; else { # ifdef WIDE_CHAR_VERSION hcollseq = __collseq_table_lookup (collseq, cend); if (hcollseq == ~((uint32_t) 0)) { /* Hum, no information about the upper bound. The matching succeeds if the lower bound is matched exactly. */ if (lcollseq != fcollseq) goto range_not_matched; goto matched; } # else hcollseq = collseq[cend]; # endif } if (lcollseq <= hcollseq && fcollseq <= hcollseq) goto matched; } # ifdef WIDE_CHAR_VERSION range_not_matched: # endif #else /* We use a boring value comparison of the character values. This is better than comparing using `strcoll' since the latter would have surprising and sometimes fatal consequences. */ UCHAR cend = *p++; if (!(flags & FNM_NOESCAPE) && cend == L_('\\')) cend = *p++; if (cend == L_('\0')) return FNM_NOMATCH; /* It is a range. */ if (cold <= fn && fn <= cend) goto matched; #endif c = *p++; } } if (c == L_(']')) break; } if (!not) return FNM_NOMATCH; break; matched: /* Skip the rest of the [...] that already matched. */ do { ignore_next: c = *p++; if (c == L_('\0')) /* [... (unterminated) loses. */ return FNM_NOMATCH; if (!(flags & FNM_NOESCAPE) && c == L_('\\')) { if (*p == L_('\0')) return FNM_NOMATCH; /* XXX 1003.2d11 is unclear if this is right. */ ++p; } else if (c == L_('[') && *p == L_(':')) { int c1 = 0; const CHAR *startp = p; while (1) { c = *++p; if (++c1 == CHAR_CLASS_MAX_LENGTH) return FNM_NOMATCH; if (*p == L_(':') && p[1] == L_(']')) break; if (c < L_('a') || c >= L_('z')) { p = startp; goto ignore_next; } } p += 2; c = *p++; } else if (c == L_('[') && *p == L_('=')) { c = *++p; if (c == L_('\0')) return FNM_NOMATCH; c = *++p; if (c != L_('=') || p[1] != L_(']')) return FNM_NOMATCH; p += 2; c = *p++; } else if (c == L_('[') && *p == L_('.')) { ++p; while (1) { c = *++p; if (c == '\0') return FNM_NOMATCH; if (*p == L_('.') && p[1] == L_(']')) break; } p += 2; c = *p++; } } while (c != L_(']')); if (not) return FNM_NOMATCH; } break; case L_('+'): case L_('@'): case L_('!'): if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(') { int res; res = EXT (c, p, n, string_end, no_leading_period, flags); if (res != -1) return res; } goto normal_match; case L_('/'): if (NO_LEADING_PERIOD (flags)) { if (n == string_end || c != (UCHAR) *n) return FNM_NOMATCH; new_no_leading_period = true; break; } /* FALLTHROUGH */ default: normal_match: if (n == string_end || c != FOLD ((UCHAR) *n)) return FNM_NOMATCH; } no_leading_period = new_no_leading_period; ++n; } if (n == string_end) return 0; if ((flags & FNM_LEADING_DIR) && n != string_end && *n == L_('/')) /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ return 0; return FNM_NOMATCH; }