fix unhandled cases in strptime
[oweals/musl.git] / src / time / strptime.c
1 #include <stdlib.h>
2 #include <langinfo.h>
3 #include <time.h>
4 #include <ctype.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <strings.h>
8
9 char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm)
10 {
11         int i, w, neg, adj, min, range, *dest, dummy;
12         const char *ex;
13         size_t len;
14         while (*f) {
15                 if (*f != '%') {
16                         if (isspace(*f)) for (; *s && isspace(*s); s++);
17                         else if (*s != *f) return 0;
18                         else s++;
19                         f++;
20                         continue;
21                 }
22                 f++;
23                 if (*f == '+') f++;
24                 if (isdigit(*f)) w=strtoul(f, (void *)&f, 10);
25                 else w=-1;
26                 adj=0;
27                 switch (*f++) {
28                 case 'a': case 'A':
29                         dest = &tm->tm_wday;
30                         min = ABDAY_1;
31                         range = 7;
32                         goto symbolic_range;
33                 case 'b': case 'B': case 'h':
34                         dest = &tm->tm_mon;
35                         min = ABMON_1;
36                         range = 12;
37                         goto symbolic_range;
38                 case 'c':
39                         s = strptime(s, nl_langinfo(D_T_FMT), tm);
40                         if (!s) return 0;
41                         break;
42                 case 'C':
43                         /* FIXME */
44                         dest = &dummy;
45                         if (w<0) w=2;
46                         goto numeric_digits;
47                 case 'd': case 'e':
48                         dest = &tm->tm_mday;
49                         min = 1;
50                         range = 31;
51                         goto numeric_range;
52                 case 'D':
53                         s = strptime(s, "%m/%d/%y", tm);
54                         if (!s) return 0;
55                         break;
56                 case 'H':
57                         dest = &tm->tm_hour;
58                         min = 0;
59                         range = 24;
60                         goto numeric_range;
61                 case 'I':
62                         dest = &tm->tm_hour;
63                         min = 1;
64                         range = 12;
65                         goto numeric_range;
66                 case 'j':
67                         dest = &tm->tm_yday;
68                         min = 1;
69                         range = 366;
70                         goto numeric_range;
71                 case 'm':
72                         dest = &tm->tm_mon;
73                         min = 1;
74                         range = 12;
75                         adj = 1;
76                         goto numeric_range;
77                 case 'M':
78                         dest = &tm->tm_min;
79                         min = 0;
80                         range = 60;
81                         goto numeric_range;
82                 case 'n': case 't':
83                         for (; *s && isspace(*s); s++);
84                         break;
85                 case 'p':
86                         ex = nl_langinfo(AM_STR);
87                         len = strlen(ex);
88                         if (!strncasecmp(s, ex, len)) {
89                                 tm->tm_hour %= 12;
90                                 break;
91                         }
92                         ex = nl_langinfo(PM_STR);
93                         len = strlen(ex);
94                         if (!strncasecmp(s, ex, len)) {
95                                 tm->tm_hour %= 12;
96                                 tm->tm_hour += 12;
97                                 break;
98                         }
99                         return 0;
100                 case 'r':
101                         s = strptime(s, nl_langinfo(T_FMT_AMPM), tm);
102                         if (!s) return 0;
103                         break;
104                 case 'R':
105                         s = strptime(s, "%H:%M", tm);
106                         if (!s) return 0;
107                         break;
108                 case 'S':
109                         dest = &tm->tm_sec;
110                         min = 0;
111                         range = 61;
112                         goto numeric_range;
113                 case 'T':
114                         s = strptime(s, "%H:%M:%S", tm);
115                         if (!s) return 0;
116                         break;
117                 case 'U':
118                 case 'W':
119                         /* Throw away result, for now. (FIXME?) */
120                         dest = &dummy;
121                         min = 0;
122                         range = 54;
123                         goto numeric_range;
124                 case 'w':
125                         dest = &tm->tm_wday;
126                         min = 0;
127                         range = 7;
128                         goto numeric_range;
129                 case 'x':
130                         s = strptime(s, nl_langinfo(D_FMT), tm);
131                         if (!s) return 0;
132                         break;
133                 case 'X':
134                         s = strptime(s, nl_langinfo(T_FMT), tm);
135                         if (!s) return 0;
136                         break;
137                 case 'y':
138                         /* FIXME */
139                         dest = &dummy;
140                         w = 2;
141                         goto numeric_digits;
142                 case 'Y':
143                         dest = &tm->tm_year;
144                         if (w<0) w=4;
145                         adj = 1900;
146                         goto numeric_digits;
147                 case '%':
148                         if (*s++ != '%') return 0;
149                         break;
150                 default:
151                         return 0;
152                 numeric_range:
153                         if (!isdigit(*s)) return 0;
154                         *dest = 0;
155                         for (i=1; i<=min+range && isdigit(*s); i*=10)
156                                 *dest = *dest * 10 + *s++ - '0';
157                         if (*dest - min >= (unsigned)range) return 0;
158                         *dest -= adj;
159                         switch((char *)dest - (char *)tm) {
160                         case offsetof(struct tm, tm_yday):
161                                 ;
162                         }
163                         goto update;
164                 numeric_digits:
165                         neg = 0;
166                         if (*s == '+') s++;
167                         else if (*s == '-') neg=1, s++;
168                         if (!isdigit(*s)) return 0;
169                         for (*dest=i=0; i<w && isdigit(*s); i++)
170                                 *dest = *dest * 10 + *s++ - '0';
171                         if (neg) *dest = -*dest;
172                         *dest -= adj;
173                         goto update;
174                 symbolic_range:
175                         for (i=2*range-1; i>=0; i--) {
176                                 ex = nl_langinfo(min+i);
177                                 len = strlen(ex);
178                                 if (strncasecmp(s, ex, len)) continue;
179                                 s += len;
180                                 *dest = i % range;
181                                 break;
182                         }
183                         if (i<0) return 0;
184                         goto update;
185                 update:
186                         //FIXME
187                         ;
188                 }
189         }
190         return (char *)s;
191 }