Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>

@@ -1,7 +1,8 @@
 /*
  * Copyright (c) 2014 Gary Mills
+ * Copyright 2014 Garrett D'Amore <garrett@damore.org>
  * Copyright 2011, Nexenta Systems, Inc.  All rights reserved.
  * Copyright (c) 1994 Powerdog Industries.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions

@@ -38,35 +39,34 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
 #include <alloca.h>
+#include <locale.h>
 #include "timelocal.h"
+#include "localeimpl.h"
 
 #define asizeof(a)      (sizeof (a) / sizeof ((a)[0]))
 
 #define F_GMT           (1 << 0)
-#define F_ZERO          (1 << 1)
 #define F_RECURSE       (1 << 2)
 
 static char *
-__strptime(const char *buf, const char *fmt, struct tm *tm, int *flagsp)
+__strptime(const char *_RESTRICT_KYWD buf, const char *_RESTRICT_KYWD fmt,
+    struct tm *_RESTRICT_KYWD tm, int *_RESTRICT_KYWD flagsp,
+    locale_t _RESTRICT_KYWD loc)
 {
         char    c;
         const char *ptr;
         int     i, len, recurse = 0;
         int Ealternative, Oalternative;
-        struct lc_time_T *tptr = __get_current_time_locale();
+        const struct lc_time *tptr = loc->time;
 
         if (*flagsp & F_RECURSE)
                 recurse = 1;
         *flagsp |= F_RECURSE;
 
-        if (*flagsp & F_ZERO)
-                (void) memset(tm, 0, sizeof (*tm));
-        *flagsp &= ~F_ZERO;
-
         ptr = fmt;
         while (*ptr != 0) {
                 if (*buf == 0)
                         break;
 

@@ -91,11 +91,11 @@
                         if (*buf++ != '%')
                                 return (NULL);
                         break;
 
                 case '+':
-                        buf = __strptime(buf, tptr->date_fmt, tm, flagsp);
+                        buf = __strptime(buf, tptr->date_fmt, tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'C':

@@ -114,17 +114,17 @@
 
                         tm->tm_year = i * 100 - 1900;
                         break;
 
                 case 'c':
-                        buf = __strptime(buf, tptr->c_fmt, tm, flagsp);
+                        buf = __strptime(buf, tptr->c_fmt, tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'D':
-                        buf = __strptime(buf, "%m/%d/%y", tm, flagsp);
+                        buf = __strptime(buf, "%m/%d/%y", tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'E':

@@ -138,41 +138,41 @@
                                 break;
                         Oalternative++;
                         goto label;
 
                 case 'F':
-                        buf = __strptime(buf, "%Y-%m-%d", tm, flagsp);
+                        buf = __strptime(buf, "%Y-%m-%d", tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'R':
-                        buf = __strptime(buf, "%H:%M", tm, flagsp);
+                        buf = __strptime(buf, "%H:%M", tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'r':
-                        buf = __strptime(buf, tptr->ampm_fmt, tm, flagsp);
+                        buf = __strptime(buf, tptr->ampm_fmt, tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'T':
-                        buf = __strptime(buf, "%H:%M:%S", tm, flagsp);
+                        buf = __strptime(buf, "%H:%M:%S", tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'X':
-                        buf = __strptime(buf, tptr->X_fmt, tm, flagsp);
+                        buf = __strptime(buf, tptr->X_fmt, tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'x':
-                        buf = __strptime(buf, tptr->x_fmt, tm, flagsp);
+                        buf = __strptime(buf, tptr->x_fmt, tm, flagsp, loc);
                         if (buf == NULL)
                                 return (NULL);
                         break;
 
                 case 'j':

@@ -517,23 +517,48 @@
 
         return ((char *)buf);
 }
 
 char *
-strptime(const char *buf, const char *fmt, struct tm *tm)
+strptime(const char *_RESTRICT_KYWD buf, const char *_RESTRICT_KYWD fmt,
+    struct tm *_RESTRICT_KYWD tm)
 {
-        int     flags = F_ZERO;
+        int     flags = 0;
 
-        return (__strptime(buf, fmt, tm, &flags));
+        (void) memset(tm, 0, sizeof (*tm));
+
+        return (__strptime(buf, fmt, tm, &flags, uselocale(NULL)));
 }
 
 /*
  * This is used by Solaris, and is a variant that does not clear the
  * incoming tm.  It is triggered by -D_STRPTIME_DONTZERO.
  */
 char *
-__strptime_dontzero(const char *buf, const char *fmt, struct tm *tm)
+__strptime_dontzero(const char *_RESTRICT_KYWD buf,
+    const char *_RESTRICT_KYWD fmt, struct tm *_RESTRICT_KYWD tm)
 {
         int     flags = 0;
 
-        return (__strptime(buf, fmt, tm, &flags));
+        return (__strptime(buf, fmt, tm, &flags, uselocale(NULL)));
+}
+
+/*
+ * strptime_l is an extension that seems natural, and indeed, MacOS X
+ * includes it within their <xlocale.h> and it is part of GNU libc as well.
+ * For now we restrict it to the cases where strict namespaces are not
+ * included.  We expect to see it in a future version of POSIX.  locale_t is
+ * not a restrict, since the spec for it doesn't assume its a pointer.  We
+ * therefore pass it analagously to the way strftime_l is specified.
+ *
+ * We are not providing a non-zeroing version at this time.
+ */
+char *
+strptime_l(const char *_RESTRICT_KYWD buf, const char *_RESTRICT_KYWD fmt,
+    struct tm *_RESTRICT_KYWD tm, locale_t loc)
+{
+        int     flags =  0;
+
+        (void) memset(tm, 0, sizeof (*tm));
+
+        return (__strptime(buf, fmt, tm, &flags, loc));
 }