This is the mail archive of the cygwin-cvs@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[newlib-cygwin] Implement GNU extension strptime_l


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=0ecb846d2ba2525ec1ec7842a8291da2a2f8f823

commit 0ecb846d2ba2525ec1ec7842a8291da2a2f8f823
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Aug 23 17:42:47 2016 +0200

    Implement GNU extension strptime_l
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 newlib/libc/include/time.h     |  5 +++
 newlib/libc/time/strptime.c    | 80 ++++++++++++++++++++++--------------------
 winsup/cygwin/common.din       |  1 +
 winsup/cygwin/libc/strptime.cc | 74 ++++++++++++++++++++++++--------------
 4 files changed, 94 insertions(+), 66 deletions(-)

diff --git a/newlib/libc/include/time.h b/newlib/libc/include/time.h
index 47ab578..ec67cb2 100644
--- a/newlib/libc/include/time.h
+++ b/newlib/libc/include/time.h
@@ -92,6 +92,11 @@ char      *_EXFUN(strptime,     (const char *__restrict,
 				 const char *__restrict,
 				 struct tm *__restrict));
 #endif
+#if __GNU_VISIBLE
+char *strptime_l (const char *__restrict, const char *__restrict,
+		  struct tm *__restrict, locale_t);
+#endif
+
 #if __POSIX_VISIBLE
 _VOID      _EXFUN(tzset,	(_VOID));
 #endif
diff --git a/newlib/libc/time/strptime.c b/newlib/libc/time/strptime.c
index a057450..c0861eb 100644
--- a/newlib/libc/time/strptime.c
+++ b/newlib/libc/time/strptime.c
@@ -30,6 +30,7 @@
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 
+#define _GNU_SOURCE
 #include <stddef.h>
 #include <stdio.h>
 #include <time.h>
@@ -68,14 +69,15 @@ is_leap_year (int year)
 
 /* Needed for strptime. */
 static int
-match_string (const char *__restrict *buf, const char * const*strs)
+match_string (const char *__restrict *buf, const char * const*strs,
+	      locale_t locale)
 {
     int i = 0;
 
     for (i = 0; strs[i] != NULL; ++i) {
 	int len = strlen (strs[i]);
 
-	if (strncasecmp (*buf, strs[i], len) == 0) {
+	if (strncasecmp_l (*buf, strs[i], len, locale) == 0) {
 	    *buf += len;
 	    return i;
 	}
@@ -148,25 +150,20 @@ set_week_number_mon4 (struct tm *timeptr, int wnum)
     }
 }
 
-/* strptime: roken */
-//extern "C"
 char *
-//strptime (const char *buf, const char *format, struct tm *timeptr)
-_DEFUN (strptime, (buf, format, timeptr),
-	_CONST char *__restrict buf _AND
-	_CONST char *__restrict format _AND
-	struct tm *__restrict timeptr)
+strptime_l (const char *buf, const char *format, struct tm *timeptr,
+	    locale_t locale)
 {
     char c;
     int ymd = 0;
 
-    const struct lc_time_T *_CurrentTimeLocale = __get_current_time_locale ();
+    const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
     for (; (c = *format) != '\0'; ++format) {
 	char *s;
 	int ret;
 
-	if (isspace ((unsigned char) c)) {
-	    while (isspace ((unsigned char) *buf))
+	if (isspace_l ((unsigned char) c, locale)) {
+	    while (isspace_l ((unsigned char) *buf, locale))
 		++buf;
 	} else if (c == '%' && format[1] != '\0') {
 	    c = *++format;
@@ -174,21 +171,21 @@ _DEFUN (strptime, (buf, format, timeptr),
 		c = *++format;
 	    switch (c) {
 	    case 'A' :
-		ret = match_string (&buf, _ctloc (weekday));
+		ret = match_string (&buf, _ctloc (weekday), locale);
 		if (ret < 0)
 		    return NULL;
 		timeptr->tm_wday = ret;
 		ymd |= SET_WDAY;
 		break;
 	    case 'a' :
-		ret = match_string (&buf, _ctloc (wday));
+		ret = match_string (&buf, _ctloc (wday), locale);
 		if (ret < 0)
 		    return NULL;
 		timeptr->tm_wday = ret;
 		ymd |= SET_WDAY;
 		break;
 	    case 'B' :
-		ret = match_string (&buf, _ctloc (month));
+		ret = match_string (&buf, _ctloc (month), locale);
 		if (ret < 0)
 		    return NULL;
 		timeptr->tm_mon = ret;
@@ -196,14 +193,14 @@ _DEFUN (strptime, (buf, format, timeptr),
 		break;
 	    case 'b' :
 	    case 'h' :
-		ret = match_string (&buf, _ctloc (mon));
+		ret = match_string (&buf, _ctloc (mon), locale);
 		if (ret < 0)
 		    return NULL;
 		timeptr->tm_mon = ret;
 		ymd |= SET_MON;
 		break;
 	    case 'C' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_year = (ret * 100) - tm_year_base;
@@ -211,14 +208,14 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_YEAR;
 		break;
 	    case 'c' :		/* %a %b %e %H:%M:%S %Y */
-		s = strptime (buf, _ctloc (c_fmt), timeptr);
+		s = strptime_l (buf, _ctloc (c_fmt), timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
 		ymd |= SET_WDAY | SET_YMD;
 		break;
 	    case 'D' :		/* %m/%d/%y */
-		s = strptime (buf, "%m/%d/%y", timeptr);
+		s = strptime_l (buf, "%m/%d/%y", timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
@@ -226,7 +223,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		break;
 	    case 'd' :
 	    case 'e' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_mday = ret;
@@ -235,7 +232,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		break;
 	    case 'H' :
 	    case 'k' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_hour = ret;
@@ -243,7 +240,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		break;
 	    case 'I' :
 	    case 'l' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		if (ret == 12)
@@ -253,7 +250,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		buf = s;
 		break;
 	    case 'j' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_yday = ret - 1;
@@ -261,7 +258,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_YDAY;
 		break;
 	    case 'm' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_mon = ret - 1;
@@ -269,7 +266,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_MON;
 		break;
 	    case 'M' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_min = ret;
@@ -282,7 +279,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		    return NULL;
 		break;
 	    case 'p' :
-		ret = match_string (&buf, _ctloc (am_pm));
+		ret = match_string (&buf, _ctloc (am_pm), locale);
 		if (ret < 0)
 		    return NULL;
 		if (timeptr->tm_hour == 0) {
@@ -292,19 +289,19 @@ _DEFUN (strptime, (buf, format, timeptr),
 		    timeptr->tm_hour += 12;
 		break;
 	    case 'r' :		/* %I:%M:%S %p */
-		s = strptime (buf, _ctloc (ampm_fmt), timeptr);
+		s = strptime_l (buf, _ctloc (ampm_fmt), timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
 		break;
 	    case 'R' :		/* %H:%M */
-		s = strptime (buf, "%H:%M", timeptr);
+		s = strptime_l (buf, "%H:%M", timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
 		break;
 	    case 'S' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_sec = ret;
@@ -317,13 +314,13 @@ _DEFUN (strptime, (buf, format, timeptr),
 		    return NULL;
 		break;
 	    case 'T' :		/* %H:%M:%S */
-		s = strptime (buf, "%H:%M:%S", timeptr);
+		s = strptime_l (buf, "%H:%M:%S", timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
 		break;
 	    case 'u' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_wday = ret - 1;
@@ -331,7 +328,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_WDAY;
 		break;
 	    case 'w' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_wday = ret;
@@ -339,7 +336,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_WDAY;
 		break;
 	    case 'U' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		set_week_number_sun (timeptr, ret);
@@ -347,7 +344,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_YDAY;
 		break;
 	    case 'V' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		set_week_number_mon4 (timeptr, ret);
@@ -355,7 +352,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_YDAY;
 		break;
 	    case 'W' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		set_week_number_mon (timeptr, ret);
@@ -363,20 +360,20 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_YDAY;
 		break;
 	    case 'x' :
-		s = strptime (buf, _ctloc (x_fmt), timeptr);
+		s = strptime_l (buf, _ctloc (x_fmt), timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
 		ymd |= SET_YMD;
 		break;
 	    case 'X' :
-		s = strptime (buf, _ctloc (X_fmt), timeptr);
+		s = strptime_l (buf, _ctloc (X_fmt), timeptr, locale);
 		if (s == NULL)
 		    return NULL;
 		buf = s;
 	    	break;
 	    case 'y' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		if (ret < 70)
@@ -387,7 +384,7 @@ _DEFUN (strptime, (buf, format, timeptr),
 		ymd |= SET_YEAR;
 		break;
 	    case 'Y' :
-		ret = strtol (buf, &s, 10);
+		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)
 		    return NULL;
 		timeptr->tm_year = ret - tm_year_base;
@@ -475,3 +472,8 @@ _DEFUN (strptime, (buf, format, timeptr),
     return (char *)buf;
 }
 
+char *
+strptime (const char *buf, const char *format, struct tm *timeptr)
+{
+  return strptime_l (buf, format, timeptr, __get_current_locale ());
+}
diff --git a/winsup/cygwin/common.din b/winsup/cygwin/common.din
index 12705fe..b0064c8 100644
--- a/winsup/cygwin/common.din
+++ b/winsup/cygwin/common.din
@@ -1364,6 +1364,7 @@ strndup SIGFE
 strnlen NOSIGFE
 strpbrk NOSIGFE
 strptime SIGFE
+strptime_l SIGFE
 strrchr NOSIGFE
 strsep NOSIGFE
 strsignal SIGFE
diff --git a/winsup/cygwin/libc/strptime.cc b/winsup/cygwin/libc/strptime.cc
index c2bf17b..efa4e5b 100644
--- a/winsup/cygwin/libc/strptime.cc
+++ b/winsup/cygwin/libc/strptime.cc
@@ -102,7 +102,7 @@ free_era_info (era_info_t *era_info)
 }
 
 static era_info_t *
-get_era_info (const char *era)
+get_era_info (const char *era, locale_t locale)
 {
   char *c;
   era_info_t *ei = NULL;
@@ -122,14 +122,14 @@ get_era_info (const char *era)
       ei[cur].num = 1;
       ei[cur].dir = (*era == '+') ? 1 : -1;
       era += 2;
-      ei[cur].offset = strtol (era, &c, 10);
+      ei[cur].offset = strtol_l (era, &c, 10, locale);
       era = c + 1;
-      ei[cur].start.tm_year = strtol (era, &c, 10);
+      ei[cur].start.tm_year = strtol_l (era, &c, 10, locale);
       /* Adjust offset for negative gregorian dates. */
       if (ei[cur].start.tm_year < 0)
 	++ei[cur].start.tm_year;
-      ei[cur].start.tm_mon = strtol (c + 1, &c, 10);
-      ei[cur].start.tm_mday = strtol (c + 1, &c, 10);
+      ei[cur].start.tm_mon = strtol_l (c + 1, &c, 10, locale);
+      ei[cur].start.tm_mday = strtol_l (c + 1, &c, 10, locale);
       ei[cur].start.tm_hour = ei[cur].start.tm_min = ei[cur].start.tm_sec = 0;
       era = c + 1;
       if (era[0] == '-' && era[1] == '*')
@@ -151,12 +151,12 @@ get_era_info (const char *era)
 	}
       else
 	{
-	  ei[cur].end.tm_year = strtol (era, &c, 10);
+	  ei[cur].end.tm_year = strtol_l (era, &c, 10, locale);
 	  /* Adjust offset for negative gregorian dates. */
 	  if (ei[cur].end.tm_year < 0)
 	    ++ei[cur].end.tm_year;
-	  ei[cur].end.tm_mon = strtol (c + 1, &c, 10);
-	  ei[cur].end.tm_mday = strtol (c + 1, &c, 10);
+	  ei[cur].end.tm_mon = strtol_l (c + 1, &c, 10, locale);
+	  ei[cur].end.tm_mday = strtol_l (c + 1, &c, 10, locale);
 	  ei[cur].end.tm_mday = 31;
 	  ei[cur].end.tm_hour = 23;
 	  ei[cur].end.tm_min = ei[cur].end.tm_sec = 59;
@@ -305,11 +305,13 @@ static const unsigned char *conv_num(const unsigned char *, int *, uint, uint,
 				     alt_digits_t *);
 static const unsigned char *find_string(const unsigned char *, int *,
 					const char * const *,
-					const char * const *, int);
+					const char * const *, int,
+					locale_t);
 
 static char *
 __strptime(const char *buf, const char *fmt, struct tm *tm,
-	   era_info_t **era_info, alt_digits_t **alt_digits)
+	   era_info_t **era_info, alt_digits_t **alt_digits,
+	   locale_t locale)
 {
 	unsigned char c;
 	const unsigned char *bp;
@@ -323,8 +325,7 @@ __strptime(const char *buf, const char *fmt, struct tm *tm,
 	int ymd = 0;
 
 	bp = (const unsigned char *)buf;
-	const struct lc_time_T *_CurrentTimeLocale =
-						__get_current_time_locale ();
+	const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
 
 	while (bp != NULL && (c = *fmt++) != '\0') {
 		/* Clear `alternate' modifier prior to new conversion. */
@@ -334,8 +335,8 @@ __strptime(const char *buf, const char *fmt, struct tm *tm,
 		i = 0;
 
 		/* Eat up white-space. */
-		if (isspace(c)) {
-			while (isspace(*bp))
+		if (isspace_l(c, locale)) {
+			while (isspace_l(*bp, locale))
 				bp++;
 			continue;
 		}
@@ -360,7 +361,8 @@ literal:
 			LEGAL_ALT(0);
 			alt_format |= ALT_E;
 			if (!*era_info && *_CurrentTimeLocale->era)
-			  *era_info = get_era_info (_CurrentTimeLocale->era);
+			  *era_info = get_era_info (_CurrentTimeLocale->era,
+						    locale);
 			goto again;
 
 		case 'O':	/* "%O?" alternative conversion modifier. */
@@ -385,7 +387,7 @@ literal:
 			LEGAL_ALT(0);
 			{
 			  char *end;
-			  width = strtoul (fmt - 1, &end, 10);
+			  width = strtoul_l (fmt - 1, &end, 10, locale);
 			  fmt = (const char *) end;
 			  goto again;
 			}
@@ -410,7 +412,8 @@ literal:
 			  LEGAL_ALT(0);
 			  ymd |= SET_YMD;
 			  char *tmp = __strptime ((const char *) bp, "%Y-%m-%d",
-						  tm, era_info, alt_digits);
+						  tm, era_info, alt_digits,
+						  locale);
 			  if (tmp && (uint) (tmp - (char *) bp) > width)
 			    return NULL;
 			  bp = (const unsigned char *) tmp;
@@ -446,7 +449,7 @@ literal:
 		    recurse:
 			bp = (const unsigned char *)
 			     __strptime((const char *)bp, new_fmt, tm,
-					era_info, alt_digits);
+					era_info, alt_digits, locale);
 			continue;
 
 		/*
@@ -455,7 +458,7 @@ literal:
 		case 'A':	/* The day of week, using the locale's form. */
 		case 'a':
 			bp = find_string(bp, &tm->tm_wday, _ctloc(weekday),
-					_ctloc(wday), 7);
+					_ctloc(wday), 7, locale);
 			LEGAL_ALT(0);
 			ymd |= SET_WDAY;
 			continue;
@@ -464,7 +467,7 @@ literal:
 		case 'b':
 		case 'h':
 			bp = find_string(bp, &tm->tm_mon, _ctloc(month),
-					_ctloc(mon), 12);
+					_ctloc(mon), 12, locale);
 			LEGAL_ALT(0);
 			ymd |= SET_WDAY;
 			continue;
@@ -554,7 +557,8 @@ literal:
 			continue;
 
 		case 'p':	/* The locale's equivalent of AM/PM. */
-			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
+			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2,
+					 locale);
 			if (tm->tm_hour > 11)
 				return NULL;
 			tm->tm_hour += i * 12;
@@ -604,7 +608,7 @@ literal:
 				char *tmp = __strptime ((const char *) bp,
 							tmp_ei->era_Y,
 							tm, &tmp_ei,
-							alt_digits);
+							alt_digits, locale);
 				if (tmp)
 				  {
 				    bp = (const unsigned char *) tmp;
@@ -675,7 +679,7 @@ literal:
 
 				ep = find_string(bp, &i,
 					       	 (const char * const *)tzname,
-					       	  NULL, 2);
+					       	  NULL, 2, locale);
 				if (ep != NULL) {
 					tm->tm_isdst = i;
 #ifdef TM_GMTOFF
@@ -696,7 +700,7 @@ literal:
 		 */
 		case 'n':	/* Any kind of white-space. */
 		case 't':
-			while (isspace(*bp))
+			while (isspace_l(*bp, locale))
 				bp++;
 			LEGAL_ALT(0);
 			continue;
@@ -776,12 +780,27 @@ literal:
 }
 
 char *
+strptime_l (const char *__restrict buf, const char *__restrict fmt,
+	    struct tm *__restrict tm, locale_t locale)
+{
+  era_info_t *era_info = NULL;
+  alt_digits_t *alt_digits = NULL;
+  char *ret = __strptime (buf, fmt, tm, &era_info, &alt_digits, locale);
+  if (era_info)
+    free_era_info (era_info);
+  if (alt_digits)
+    free_alt_digits (alt_digits);
+  return ret;
+}
+
+char *
 strptime (const char *__restrict buf, const char *__restrict fmt,
 	  struct tm *__restrict tm)
 {
   era_info_t *era_info = NULL;
   alt_digits_t *alt_digits = NULL;
-  char *ret = __strptime (buf, fmt, tm, &era_info, &alt_digits);
+  char *ret = __strptime (buf, fmt, tm, &era_info, &alt_digits,
+			  __get_current_locale ());
   if (era_info)
     free_era_info (era_info);
   if (alt_digits)
@@ -824,7 +843,7 @@ conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim,
 
 static const unsigned char *
 find_string(const unsigned char *bp, int *tgt, const char * const *n1,
-	    const char * const *n2, int c)
+	    const char * const *n2, int c, locale_t locale)
 {
 	int i;
 	unsigned int len;
@@ -833,7 +852,8 @@ find_string(const unsigned char *bp, int *tgt, const char * const *n1,
 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
 		for (i = 0; i < c; i++, n1++) {
 			len = strlen(*n1);
-			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
+			if (strncasecmp_l(*n1, (const char *)bp, len,
+					  locale) == 0) {
 				*tgt = i;
 				return bp + len;
 			}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]