Don't redeclare strsep if the system already has it.
[gnulib.git] / lib / strftime.c
index c3b2148..dccbaf7 100644 (file)
@@ -1,29 +1,27 @@
-/* Copyright (C) 1991-1999, 2000 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 <config.h>
 #endif
 
 #ifdef _LIBC
-# define HAVE_LIMITS_H 1
 # define HAVE_MBLEN 1
 # define HAVE_MBRLEN 1
 # define HAVE_STRUCT_ERA_ENTRY 1
 # define HAVE_TZNAME 1
 # define HAVE_TZSET 1
 # define MULTIBYTE_IS_FORMAT_SAFE 1
-# define STDC_HEADERS 1
 # include "../locale/localeinfo.h"
 #endif
 
-#if defined emacs && !defined HAVE_BCOPY
-# define HAVE_MEMCPY 1
-#endif
-
 #include <ctype.h>
 #include <sys/types.h>         /* Some systems define `time_t' here.  */
 
@@ -77,19 +70,10 @@ extern char *tzname[];
   static const mbstate_t mbstate_zero;
 #endif
 
-#if HAVE_LIMITS_H
-# include <limits.h>
-#endif
-
-#if STDC_HEADERS
-# include <stddef.h>
-# include <stdlib.h>
-# include <string.h>
-#else
-# ifndef HAVE_MEMCPY
-#  define memcpy(d, s, n) bcopy ((s), (d), (n))
-# endif
-#endif
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
 
 #ifdef COMPILE_WIDE
 # include <endian.h>
@@ -107,11 +91,7 @@ extern char *tzname[];
 # define L_(Str) Str
 # define NLW(Sym) Sym
 
-# if !defined STDC_HEADERS && !defined HAVE_MEMCPY
-#  define MEMCPY(d, s, n) bcopy ((s), (d), (n))
-# else
-#  define MEMCPY(d, s, n) memcpy ((d), (s), (n))
-# endif
+# define MEMCPY(d, s, n) memcpy (d, s, n)
 # define STRLEN(s) strlen (s)
 
 # ifdef _LIBC
@@ -123,30 +103,6 @@ extern char *tzname[];
 # endif
 #endif
 
-#ifndef __P
-# if defined __GNUC__ || (defined __STDC__ && __STDC__)
-#  define __P(args) args
-# else
-#  define __P(args) ()
-# endif  /* GCC.  */
-#endif  /* Not __P.  */
-
-#ifndef PTR
-# ifdef __STDC__
-#  define PTR void *
-# else
-#  define PTR char *
-# endif
-#endif
-
-#ifndef CHAR_BIT
-# define CHAR_BIT 8
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
-
 #define TYPE_SIGNED(t) ((t) -1 < 0)
 
 /* Bound on length of the string representing an integer value of type t.
@@ -168,95 +124,29 @@ extern char *tzname[];
 
 
 #ifdef _LIBC
-# define my_strftime_gmtime_r __gmtime_r
-# define my_strftime_localtime_r __localtime_r
 # define tzname __tzname
 # define tzset __tzset
-#else
-
-/* If we're a strftime substitute in a GNU program, then prefer gmtime
-   to gmtime_r, since many gmtime_r implementations are buggy.
-   Similarly for localtime_r.  */
-
-# if ! HAVE_TM_GMTOFF
-static struct tm *my_strftime_gmtime_r __P ((const time_t *, struct tm *));
-static struct tm *
-my_strftime_gmtime_r (t, tp)
-     const time_t *t;
-     struct tm *tp;
-{
-  struct tm *l = gmtime (t);
-  if (! l)
-    return 0;
-  *tp = *l;
-  return tp;
-}
-# endif /* ! HAVE_TM_GMTOFF */
+#endif
 
-static struct tm *my_strftime_localtime_r __P ((const time_t *, struct tm *));
-static struct tm *
-my_strftime_localtime_r (t, tp)
-     const time_t *t;
-     struct tm *tp;
-{
-  struct tm *l = localtime (t);
-  if (! l)
-    return 0;
-  *tp = *l;
-  return tp;
-}
-#endif /* ! defined _LIBC */
+#if !HAVE_TM_GMTOFF
+/* Portable standalone applications should supply a "time_r.h" that
+   declares a POSIX-compliant localtime_r, for the benefit of older
+   implementations that lack localtime_r or have a nonstandard one.
+   See the gnulib time_r module for one way to implement this.  */
+# include "time_r.h"
+# undef __gmtime_r
+# undef __localtime_r
+# define __gmtime_r gmtime_r
+# define __localtime_r localtime_r
+#endif
 
 
-#if !defined memset && !defined HAVE_MEMSET && !defined _LIBC
-/* Some systems lack the `memset' function and we don't want to
-   introduce additional dependencies.  */
-/* The SGI compiler reportedly barfs on the trailing null
-   if we use a string constant as the initializer.  28 June 1997, rms.  */
-static const CHAR_T spaces[16] = /* "                " */
-{
-  L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),
-  L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' ')
-};
-static const CHAR_T zeroes[16] = /* "0000000000000000" */
-{
-  L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),
-  L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0')
-};
-
-# define memset_space(P, Len) \
-  do {                                                                       \
-    int _len = (Len);                                                        \
-                                                                             \
-    do                                                                       \
-      {                                                                              \
-       int _this = _len > 16 ? 16 : _len;                                    \
-       (P) = MEMPCPY ((P), spaces, _this * sizeof (CHAR_T));                 \
-       _len -= _this;                                                        \
-      }                                                                              \
-    while (_len > 0);                                                        \
-  } while (0)
-
-# define memset_zero(P, Len) \
-  do {                                                                       \
-    int _len = (Len);                                                        \
-                                                                             \
-    do                                                                       \
-      {                                                                              \
-       int _this = _len > 16 ? 16 : _len;                                    \
-       (P) = MEMPCPY ((P), zeroes, _this * sizeof (CHAR_T));                 \
-       _len -= _this;                                                        \
-      }                                                                              \
-    while (_len > 0);                                                        \
-  } while (0)
+#ifdef COMPILE_WIDE
+# define memset_space(P, Len) (wmemset (P, L' ', Len), (P) += (Len))
+# define memset_zero(P, Len) (wmemset (P, L'0', Len), (P) += (Len))
 #else
-# ifdef COMPILE_WIDE
-#  define memset_space(P, Len) (wmemset ((P), L' ', (Len)), (P) += (Len))
-#  define memset_zero(P, Len) (wmemset ((P), L'0', (Len)), (P) += (Len))
-# else
-#  define memset_space(P, Len) (memset ((P), ' ', (Len)), (P) += (Len))
-#  define memset_zero(P, Len) (memset ((P), '0', (Len)), (P) += (Len))
-# endif
+# define memset_space(P, Len) (memset (P, ' ', Len), (P) += (Len))
+# define memset_zero(P, Len) (memset (P, '0', Len), (P) += (Len))
 #endif
 
 #define add(n, f)                                                            \
@@ -265,7 +155,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 +175,72 @@ 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, (PTR) (s), _n))
+          MEMCPY ((void *) p, (void const *) (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_ARG , loc
+# define LOCALE_PARAM_PROTO , __locale_t loc
+# define HELPER_LOCALE_ARG  , current
+#else
+# define LOCALE_PARAM_PROTO
+# define LOCALE_ARG
+# 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
@@ -322,31 +249,21 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */
    more reliable way to accept other sets of digits.  */
 #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));
-
 static CHAR_T *
-memcpy_lowcase (dest, src, len)
-     CHAR_T *dest;
-     const CHAR_T *src;
-     size_t len;
+memcpy_lowcase (CHAR_T *dest, const CHAR_T *src,
+               size_t len LOCALE_PARAM_PROTO)
 {
   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));
-
 static CHAR_T *
-memcpy_uppcase (dest, src, len)
-     CHAR_T *dest;
-     const CHAR_T *src;
-     size_t len;
+memcpy_uppcase (CHAR_T *dest, const CHAR_T *src,
+               size_t len LOCALE_PARAM_PROTO)
 {
   while (len-- > 0)
-    dest[len] = TOUPPER ((UCHAR_T) src[len]);
+    dest[len] = TOUPPER ((UCHAR_T) src[len], loc);
   return dest;
 }
 
@@ -355,11 +272,8 @@ memcpy_uppcase (dest, src, len)
 /* Yield the difference between *A and *B,
    measured in seconds, ignoring leap seconds.  */
 # define tm_diff ftime_tm_diff
-static int tm_diff __P ((const struct tm *, const struct tm *));
 static int
-tm_diff (a, b)
-     const struct tm *a;
-     const struct tm *b;
+tm_diff (const struct tm *a, const struct tm *b)
 {
   /* Compute intervening leap days correctly even if year is negative.
      Take care to avoid int overflow in leap day calculations,
@@ -389,14 +303,11 @@ tm_diff (a, b)
 #define ISO_WEEK_START_WDAY 1 /* Monday */
 #define ISO_WEEK1_WDAY 4 /* Thursday */
 #define YDAY_MINIMUM (-366)
-static int iso_week_days __P ((int, int));
 #ifdef __GNUC__
 __inline__
 #endif
 static int
-iso_week_days (yday, wday)
-     int yday;
-     int wday;
+iso_week_days (int yday, int wday)
 {
   /* Add enough to the first operand of % to make it nonnegative.  */
   int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
@@ -421,44 +332,38 @@ static CHAR_T const month_name[][10] =
 #endif
 
 
-#ifdef emacs
-# define my_strftime emacs_strftimeu
-# define ut_argument , ut
-# define ut_argument_spec int ut;
-# define ut_argument_spec_iso , int ut
+/* When compiling this file, GNU applications can #define my_strftime
+   to a symbol (typically nstrftime) to get an extended strftime with
+   extra arguments UT and NS.  Emacs is a special case for now, but
+   this Emacs-specific code can be removed once Emacs's config.h
+   defines my_strftime.  */
+#if defined emacs && !defined my_strftime
+# define my_strftime nstrftime
+#endif
+
+#ifdef my_strftime
+# define extra_args , ut, ns
+# define extra_args_spec , int ut, int ns
 #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 ut_argument
-# define ut_argument_spec
-# define ut_argument_spec_iso
+# define extra_args
+# define extra_args_spec
 /* We don't have this information in general.  */
 # define ut 0
+# define ns 0
 #endif
 
-#if !defined _LIBC && HAVE_TZNAME && HAVE_TZSET
-  /* Solaris 2.5 tzset sometimes modifies the storage returned by localtime.
-     Work around this bug by copying *tp before it might be munged.  */
-  size_t _strftime_copytm __P ((char *, size_t, const char *,
-                               const struct tm * ut_argument_spec_iso));
-  size_t
-  my_strftime (s, maxsize, format, tp ut_argument)
-      CHAR_T *s;
-      size_t maxsize;
-      const CHAR_T *format;
-      const struct tm *tp;
-      ut_argument_spec
-  {
-    struct tm tmcopy;
-    tmcopy = *tp;
-    return _strftime_copytm (s, maxsize, format, &tmcopy ut_argument);
-  }
-# undef my_strftime
-# define my_strftime(S, Maxsize, Format, Tp) \
-  _strftime_copytm (S, Maxsize, Format, Tp)
+#if ! defined _LIBC && ! HAVE_RUN_TZSET_TEST
+/* Solaris 2.5.x and 2.6 tzset sometimes modify the storage returned
+   by localtime.  On such systems, we must use the tzset and localtime
+   wrappers to work around the bug.  */
+"you must run the autoconf test for a working tzset function"
 #endif
 
 
@@ -469,13 +374,13 @@ 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 ut_argument)
-      CHAR_T *s;
-      size_t maxsize;
-      const CHAR_T *format;
-      const struct tm *tp;
-      ut_argument_spec
+my_strftime (CHAR_T *s, size_t maxsize, const CHAR_T *format,
+            const struct tm *tp extra_args_spec LOCALE_PARAM_PROTO)
 {
+#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
@@ -516,6 +421,9 @@ my_strftime (s, maxsize, format, tp ut_argument)
   size_t i = 0;
   CHAR_T *p = s;
   const CHAR_T *f;
+#if DO_MULTIBYTE && !defined COMPILE_WIDE
+  const char *format_end = NULL;
+#endif
 
   zone = NULL;
 #if HAVE_TM_ZONE
@@ -535,9 +443,8 @@ my_strftime (s, maxsize, format, tp ut_argument)
     }
   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
@@ -555,7 +462,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
       int pad = 0;             /* Padding for number ('-', '_', or 0).  */
       int modifier;            /* Field modifier ('E', 'O', or 0).  */
       int digits;              /* Max digits for numeric format.  */
-      int number_value;        /* Numeric value to be printed.  */
+      int number_value;                /* Numeric value to be printed.  */
       int negative_number;     /* 1 if the number is negative.  */
       const CHAR_T *subfmt;
       CHAR_T *bufp;
@@ -608,10 +515,15 @@ my_strftime (s, maxsize, format, tp ut_argument)
          {
            mbstate_t mbstate = mbstate_zero;
            size_t len = 0;
+           size_t fsize;
+
+           if (! format_end)
+             format_end = f + strlen (f) + 1;
+           fsize = format_end - f;
 
            do
              {
-               size_t bytes = mbrlen (f + len, (size_t) -1, &mbstate);
+               size_t bytes = mbrlen (f + len, fsize - len, &mbstate);
 
                if (bytes == 0)
                  break;
@@ -683,8 +595,15 @@ my_strftime (s, maxsize, format, tp ut_argument)
          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));
@@ -708,10 +627,10 @@ my_strftime (s, maxsize, format, tp ut_argument)
       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_('%'):
@@ -751,7 +670,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
 #endif
 
        case L_('b'):
-       case L_('h'):           /* POSIX.2 extension.  */
+       case L_('h'):
          if (change_case)
            {
              to_uppcase = 1;
@@ -786,10 +705,11 @@ my_strftime (s, maxsize, format, tp ut_argument)
            goto bad_format;
 #ifdef _NL_CURRENT
          if (! (modifier == 'E'
-                && (*(subfmt = (CHAR_T *) _NL_CURRENT (LC_TIME,
-                                                       NLW(ERA_D_T_FMT)))
+                && (*(subfmt =
+                      (const CHAR_T *) _NL_CURRENT (LC_TIME,
+                                                    NLW(ERA_D_T_FMT)))
                     != '\0')))
-           subfmt = (CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_T_FMT));
+           subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_T_FMT));
 #else
 # if HAVE_STRFTIME
          goto underlying_strftime;
@@ -801,13 +721,15 @@ my_strftime (s, maxsize, format, tp ut_argument)
        subformat:
          {
            CHAR_T *old_start = p;
-           size_t len = my_strftime (NULL, (size_t) -1, subfmt, tp);
-           add (len, my_strftime (p, maxsize - i, subfmt, tp));
+           size_t len = my_strftime (NULL, (size_t) -1, subfmt,
+                                     tp extra_args LOCALE_ARG);
+           add (len, my_strftime (p, maxsize - i, subfmt,
+                                  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;
                }
          }
@@ -843,13 +765,13 @@ my_strftime (s, maxsize, format, tp ut_argument)
          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
@@ -878,10 +800,10 @@ my_strftime (s, maxsize, format, tp ut_argument)
            goto bad_format;
 #ifdef _NL_CURRENT
          if (! (modifier == L_('E')
-                && (*(subfmt = (CHAR_T *)_NL_CURRENT (LC_TIME,
-                                                      NLW(ERA_D_FMT)))
+                && (*(subfmt =
+                      (const CHAR_T *)_NL_CURRENT (LC_TIME, NLW(ERA_D_FMT)))
                     != L_('\0'))))
-           subfmt = (CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_FMT));
+           subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_FMT));
          goto subformat;
 #else
 # if HAVE_STRFTIME
@@ -890,7 +812,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
          /* Fall through.  */
 # endif
 #endif
-       case L_('D'):           /* POSIX.2 extension.  */
+       case L_('D'):
          if (modifier != 0)
            goto bad_format;
          subfmt = L_("%m/%d/%y");
@@ -902,7 +824,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
 
          DO_NUMBER (2, tp->tm_mday);
 
-       case L_('e'):           /* POSIX.2 extension.  */
+       case L_('e'):
          if (modifier == L_('E'))
            goto bad_format;
 
@@ -912,8 +834,8 @@ my_strftime (s, maxsize, format, tp ut_argument)
             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:
@@ -924,11 +846,8 @@ my_strftime (s, maxsize, format, tp ut_argument)
 #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)
                {
@@ -957,7 +876,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
            do
              *--bufp = u % 10 + L_('0');
            while ((u /= 10) != 0);
-         }
+         }
 
        do_number_sign_and_padding:
          if (negative_number)
@@ -968,18 +887,37 @@ my_strftime (s, maxsize, format, tp ut_argument)
              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;
+                   }
                }
            }
 
@@ -1034,7 +972,24 @@ my_strftime (s, maxsize, format, tp ut_argument)
 
          DO_NUMBER (2, tp->tm_mon + 1);
 
-       case L_('n'):           /* POSIX.2 extension.  */
+#ifndef _LIBC
+       case L_('N'):           /* GNU extension.  */
+         if (modifier == L_('E'))
+           goto bad_format;
+
+         number_value = ns;
+         if (width != -1)
+           {
+             /* Take an explicit width less than 9 as a precision.  */
+             int j;
+             for (j = width; j < 9; j++)
+               number_value /= 10;
+           }
+
+         DO_NUMBER (9, number_value);
+#endif
+
+       case L_('n'):
          add (1, *p = L_('\n'));
          break;
 
@@ -1058,17 +1013,22 @@ my_strftime (s, maxsize, format, tp ut_argument)
          goto underlying_strftime;
 #endif
 
-       case L_('R'):           /* GNU extension.  */
+       case L_('R'):
          subfmt = L_("%H:%M");
          goto subformat;
 
-       case L_('r'):           /* POSIX.2 extension.  */
-#ifdef _NL_CURRENT
-         if (*(subfmt = (CHAR_T *) _NL_CURRENT (LC_TIME,
-                                                NLW(T_FMT_AMPM))) == L_('\0'))
-#endif
+       case L_('r'):
+#if !defined _NL_CURRENT && HAVE_STRFTIME
+         goto underlying_strftime;
+#else
+# ifdef _NL_CURRENT
+         if (*(subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME,
+                                                      NLW(T_FMT_AMPM)))
+             == L_('\0'))
+# endif
            subfmt = L_("%I:%M:%S %p");
          goto subformat;
+#endif
 
        case L_('S'):
          if (modifier == L_('E'))
@@ -1077,7 +1037,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
          DO_NUMBER (2, tp->tm_sec);
 
        case L_('s'):           /* GNU extension.  */
-         {
+         {
            struct tm ltm;
            time_t t;
 
@@ -1120,10 +1080,10 @@ my_strftime (s, maxsize, format, tp ut_argument)
            goto bad_format;
 #ifdef _NL_CURRENT
          if (! (modifier == L_('E')
-                && (*(subfmt = (CHAR_T *) _NL_CURRENT (LC_TIME,
-                                                       NLW(ERA_T_FMT)))
+                && (*(subfmt =
+                      (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ERA_T_FMT)))
                     != L_('\0'))))
-           subfmt = (CHAR_T *) _NL_CURRENT (LC_TIME, NLW(T_FMT));
+           subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(T_FMT));
          goto subformat;
 #else
 # if HAVE_STRFTIME
@@ -1132,15 +1092,15 @@ my_strftime (s, maxsize, format, tp ut_argument)
          /* 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'):
@@ -1150,8 +1110,8 @@ my_strftime (s, maxsize, format, tp ut_argument)
          DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7);
 
        case L_('V'):
-       case L_('g'):           /* GNU extension.  */
-       case L_('G'):           /* GNU extension.  */
+       case L_('g'):
+       case L_('G'):
          if (modifier == L_('E'))
            goto bad_format;
          {
@@ -1206,7 +1166,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
          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
@@ -1231,7 +1191,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
          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];
@@ -1259,7 +1219,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
            zone = tzname[tp->tm_isdst];
 #endif
          if (! zone)
-           zone = "";          /* POSIX.2 requires the empty string here.  */
+           zone = "";
 
 #ifdef COMPILE_WIDE
          {
@@ -1275,7 +1235,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
 #endif
          break;
 
-       case L_('z'):           /* GNU extension.  */
+       case L_('z'):
          if (tp->tm_isdst < 0)
            break;
 
@@ -1302,7 +1262,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
                       occurred.  */
                    struct tm tm;
 
-                   if (! my_strftime_localtime_r (&lt, &tm)
+                   if (! __localtime_r (&lt, &tm)
                        || ((ltm.tm_sec ^ tm.tm_sec)
                            | (ltm.tm_min ^ tm.tm_min)
                            | (ltm.tm_hour ^ tm.tm_hour)
@@ -1312,7 +1272,7 @@ my_strftime (s, maxsize, format, tp ut_argument)
                      break;
                  }
 
-               if (! my_strftime_gmtime_r (&lt, &gtm))
+               if (! __gmtime_r (&lt, &gtm))
                  break;
 
                diff = tm_diff (&ltm, &gtm);
@@ -1353,19 +1313,18 @@ my_strftime (s, maxsize, format, tp ut_argument)
     *p = L_('\0');
   return i;
 }
+#ifdef _LIBC
+libc_hidden_def (my_strftime)
+#endif
 
 
 #ifdef emacs
 /* For Emacs we have a separate interface which corresponds to the normal
-   strftime function and does not have the extra information whether the
-   TP arguments comes from a `gmtime' call or not.  */
+   strftime function plus the ut argument, but without the ns argument.  */
 size_t
-emacs_strftime (s, maxsize, format, tp)
-      char *s;
-      size_t maxsize;
-      const char *format;
-      const struct tm *tp;
+emacs_strftimeu (char *s, size_t maxsize, const char *format,
+                const struct tm *tp, int ut)
 {
-  return my_strftime (s, maxsize, format, tp, 0);
+  return my_strftime (s, maxsize, format, tp, ut, 0);
 }
 #endif