X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fstrftime.c;h=b15dfa9a451053e9ae576f1a45ace92b6fa9bf22;hb=c1ebd28b04d9ecf3abc3fe980eaaac13456ba51e;hp=cc3a2d56867d216faac5a111468a3f3672e84fea;hpb=d763b0b65ebf7a0961f819ab0e733c23cbef7b58;p=gnulib.git diff --git a/lib/strftime.c b/lib/strftime.c index cc3a2d568..b15dfa9a4 100644 --- a/lib/strftime.c +++ b/lib/strftime.c @@ -1,22 +1,21 @@ -/* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc. +/* Copyright (C) 1991-1999, 2000, 2001, 2003 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2, or (at your option) any - later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - USA. */ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include @@ -265,7 +264,7 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */ int _n = (n); \ int _delta = width - _n; \ int _incr = _n + (_delta > 0 ? _delta : 0); \ - if (i + _incr >= maxsize) \ + if ((size_t) _incr >= maxsize - i) \ return 0; \ if (p) \ { \ @@ -285,35 +284,76 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */ #define cpy(n, s) \ add ((n), \ if (to_lowcase) \ - memcpy_lowcase (p, (s), _n); \ + memcpy_lowcase (p, (s), _n LOCALE_ARG); \ else if (to_uppcase) \ - memcpy_uppcase (p, (s), _n); \ + memcpy_uppcase (p, (s), _n LOCALE_ARG); \ else \ MEMCPY ((PTR) p, (const PTR) (s), _n)) #ifdef COMPILE_WIDE +# ifndef USE_IN_EXTENDED_LOCALE_MODEL +# undef __mbsrtowcs_l +# define __mbsrtowcs_l(d, s, l, st, loc) __mbsrtowcs (d, s, l, st) +# endif # define widen(os, ws, l) \ { \ mbstate_t __st; \ const char *__s = os; \ memset (&__st, '\0', sizeof (__st)); \ - l = __mbsrtowcs (NULL, &__s, 0, &__st); \ - ws = alloca ((l + 1) * sizeof (wchar_t)); \ - (void) __mbsrtowcs (ws, &__s, l, &__st); \ + l = __mbsrtowcs_l (NULL, &__s, 0, &__st, loc); \ + ws = (wchar_t *) alloca ((l + 1) * sizeof (wchar_t)); \ + (void) __mbsrtowcs_l (ws, &__s, l, &__st, loc); \ } #endif +#if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL +/* We use this code also for the extended locale handling where the + function gets as an additional argument the locale which has to be + used. To access the values we have to redefine the _NL_CURRENT + macro. */ +# define strftime __strftime_l +# define wcsftime __wcsftime_l +# undef _NL_CURRENT +# define _NL_CURRENT(category, item) \ + (current->values[_NL_ITEM_INDEX (item)].string) +# define LOCALE_PARAM , loc +# define LOCALE_ARG , loc +# define LOCALE_PARAM_DECL __locale_t loc; +# define LOCALE_PARAM_PROTO , __locale_t loc +# define HELPER_LOCALE_ARG , current +#else +# define LOCALE_PARAM +# define LOCALE_PARAM_PROTO +# define LOCALE_ARG +# define LOCALE_PARAM_DECL +# ifdef _LIBC +# define HELPER_LOCALE_ARG , _NL_CURRENT_DATA (LC_TIME) +# else +# define HELPER_LOCALE_ARG +# endif +#endif + #ifdef COMPILE_WIDE -# define TOUPPER(Ch) towupper (Ch) -# define TOLOWER(Ch) towlower (Ch) +# ifdef USE_IN_EXTENDED_LOCALE_MODEL +# define TOUPPER(Ch, L) __towupper_l (Ch, L) +# define TOLOWER(Ch, L) __towlower_l (Ch, L) +# else +# define TOUPPER(Ch, L) towupper (Ch) +# define TOLOWER(Ch, L) towlower (Ch) +# endif #else # ifdef _LIBC -# define TOUPPER(Ch) toupper (Ch) -# define TOLOWER(Ch) tolower (Ch) +# ifdef USE_IN_EXTENDED_LOCALE_MODEL +# define TOUPPER(Ch, L) __toupper_l (Ch, L) +# define TOLOWER(Ch, L) __tolower_l (Ch, L) +# else +# define TOUPPER(Ch, L) toupper (Ch) +# define TOLOWER(Ch, L) tolower (Ch) +# endif # else -# define TOUPPER(Ch) (islower (Ch) ? toupper (Ch) : (Ch)) -# define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) +# define TOUPPER(Ch, L) (islower (Ch) ? toupper (Ch) : (Ch)) +# define TOLOWER(Ch, L) (isupper (Ch) ? tolower (Ch) : (Ch)) # endif #endif /* We don't use `isdigit' here since the locale dependent @@ -323,30 +363,32 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */ #define ISDIGIT(Ch) ((unsigned int) (Ch) - L_('0') <= 9) static CHAR_T *memcpy_lowcase __P ((CHAR_T *dest, const CHAR_T *src, - size_t len)); + size_t len LOCALE_PARAM_PROTO)); static CHAR_T * -memcpy_lowcase (dest, src, len) +memcpy_lowcase (dest, src, len LOCALE_PARAM) CHAR_T *dest; const CHAR_T *src; size_t len; + LOCALE_PARAM_DECL { while (len-- > 0) - dest[len] = TOLOWER ((UCHAR_T) src[len]); + dest[len] = TOLOWER ((UCHAR_T) src[len], loc); return dest; } static CHAR_T *memcpy_uppcase __P ((CHAR_T *dest, const CHAR_T *src, - size_t len)); + size_t len LOCALE_PARAM_PROTO)); static CHAR_T * -memcpy_uppcase (dest, src, len) +memcpy_uppcase (dest, src, len LOCALE_PARAM) CHAR_T *dest; const CHAR_T *src; size_t len; + LOCALE_PARAM_DECL { while (len-- > 0) - dest[len] = TOUPPER ((UCHAR_T) src[len]); + dest[len] = TOUPPER ((UCHAR_T) src[len], loc); return dest; } @@ -437,8 +479,10 @@ static CHAR_T const month_name[][10] = #else # ifdef COMPILE_WIDE # define my_strftime wcsftime +# define nl_get_alt_digit _nl_get_walt_digit # else # define my_strftime strftime +# define nl_get_alt_digit _nl_get_alt_digit # endif # define extra_args # define extra_args_spec @@ -477,13 +521,18 @@ static CHAR_T const month_name[][10] = anywhere, so to determine how many characters would be written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */ size_t -my_strftime (s, maxsize, format, tp extra_args) +my_strftime (s, maxsize, format, tp extra_args LOCALE_PARAM) CHAR_T *s; size_t maxsize; const CHAR_T *format; const struct tm *tp; extra_args_spec + LOCALE_PARAM_DECL { +#if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL + struct locale_data *const current = loc->__locales[LC_TIME]; +#endif + int hour12 = tp->tm_hour; #ifdef _NL_CURRENT /* We cannot make the following values variables since we must delay @@ -546,9 +595,8 @@ my_strftime (s, maxsize, format, tp extra_args) } else { - /* POSIX.1 8.1.1 requires that whenever strftime() is called, the - time zone names contained in the external variable `tzname' shall - be set as if the tzset() function had been called. */ + /* POSIX.1 requires that local time zone information be used as + though strftime called tzset. */ # if HAVE_TZSET tzset (); # endif @@ -699,8 +747,15 @@ my_strftime (s, maxsize, format, tp extra_args) width = 0; do { - width *= 10; - width += *f - L_('0'); + if (width > INT_MAX / 10 + || (width == INT_MAX / 10 && *f - L_('0') > INT_MAX % 10)) + /* Avoid overflow. */ + width = INT_MAX; + else + { + width *= 10; + width += *f - L_('0'); + } ++f; } while (ISDIGIT (*f)); @@ -724,10 +779,10 @@ my_strftime (s, maxsize, format, tp extra_args) switch (format_char) { #define DO_NUMBER(d, v) \ - digits = width == -1 ? d : width; \ + digits = d > width ? d : width; \ number_value = v; goto do_number #define DO_NUMBER_SPACEPAD(d, v) \ - digits = width == -1 ? d : width; \ + digits = d > width ? d : width; \ number_value = v; goto do_number_spacepad case L_('%'): @@ -767,7 +822,7 @@ my_strftime (s, maxsize, format, tp extra_args) #endif case L_('b'): - case L_('h'): /* POSIX.2 extension. */ + case L_('h'): if (change_case) { to_uppcase = 1; @@ -819,14 +874,14 @@ my_strftime (s, maxsize, format, tp extra_args) { CHAR_T *old_start = p; size_t len = my_strftime (NULL, (size_t) -1, subfmt, - tp extra_args); + tp extra_args LOCALE_ARG); add (len, my_strftime (p, maxsize - i, subfmt, - tp extra_args)); + tp extra_args LOCALE_ARG)); if (to_uppcase) while (old_start < p) { - *old_start = TOUPPER ((UCHAR_T) *old_start); + *old_start = TOUPPER ((UCHAR_T) *old_start, loc); ++old_start; } } @@ -862,13 +917,13 @@ my_strftime (s, maxsize, format, tp extra_args) break; #endif - case L_('C'): /* POSIX.2 extension. */ + case L_('C'): if (modifier == L_('O')) goto bad_format; if (modifier == L_('E')) { #if HAVE_STRUCT_ERA_ENTRY - struct era_entry *era = _nl_get_era_entry (tp); + struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG); if (era) { # ifdef COMPILE_WIDE @@ -909,7 +964,7 @@ my_strftime (s, maxsize, format, tp extra_args) /* Fall through. */ # endif #endif - case L_('D'): /* POSIX.2 extension. */ + case L_('D'): if (modifier != 0) goto bad_format; subfmt = L_("%m/%d/%y"); @@ -921,7 +976,7 @@ my_strftime (s, maxsize, format, tp extra_args) DO_NUMBER (2, tp->tm_mday); - case L_('e'): /* POSIX.2 extension. */ + case L_('e'): if (modifier == L_('E')) goto bad_format; @@ -931,8 +986,8 @@ my_strftime (s, maxsize, format, tp extra_args) jump to one of these two labels. */ do_number_spacepad: - /* Force `_' flag unless overwritten by `0' flag. */ - if (pad != L_('0')) + /* Force `_' flag unless overridden by `0' or `-' flag. */ + if (pad != L_('0') && pad != L_('-')) pad = L_('_'); do_number: @@ -943,11 +998,8 @@ my_strftime (s, maxsize, format, tp extra_args) #ifdef _NL_CURRENT /* Get the locale specific alternate representation of the number NUMBER_VALUE. If none exist NULL is returned. */ -# ifdef COMPILE_WIDE - const wchar_t *cp = _nl_get_walt_digit (number_value); -# else - const char *cp = _nl_get_alt_digit (number_value); -# endif + const CHAR_T *cp = nl_get_alt_digit (number_value + HELPER_LOCALE_ARG); if (cp != NULL) { @@ -987,18 +1039,37 @@ my_strftime (s, maxsize, format, tp extra_args) int padding = digits - (buf + (sizeof (buf) / sizeof (buf[0])) - bufp); - if (pad == L_('_')) + if (padding > 0) { - while (0 < padding--) - *--bufp = L_(' '); - } - else - { - bufp += negative_number; - while (0 < padding--) - *--bufp = L_('0'); - if (negative_number) - *--bufp = L_('-'); + if (pad == L_('_')) + { + if ((size_t) padding >= maxsize - i) + return 0; + + if (p) + memset_space (p, padding); + i += padding; + width = width > padding ? width - padding : 0; + } + else + { + if ((size_t) digits >= maxsize - i) + return 0; + + if (negative_number) + { + ++bufp; + + if (p) + *p++ = L_('-'); + ++i; + } + + if (p) + memset_zero (p, padding); + i += padding; + width = 0; + } } } @@ -1053,6 +1124,7 @@ my_strftime (s, maxsize, format, tp extra_args) DO_NUMBER (2, tp->tm_mon + 1); +#ifndef _LIBC case L_('N'): /* GNU extension. */ if (modifier == L_('E')) goto bad_format; @@ -1067,8 +1139,9 @@ my_strftime (s, maxsize, format, tp extra_args) } DO_NUMBER (9, number_value); +#endif - case L_('n'): /* POSIX.2 extension. */ + case L_('n'): add (1, *p = L_('\n')); break; @@ -1092,11 +1165,11 @@ my_strftime (s, maxsize, format, tp extra_args) goto underlying_strftime; #endif - case L_('R'): /* ISO C99 extension. */ + case L_('R'): subfmt = L_("%H:%M"); goto subformat; - case L_('r'): /* POSIX.2 extension. */ + case L_('r'): #ifdef _NL_CURRENT if (*(subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(T_FMT_AMPM))) @@ -1167,15 +1240,15 @@ my_strftime (s, maxsize, format, tp extra_args) /* Fall through. */ # endif #endif - case L_('T'): /* POSIX.2 extension. */ + case L_('T'): subfmt = L_("%H:%M:%S"); goto subformat; - case L_('t'): /* POSIX.2 extension. */ + case L_('t'): add (1, *p = L_('\t')); break; - case L_('u'): /* POSIX.2 extension. */ + case L_('u'): DO_NUMBER (1, (tp->tm_wday - 1 + 7) % 7 + 1); case L_('U'): @@ -1185,8 +1258,8 @@ my_strftime (s, maxsize, format, tp extra_args) DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7); case L_('V'): - case L_('g'): /* ISO C99 extension. */ - case L_('G'): /* ISO C99 extension. */ + case L_('g'): + case L_('G'): if (modifier == L_('E')) goto bad_format; { @@ -1241,7 +1314,7 @@ my_strftime (s, maxsize, format, tp extra_args) if (modifier == 'E') { #if HAVE_STRUCT_ERA_ENTRY - struct era_entry *era = _nl_get_era_entry (tp); + struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG); if (era) { # ifdef COMPILE_WIDE @@ -1266,7 +1339,7 @@ my_strftime (s, maxsize, format, tp extra_args) if (modifier == L_('E')) { #if HAVE_STRUCT_ERA_ENTRY - struct era_entry *era = _nl_get_era_entry (tp); + struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG); if (era) { int delta = tp->tm_year - era->start_date[0]; @@ -1294,7 +1367,7 @@ my_strftime (s, maxsize, format, tp extra_args) zone = tzname[tp->tm_isdst]; #endif if (! zone) - zone = ""; /* POSIX.2 requires the empty string here. */ + zone = ""; #ifdef COMPILE_WIDE { @@ -1310,7 +1383,7 @@ my_strftime (s, maxsize, format, tp extra_args) #endif break; - case L_('z'): /* ISO C99 extension. */ + case L_('z'): if (tp->tm_isdst < 0) break; @@ -1388,6 +1461,9 @@ my_strftime (s, maxsize, format, tp extra_args) *p = L_('\0'); return i; } +#ifdef _LIBC +libc_hidden_def (my_strftime) +#endif #ifdef emacs