|
13627
|
1 /**
|
|
|
2 * @file msn-utils.c Utility functions
|
|
|
3 *
|
|
|
4 * gaim
|
|
|
5 *
|
|
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
|
8 * source distribution.
|
|
|
9 *
|
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
|
13 * (at your option) any later version.
|
|
|
14 *
|
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 * GNU General Public License for more details.
|
|
|
19 *
|
|
|
20 * You should have received a copy of the GNU General Public License
|
|
|
21 * along with this program; if not, write to the Free Software
|
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
23 */
|
|
|
24 #include "msn.h"
|
|
|
25 #include "msn-utils.h"
|
|
19794
|
26 #include "time.h"
|
|
19785
|
27 //#include <openssl/md5.h>
|
|
13627
|
28
|
|
19824
|
29 /**************************************************************************
|
|
|
30 * Util
|
|
|
31 **************************************************************************/
|
|
|
32 char *
|
|
|
33 rand_guid()
|
|
|
34 {
|
|
|
35 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
|
|
|
36 rand() % 0xAAFF + 0x1111,
|
|
|
37 rand() % 0xAAFF + 0x1111,
|
|
|
38 rand() % 0xAAFF + 0x1111,
|
|
|
39 rand() % 0xAAFF + 0x1111,
|
|
|
40 rand() % 0xAAFF + 0x1111,
|
|
|
41 rand() % 0xAAFF + 0x1111,
|
|
|
42 rand() % 0xAAFF + 0x1111,
|
|
|
43 rand() % 0xAAFF + 0x1111);
|
|
|
44 }
|
|
|
45
|
|
13627
|
46 void
|
|
|
47 msn_parse_format(const char *mime, char **pre_ret, char **post_ret)
|
|
|
48 {
|
|
|
49 char *cur;
|
|
|
50 GString *pre = g_string_new(NULL);
|
|
|
51 GString *post = g_string_new(NULL);
|
|
|
52 unsigned int colors[3];
|
|
|
53
|
|
|
54 if (pre_ret != NULL) *pre_ret = NULL;
|
|
|
55 if (post_ret != NULL) *post_ret = NULL;
|
|
|
56
|
|
|
57 cur = strstr(mime, "FN=");
|
|
|
58
|
|
|
59 if (cur && (*(cur = cur + 3) != ';'))
|
|
|
60 {
|
|
|
61 pre = g_string_append(pre, "<FONT FACE=\"");
|
|
|
62
|
|
|
63 while (*cur && *cur != ';')
|
|
|
64 {
|
|
|
65 pre = g_string_append_c(pre, *cur);
|
|
|
66 cur++;
|
|
|
67 }
|
|
|
68
|
|
|
69 pre = g_string_append(pre, "\">");
|
|
|
70 post = g_string_prepend(post, "</FONT>");
|
|
|
71 }
|
|
|
72
|
|
|
73 cur = strstr(mime, "EF=");
|
|
|
74
|
|
|
75 if (cur && (*(cur = cur + 3) != ';'))
|
|
|
76 {
|
|
|
77 while (*cur && *cur != ';')
|
|
|
78 {
|
|
|
79 pre = g_string_append_c(pre, '<');
|
|
|
80 pre = g_string_append_c(pre, *cur);
|
|
|
81 pre = g_string_append_c(pre, '>');
|
|
|
82 post = g_string_prepend_c(post, '>');
|
|
|
83 post = g_string_prepend_c(post, *cur);
|
|
|
84 post = g_string_prepend_c(post, '/');
|
|
|
85 post = g_string_prepend_c(post, '<');
|
|
|
86 cur++;
|
|
|
87 }
|
|
|
88 }
|
|
|
89
|
|
|
90 cur = strstr(mime, "CO=");
|
|
|
91
|
|
|
92 if (cur && (*(cur = cur + 3) != ';'))
|
|
|
93 {
|
|
|
94 int i;
|
|
|
95
|
|
|
96 i = sscanf(cur, "%02x%02x%02x;", &colors[0], &colors[1], &colors[2]);
|
|
|
97
|
|
|
98 if (i > 0)
|
|
|
99 {
|
|
|
100 char tag[64];
|
|
|
101
|
|
|
102 if (i == 1)
|
|
|
103 {
|
|
|
104 colors[1] = 0;
|
|
|
105 colors[2] = 0;
|
|
|
106 }
|
|
|
107 else if (i == 2)
|
|
|
108 {
|
|
|
109 unsigned int temp = colors[0];
|
|
|
110
|
|
|
111 colors[0] = colors[1];
|
|
|
112 colors[1] = temp;
|
|
|
113 colors[2] = 0;
|
|
|
114 }
|
|
|
115 else if (i == 3)
|
|
|
116 {
|
|
|
117 unsigned int temp = colors[2];
|
|
|
118
|
|
|
119 colors[2] = colors[0];
|
|
|
120 colors[0] = temp;
|
|
|
121 }
|
|
|
122
|
|
|
123 g_snprintf(tag, sizeof(tag),
|
|
|
124 "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">",
|
|
|
125 colors[0], colors[1], colors[2]);
|
|
|
126
|
|
|
127 pre = g_string_append(pre, tag);
|
|
|
128 post = g_string_prepend(post, "</FONT>");
|
|
|
129 }
|
|
|
130 }
|
|
|
131
|
|
|
132 cur = g_strdup(gaim_url_decode(pre->str));
|
|
|
133 g_string_free(pre, TRUE);
|
|
|
134
|
|
|
135 if (pre_ret != NULL)
|
|
|
136 *pre_ret = cur;
|
|
|
137 else
|
|
|
138 g_free(cur);
|
|
|
139
|
|
|
140 cur = g_strdup(gaim_url_decode(post->str));
|
|
|
141 g_string_free(post, TRUE);
|
|
|
142
|
|
|
143 if (post_ret != NULL)
|
|
|
144 *post_ret = cur;
|
|
|
145 else
|
|
|
146 g_free(cur);
|
|
|
147 }
|
|
|
148
|
|
|
149 /*
|
|
|
150 * We need this because we're only supposed to encode spaces in the font
|
|
|
151 * names. gaim_url_encode() isn't acceptable.
|
|
|
152 */
|
|
|
153 static const char *
|
|
|
154 encode_spaces(const char *str)
|
|
|
155 {
|
|
|
156 static char buf[BUF_LEN];
|
|
|
157 const char *c;
|
|
|
158 char *d;
|
|
|
159
|
|
|
160 g_return_val_if_fail(str != NULL, NULL);
|
|
|
161
|
|
|
162 for (c = str, d = buf; *c != '\0'; c++)
|
|
|
163 {
|
|
|
164 if (*c == ' ')
|
|
|
165 {
|
|
|
166 *d++ = '%';
|
|
|
167 *d++ = '2';
|
|
|
168 *d++ = '0';
|
|
|
169 }
|
|
|
170 else
|
|
|
171 *d++ = *c;
|
|
|
172 }
|
|
|
173
|
|
|
174 return buf;
|
|
|
175 }
|
|
|
176
|
|
|
177 /*
|
|
|
178 * Taken from the zephyr plugin.
|
|
|
179 * This parses HTML formatting (put out by one of the gtkimhtml widgets
|
|
|
180 * and converts it to msn formatting. It doesn't deal with the tag closing,
|
|
|
181 * but gtkimhtml widgets give valid html.
|
|
|
182 * It currently deals properly with <b>, <u>, <i>, <font face=...>,
|
|
|
183 * <font color=...>.
|
|
|
184 * It ignores <font back=...> and <font size=...>
|
|
|
185 */
|
|
|
186 void
|
|
|
187 msn_import_html(const char *html, char **attributes, char **message)
|
|
|
188 {
|
|
|
189 int len, retcount = 0;
|
|
|
190 const char *c;
|
|
|
191 char *msg;
|
|
|
192 char *fontface = NULL;
|
|
|
193 char fonteffect[4];
|
|
|
194 char fontcolor[7];
|
|
|
195
|
|
|
196 g_return_if_fail(html != NULL);
|
|
|
197 g_return_if_fail(attributes != NULL);
|
|
|
198 g_return_if_fail(message != NULL);
|
|
|
199
|
|
|
200 len = strlen(html);
|
|
|
201 msg = g_malloc0(len + 1);
|
|
|
202
|
|
|
203 memset(fontcolor, 0, sizeof(fontcolor));
|
|
|
204 strcat(fontcolor, "0");
|
|
|
205 memset(fonteffect, 0, sizeof(fonteffect));
|
|
|
206
|
|
|
207 for (c = html; *c != '\0';)
|
|
|
208 {
|
|
|
209 if (*c == '<')
|
|
|
210 {
|
|
|
211 if (!g_ascii_strncasecmp(c + 1, "br>", 3))
|
|
|
212 {
|
|
|
213 msg[retcount++] = '\r';
|
|
|
214 msg[retcount++] = '\n';
|
|
|
215 c += 4;
|
|
|
216 }
|
|
|
217 else if (!g_ascii_strncasecmp(c + 1, "i>", 2))
|
|
|
218 {
|
|
|
219 strcat(fonteffect, "I");
|
|
|
220 c += 3;
|
|
|
221 }
|
|
|
222 else if (!g_ascii_strncasecmp(c + 1, "b>", 2))
|
|
|
223 {
|
|
|
224 strcat(fonteffect, "B");
|
|
|
225 c += 3;
|
|
|
226 }
|
|
|
227 else if (!g_ascii_strncasecmp(c + 1, "u>", 2))
|
|
|
228 {
|
|
|
229 strcat(fonteffect, "U");
|
|
|
230 c += 3;
|
|
|
231 }
|
|
|
232 else if (!g_ascii_strncasecmp(c + 1, "s>", 2))
|
|
|
233 {
|
|
|
234 strcat(fonteffect, "S");
|
|
|
235 c += 3;
|
|
|
236 }
|
|
|
237 else if (!g_ascii_strncasecmp(c + 1, "a href=\"", 8))
|
|
|
238 {
|
|
|
239 c += 9;
|
|
|
240
|
|
|
241 if (!g_ascii_strncasecmp(c, "mailto:", 7))
|
|
|
242 c += 7;
|
|
|
243
|
|
|
244 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
|
|
|
245 msg[retcount++] = *c++;
|
|
|
246
|
|
|
247 if (*c != '\0')
|
|
|
248 c += 2;
|
|
|
249
|
|
|
250 /* ignore descriptive string */
|
|
|
251 while ((*c != '\0') && g_ascii_strncasecmp(c, "</a>", 4))
|
|
|
252 c++;
|
|
|
253
|
|
|
254 if (*c != '\0')
|
|
|
255 c += 4;
|
|
|
256 }
|
|
|
257 else if (!g_ascii_strncasecmp(c + 1, "font", 4))
|
|
|
258 {
|
|
|
259 c += 5;
|
|
|
260
|
|
|
261 while ((*c != '\0') && !g_ascii_strncasecmp(c, " ", 1))
|
|
|
262 c++;
|
|
|
263
|
|
|
264 if (!g_ascii_strncasecmp(c, "color=\"#", 7))
|
|
|
265 {
|
|
|
266 c += 8;
|
|
|
267
|
|
|
268 fontcolor[0] = *(c + 4);
|
|
|
269 fontcolor[1] = *(c + 5);
|
|
|
270 fontcolor[2] = *(c + 2);
|
|
|
271 fontcolor[3] = *(c + 3);
|
|
|
272 fontcolor[4] = *c;
|
|
|
273 fontcolor[5] = *(c + 1);
|
|
|
274
|
|
|
275 c += 8;
|
|
|
276 }
|
|
|
277 else if (!g_ascii_strncasecmp(c, "face=\"", 6))
|
|
|
278 {
|
|
|
279 const char *end = NULL;
|
|
|
280 const char *comma = NULL;
|
|
|
281 unsigned int namelen = 0;
|
|
|
282
|
|
|
283 c += 6;
|
|
|
284 end = strchr(c, '\"');
|
|
|
285 comma = strchr(c, ',');
|
|
|
286
|
|
|
287 if (comma == NULL || comma > end)
|
|
|
288 namelen = (unsigned int)(end - c);
|
|
|
289 else
|
|
|
290 namelen = (unsigned int)(comma - c);
|
|
|
291
|
|
|
292 fontface = g_strndup(c, namelen);
|
|
|
293 c = end + 2;
|
|
|
294 }
|
|
|
295 else
|
|
|
296 {
|
|
|
297 /* Drop all unrecognized/misparsed font tags */
|
|
|
298 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
|
|
|
299 c++;
|
|
|
300
|
|
|
301 if (*c != '\0')
|
|
|
302 c += 2;
|
|
|
303 }
|
|
|
304 }
|
|
|
305 else
|
|
|
306 {
|
|
|
307 while ((*c != '\0') && (*c != '>'))
|
|
|
308 c++;
|
|
|
309 if (*c != '\0')
|
|
|
310 c++;
|
|
|
311 }
|
|
|
312 }
|
|
|
313 else if (*c == '&')
|
|
|
314 {
|
|
|
315 if (!g_ascii_strncasecmp(c, "<", 4))
|
|
|
316 {
|
|
|
317 msg[retcount++] = '<';
|
|
|
318 c += 4;
|
|
|
319 }
|
|
|
320 else if (!g_ascii_strncasecmp(c, ">", 4))
|
|
|
321 {
|
|
|
322 msg[retcount++] = '>';
|
|
|
323 c += 4;
|
|
|
324 }
|
|
|
325 else if (!g_ascii_strncasecmp(c, " ", 6))
|
|
|
326 {
|
|
|
327 msg[retcount++] = ' ';
|
|
|
328 c += 6;
|
|
|
329 }
|
|
|
330 else if (!g_ascii_strncasecmp(c, """, 6))
|
|
|
331 {
|
|
|
332 msg[retcount++] = '"';
|
|
|
333 c += 6;
|
|
|
334 }
|
|
|
335 else if (!g_ascii_strncasecmp(c, "&", 5))
|
|
|
336 {
|
|
|
337 msg[retcount++] = '&';
|
|
|
338 c += 5;
|
|
|
339 }
|
|
|
340 else if (!g_ascii_strncasecmp(c, "'", 6))
|
|
|
341 {
|
|
|
342 msg[retcount++] = '\'';
|
|
|
343 c += 6;
|
|
|
344 }
|
|
|
345 else
|
|
|
346 msg[retcount++] = *c++;
|
|
|
347 }
|
|
|
348 else
|
|
|
349 msg[retcount++] = *c++;
|
|
|
350 }
|
|
|
351
|
|
|
352 if (fontface == NULL)
|
|
|
353 fontface = g_strdup("MS Sans Serif");
|
|
|
354
|
|
|
355 *attributes = g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0",
|
|
|
356 encode_spaces(fontface),
|
|
|
357 fonteffect, fontcolor);
|
|
|
358 *message = g_strdup(msg);
|
|
|
359
|
|
|
360 g_free(fontface);
|
|
|
361 g_free(msg);
|
|
|
362 }
|
|
|
363
|
|
|
364 void
|
|
|
365 msn_parse_socket(const char *str, char **ret_host, int *ret_port)
|
|
|
366 {
|
|
|
367 char *host;
|
|
|
368 char *c;
|
|
|
369 int port;
|
|
|
370
|
|
|
371 host = g_strdup(str);
|
|
|
372
|
|
19784
|
373 if ((c = strchr(host, ':')) != NULL){
|
|
13627
|
374 *c = '\0';
|
|
|
375 port = atoi(c + 1);
|
|
19784
|
376 }else{
|
|
|
377 port = 1863;
|
|
13627
|
378 }
|
|
|
379
|
|
|
380 *ret_host = host;
|
|
|
381 *ret_port = port;
|
|
|
382 }
|
|
19794
|
383 /***************************************************************************
|
|
|
384 * MSN Time Related Funciton
|
|
|
385 ***************************************************************************/
|
|
|
386 #if 0
|
|
|
387 int
|
|
|
388 msn_convert_iso8601(const char *timestr,struct tm tm_time)
|
|
|
389 {
|
|
|
390 char temp[64];
|
|
|
391 struct tm ctime;
|
|
|
392 time_t ts;
|
|
19784
|
393
|
|
19794
|
394 gaim_debug_info("MaYuan","convert string is{%s}\n",timestr);
|
|
|
395 tzset();
|
|
|
396 /*copy string first*/
|
|
|
397 memset(temp, 0, sizeof(temp));
|
|
|
398 strncpy(temp, timestr, strlen(timestr));
|
|
|
399
|
|
|
400 /*convert via strptime()*/
|
|
|
401 memset(&ctime, 0, sizeof(struct tm));
|
|
|
402 strptime(temp, "%d %b %Y %T %Z", &ctime);
|
|
|
403 ts = mktime(&ctime) - timezone;
|
|
|
404 localtime_r(&ts, tm_time);
|
|
|
405 }
|
|
|
406 #endif
|
|
|
407
|
|
|
408 /***************************************************************************
|
|
|
409 * MSN Challenge Computing Function
|
|
|
410 ***************************************************************************/
|
|
19785
|
411 /*check the edian of system*/
|
|
|
412 int
|
|
|
413 isBigEndian(void)
|
|
|
414 {
|
|
|
415 short int word = 0x0100;
|
|
|
416 char *byte = (char *)&word;
|
|
|
417
|
|
|
418 return(byte[0]);
|
|
|
419 }
|
|
|
420
|
|
|
421 /*swap utility*/
|
|
|
422 unsigned int
|
|
|
423 swapInt(unsigned int dw)
|
|
|
424 {
|
|
|
425 unsigned int tmp;
|
|
|
426 tmp = (dw & 0x000000FF);
|
|
|
427 tmp = ((dw & 0x0000FF00) >> 0x08) | (tmp << 0x08);
|
|
|
428 tmp = ((dw & 0x00FF0000) >> 0x10) | (tmp << 0x08);
|
|
|
429 tmp = ((dw & 0xFF000000) >> 0x18) | (tmp << 0x08);
|
|
|
430 return(tmp);
|
|
|
431 }
|
|
|
432
|
|
|
433 /*
|
|
19788
|
434 * Handle MSN Chanllege computation
|
|
19785
|
435 *This algorithm reference with http://msnpiki.msnfanatic.com/index.php/MSNP11:Challenges
|
|
|
436 */
|
|
|
437 #define BUFSIZE 256
|
|
|
438 void
|
|
|
439 msn_handle_chl(char *input, char *output)
|
|
|
440 {
|
|
|
441 GaimCipher *cipher;
|
|
|
442 GaimCipherContext *context;
|
|
|
443 char *productKey = MSNP13_WLM_PRODUCT_KEY,
|
|
|
444 *productID = MSNP13_WLM_PRODUCT_ID,
|
|
|
445 *hexChars = "0123456789abcdef",
|
|
|
446 buf[BUFSIZE];
|
|
|
447 unsigned char md5Hash[16], *newHash;
|
|
|
448 unsigned int *md5Parts, *chlStringParts, newHashParts[5];
|
|
|
449
|
|
|
450 long long nHigh=0, nLow=0;
|
|
|
451
|
|
|
452 int i, bigEndian;
|
|
|
453
|
|
|
454 /* Determine our endianess */
|
|
|
455 bigEndian = isBigEndian();
|
|
|
456
|
|
|
457 /* Create the MD5 hash by using Gaim MD5 algorithm*/
|
|
|
458 cipher = gaim_ciphers_find_cipher("md5");
|
|
|
459 context = gaim_cipher_context_new(cipher, NULL);
|
|
|
460
|
|
|
461 gaim_cipher_context_append(context, (const guchar *)input,
|
|
|
462 strlen(input));
|
|
|
463 gaim_cipher_context_append(context, (const guchar *)productKey,
|
|
|
464 strlen(productKey));
|
|
|
465 gaim_cipher_context_digest(context, sizeof(md5Hash), md5Hash, NULL);
|
|
|
466 gaim_cipher_context_destroy(context);
|
|
|
467
|
|
|
468 /* Split it into four integers */
|
|
|
469 md5Parts = (unsigned int *)md5Hash;
|
|
|
470 for(i=0; i<4; i++){
|
|
|
471 /* check for endianess */
|
|
|
472 if(bigEndian)
|
|
|
473 md5Parts[i] = swapInt(md5Parts[i]);
|
|
|
474
|
|
|
475 /* & each integer with 0x7FFFFFFF */
|
|
|
476 /* and save one unmodified array for later */
|
|
|
477 newHashParts[i] = md5Parts[i];
|
|
|
478 md5Parts[i] &= 0x7FFFFFFF;
|
|
|
479 }
|
|
|
480
|
|
|
481 /* make a new string and pad with '0' */
|
|
|
482 snprintf(buf, BUFSIZE-5, "%s%s", input, productID);
|
|
|
483 i = strlen(buf);
|
|
|
484 memset(&buf[i], '0', 8 - (i % 8));
|
|
|
485 buf[i + (8 - (i % 8))]='\0';
|
|
|
486
|
|
|
487 /* split into integers */
|
|
|
488 chlStringParts = (unsigned int *)buf;
|
|
|
489
|
|
|
490 /* this is magic */
|
|
|
491 for (i=0; i<(strlen(buf)/4)-1; i+=2){
|
|
|
492 long long temp;
|
|
|
493
|
|
|
494 if(bigEndian){
|
|
|
495 chlStringParts[i] = swapInt(chlStringParts[i]);
|
|
|
496 chlStringParts[i+1] = swapInt(chlStringParts[i+1]);
|
|
|
497 }
|
|
|
498
|
|
|
499 temp=(md5Parts[0] * (((0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF)+nHigh) + md5Parts[1])%0x7FFFFFFF;
|
|
|
500 nHigh=(md5Parts[2] * (((long long)chlStringParts[i+1]+temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
|
|
|
501 nLow=nLow + nHigh + temp;
|
|
|
502 }
|
|
|
503 nHigh=(nHigh+md5Parts[1]) % 0x7FFFFFFF;
|
|
|
504 nLow=(nLow+md5Parts[3]) % 0x7FFFFFFF;
|
|
|
505
|
|
|
506 newHashParts[0]^=nHigh;
|
|
|
507 newHashParts[1]^=nLow;
|
|
|
508 newHashParts[2]^=nHigh;
|
|
|
509 newHashParts[3]^=nLow;
|
|
|
510
|
|
|
511 /* swap more bytes if big endian */
|
|
|
512 for(i=0; i<4 && bigEndian; i++)
|
|
|
513 newHashParts[i] = swapInt(newHashParts[i]);
|
|
|
514
|
|
|
515 /* make a string of the parts */
|
|
|
516 newHash = (unsigned char *)newHashParts;
|
|
|
517
|
|
|
518 /* convert to hexadecimal */
|
|
|
519 for (i=0; i<16; i++)
|
|
|
520 {
|
|
|
521 output[i*2]=hexChars[(newHash[i]>>4)&0xF];
|
|
|
522 output[(i*2)+1]=hexChars[newHash[i]&0xF];
|
|
|
523 }
|
|
|
524
|
|
|
525 output[32]='\0';
|
|
|
526
|
|
|
527 // gaim_debug_info("MaYuan","chl output{%s}\n",output);
|
|
|
528 }
|
|
|
529
|
|
19794
|
530 #if (!defined(_XOPEN_SOURCE))||defined(_WIN32)
|
|
|
531
|
|
|
532 #ifndef __P
|
|
|
533 # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
|
|
|
534 # define __P(args) args
|
|
|
535 # else
|
|
|
536 # define __P(args) ()
|
|
|
537 # endif /* GCC. */
|
|
|
538 #endif /* Not __P. */
|
|
|
539
|
|
|
540 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
|
|
|
541 # ifdef _LIBC
|
|
|
542 # define localtime_r __localtime_r
|
|
|
543 # else
|
|
|
544 /* Approximate localtime_r as best we can in its absence. */
|
|
|
545 # define localtime_r my_localtime_r
|
|
|
546 static struct tm *localtime_r __P ((const time_t *, struct tm *));
|
|
|
547 static struct tm *
|
|
|
548 localtime_r (t, tp)
|
|
|
549 const time_t *t;
|
|
|
550 struct tm *tp;
|
|
|
551 {
|
|
|
552 struct tm *l = localtime (t);
|
|
|
553 if (! l)
|
|
|
554 return 0;
|
|
|
555 *tp = *l;
|
|
|
556 return tp;
|
|
|
557 }
|
|
|
558 # endif /* ! _LIBC */
|
|
|
559 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
|
|
|
560
|
|
|
561
|
|
|
562 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
|
|
|
563
|
|
|
564 #if defined __GNUC__ && __GNUC__ >= 2
|
|
|
565 # define match_string(cs1, s2) \
|
|
|
566 ({ size_t len = strlen (cs1); \
|
|
|
567 int result = strncasecmp ((cs1), (s2), len) == 0; \
|
|
|
568 if (result) (s2) += len; \
|
|
|
569 result; })
|
|
|
570 #else
|
|
|
571 /* Oh come on. Get a reasonable compiler. */
|
|
|
572 # define match_string(cs1, s2) \
|
|
|
573 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
|
|
|
574 #endif
|
|
|
575
|
|
|
576 /* We intentionally do not use isdigit() for testing because this will
|
|
|
577 lead to problems with the wide character version. */
|
|
|
578 #define get_number(from, to, n) \
|
|
|
579 do { \
|
|
|
580 int __n = n; \
|
|
|
581 val = 0; \
|
|
|
582 while (*rp == ' ') \
|
|
|
583 ++rp; \
|
|
|
584 if ((*rp < '0') || (*rp > '9')) \
|
|
|
585 return NULL; \
|
|
|
586 do { \
|
|
|
587 val *= 10; \
|
|
|
588 val += *rp++ - '0'; \
|
|
|
589 } while ((--__n > 0) && (val * 10 <= to) && (*rp >= '0') && (*rp <= '9')); \
|
|
|
590 if ((val < from) || (val > to)) \
|
|
|
591 return NULL; \
|
|
|
592 } while (0)
|
|
|
593
|
|
|
594 #ifdef _NL_CURRENT
|
|
|
595 # define get_alt_number(from, to, n) \
|
|
|
596 ({ \
|
|
|
597 __label__ do_normal; \
|
|
|
598 if (*decided != raw) \
|
|
|
599 { \
|
|
|
600 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
|
|
|
601 int __n = n; \
|
|
|
602 int any = 0; \
|
|
|
603 while (*rp == ' ') \
|
|
|
604 ++rp; \
|
|
|
605 val = 0; \
|
|
|
606 do { \
|
|
|
607 val *= 10; \
|
|
|
608 while (*alts != '\0') \
|
|
|
609 { \
|
|
|
610 size_t len = strlen (alts); \
|
|
|
611 if (strncasecmp (alts, rp, len) == 0) \
|
|
|
612 break; \
|
|
|
613 alts += len + 1; \
|
|
|
614 ++val; \
|
|
|
615 } \
|
|
|
616 if (*alts == '\0') \
|
|
|
617 { \
|
|
|
618 if (*decided == not && ! any) \
|
|
|
619 goto do_normal; \
|
|
|
620 /* If we haven't read anything it's an error. */ \
|
|
|
621 if (! any) \
|
|
|
622 return NULL; \
|
|
|
623 /* Correct the premature multiplication. */ \
|
|
|
624 val /= 10; \
|
|
|
625 break; \
|
|
|
626 } \
|
|
|
627 else \
|
|
|
628 *decided = loc; \
|
|
|
629 } while (--__n > 0 && val * 10 <= to); \
|
|
|
630 if (val < from || val > to) \
|
|
|
631 return NULL; \
|
|
|
632 } \
|
|
|
633 else \
|
|
|
634 { \
|
|
|
635 do_normal: \
|
|
|
636 get_number (from, to, n); \
|
|
|
637 } \
|
|
|
638 0; \
|
|
|
639 })
|
|
|
640 #else
|
|
|
641 # define get_alt_number(from, to, n) \
|
|
|
642 /* We don't have the alternate representation. */ \
|
|
|
643 get_number(from, to, n)
|
|
|
644 #endif
|
|
|
645
|
|
|
646 #define recursive(new_fmt) \
|
|
|
647 (*(new_fmt) != '\0' \
|
|
|
648 && (rp = strptime_internal (rp, (new_fmt), tm, decided, era_cnt)) != NULL)
|
|
|
649
|
|
|
650
|
|
|
651 #ifdef _LIBC
|
|
|
652 /* This is defined in locale/C-time.c in the GNU libc. */
|
|
|
653 extern const struct locale_data _nl_C_LC_TIME;
|
|
|
654 extern const unsigned short int __mon_yday[2][13];
|
|
|
655
|
|
|
656 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
|
|
|
657 # define ab_weekday_name \
|
|
|
658 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
|
|
|
659 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
|
|
|
660 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
|
|
|
661 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
|
|
|
662 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
|
|
|
663 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
|
|
|
664 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
|
|
|
665 # define HERE_T_FMT_AMPM \
|
|
|
666 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
|
|
|
667 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
|
|
|
668
|
|
|
669 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
|
|
|
670 #else
|
|
|
671 static char const weekday_name[][10] =
|
|
|
672 {
|
|
|
673 "Sunday", "Monday", "Tuesday", "Wednesday",
|
|
|
674 "Thursday", "Friday", "Saturday"
|
|
|
675 };
|
|
|
676 static char const ab_weekday_name[][4] =
|
|
|
677 {
|
|
|
678 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
679 };
|
|
|
680 static char const month_name[][10] =
|
|
|
681 {
|
|
|
682 "January", "February", "March", "April", "May", "June",
|
|
|
683 "July", "August", "September", "October", "November", "December"
|
|
|
684 };
|
|
|
685 static char const ab_month_name[][4] =
|
|
|
686 {
|
|
|
687 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
688 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
689 };
|
|
|
690 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
|
|
|
691 # define HERE_D_FMT "%m/%d/%y"
|
|
|
692 # define HERE_AM_STR "AM"
|
|
|
693 # define HERE_PM_STR "PM"
|
|
|
694 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
|
|
|
695 # define HERE_T_FMT "%H:%M:%S"
|
|
|
696
|
|
|
697 const unsigned short int __mon_yday[2][13] =
|
|
|
698 {
|
|
|
699 /* Normal years. */
|
|
|
700 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
|
|
|
701 /* Leap years. */
|
|
|
702 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
|
|
|
703 };
|
|
|
704 #endif
|
|
|
705
|
|
|
706 /* Status of lookup: do we use the locale data or the raw data? */
|
|
|
707 enum locale_status { not, loc, raw };
|
|
|
708
|
|
|
709
|
|
|
710 #ifndef __isleap
|
|
|
711 /* Nonzero if YEAR is a leap year (every 4 years,
|
|
|
712 except every 100th isn't, and every 400th is). */
|
|
|
713 # define __isleap(year) \
|
|
|
714 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
|
|
|
715 #endif
|
|
|
716
|
|
|
717 /* Compute the day of the week. */
|
|
|
718 static void
|
|
|
719 day_of_the_week (struct tm *tm)
|
|
|
720 {
|
|
|
721 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
|
|
|
722 the difference between this data in the one on TM and so determine
|
|
|
723 the weekday. */
|
|
|
724 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
|
|
|
725 int wday = (-473
|
|
|
726 + (365 * (tm->tm_year - 70))
|
|
|
727 + (corr_year / 4)
|
|
|
728 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
|
|
|
729 + (((corr_year / 4) / 25) / 4)
|
|
|
730 + __mon_yday[0][tm->tm_mon]
|
|
|
731 + tm->tm_mday - 1);
|
|
|
732 tm->tm_wday = ((wday % 7) + 7) % 7;
|
|
|
733 }
|
|
|
734
|
|
|
735 /* Compute the day of the year. */
|
|
|
736 static void
|
|
|
737 day_of_the_year (struct tm *tm)
|
|
|
738 {
|
|
|
739 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
|
|
|
740 + (tm->tm_mday - 1));
|
|
|
741 }
|
|
|
742
|
|
|
743 static char *
|
|
|
744 #ifdef _LIBC
|
|
|
745 internal_function
|
|
|
746 #endif
|
|
|
747 strptime_internal __P ((const char *rp, const char *fmt, struct tm *tm,
|
|
|
748 enum locale_status *decided, int era_cnt));
|
|
|
749
|
|
|
750 static char *
|
|
|
751 #ifdef _LIBC
|
|
|
752 internal_function
|
|
|
753 #endif
|
|
|
754 strptime_internal (rp, fmt, tm, decided, era_cnt)
|
|
|
755 const char *rp;
|
|
|
756 const char *fmt;
|
|
|
757 struct tm *tm;
|
|
|
758 enum locale_status *decided;
|
|
|
759 int era_cnt;
|
|
|
760 {
|
|
|
761 const char *rp_backup;
|
|
|
762 int cnt;
|
|
|
763 size_t val;
|
|
|
764 int have_I, is_pm;
|
|
|
765 int century, want_century;
|
|
|
766 int want_era;
|
|
|
767 int have_wday, want_xday;
|
|
|
768 int have_yday;
|
|
|
769 int have_mon, have_mday;
|
|
|
770 #ifdef _NL_CURRENT
|
|
|
771 size_t num_eras;
|
|
|
772 #endif
|
|
|
773 struct era_entry *era;
|
|
|
774
|
|
|
775 have_I = is_pm = 0;
|
|
|
776 century = -1;
|
|
|
777 want_century = 0;
|
|
|
778 want_era = 0;
|
|
|
779 era = NULL;
|
|
|
780
|
|
|
781 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
|
|
|
782
|
|
|
783 while (*fmt != '\0')
|
|
|
784 {
|
|
|
785 /* A white space in the format string matches 0 more or white
|
|
|
786 space in the input string. */
|
|
|
787 if (isspace (*fmt))
|
|
|
788 {
|
|
|
789 while (isspace (*rp))
|
|
|
790 ++rp;
|
|
|
791 ++fmt;
|
|
|
792 continue;
|
|
|
793 }
|
|
|
794
|
|
|
795 /* Any character but `%' must be matched by the same character
|
|
|
796 in the iput string. */
|
|
|
797 if (*fmt != '%')
|
|
|
798 {
|
|
|
799 match_char (*fmt++, *rp++);
|
|
|
800 continue;
|
|
|
801 }
|
|
|
802
|
|
|
803 ++fmt;
|
|
|
804 #ifndef _NL_CURRENT
|
|
|
805 /* We need this for handling the `E' modifier. */
|
|
|
806 start_over:
|
|
|
807 #endif
|
|
|
808
|
|
|
809 /* Make back up of current processing pointer. */
|
|
|
810 rp_backup = rp;
|
|
|
811
|
|
|
812 switch (*fmt++)
|
|
|
813 {
|
|
|
814 case '%':
|
|
|
815 /* Match the `%' character itself. */
|
|
|
816 match_char ('%', *rp++);
|
|
|
817 break;
|
|
|
818 case 'a':
|
|
|
819 case 'A':
|
|
|
820 /* Match day of week. */
|
|
|
821 for (cnt = 0; cnt < 7; ++cnt)
|
|
|
822 {
|
|
|
823 #ifdef _NL_CURRENT
|
|
|
824 if (*decided !=raw)
|
|
|
825 {
|
|
|
826 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
|
|
|
827 {
|
|
|
828 if (*decided == not
|
|
|
829 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
|
|
|
830 weekday_name[cnt]))
|
|
|
831 *decided = loc;
|
|
|
832 break;
|
|
|
833 }
|
|
|
834 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
|
|
|
835 {
|
|
|
836 if (*decided == not
|
|
|
837 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
|
|
|
838 ab_weekday_name[cnt]))
|
|
|
839 *decided = loc;
|
|
|
840 break;
|
|
|
841 }
|
|
|
842 }
|
|
|
843 #endif
|
|
|
844 if (*decided != loc
|
|
|
845 && (match_string (weekday_name[cnt], rp)
|
|
|
846 || match_string (ab_weekday_name[cnt], rp)))
|
|
|
847 {
|
|
|
848 *decided = raw;
|
|
|
849 break;
|
|
|
850 }
|
|
|
851 }
|
|
|
852 if (cnt == 7)
|
|
|
853 /* Does not match a weekday name. */
|
|
|
854 return NULL;
|
|
|
855 tm->tm_wday = cnt;
|
|
|
856 have_wday = 1;
|
|
|
857 break;
|
|
|
858 case 'b':
|
|
|
859 case 'B':
|
|
|
860 case 'h':
|
|
|
861 /* Match month name. */
|
|
|
862 for (cnt = 0; cnt < 12; ++cnt)
|
|
|
863 {
|
|
|
864 #ifdef _NL_CURRENT
|
|
|
865 if (*decided !=raw)
|
|
|
866 {
|
|
|
867 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
|
|
|
868 {
|
|
|
869 if (*decided == not
|
|
|
870 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
|
|
|
871 month_name[cnt]))
|
|
|
872 *decided = loc;
|
|
|
873 break;
|
|
|
874 }
|
|
|
875 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
|
|
|
876 {
|
|
|
877 if (*decided == not
|
|
|
878 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
|
|
|
879 ab_month_name[cnt]))
|
|
|
880 *decided = loc;
|
|
|
881 break;
|
|
|
882 }
|
|
|
883 }
|
|
|
884 #endif
|
|
|
885 if (match_string (month_name[cnt], rp)
|
|
|
886 || match_string (ab_month_name[cnt], rp))
|
|
|
887 {
|
|
|
888 *decided = raw;
|
|
|
889 break;
|
|
|
890 }
|
|
|
891 }
|
|
|
892 if (cnt == 12)
|
|
|
893 /* Does not match a month name. */
|
|
|
894 return NULL;
|
|
|
895 tm->tm_mon = cnt;
|
|
|
896 want_xday = 1;
|
|
|
897 break;
|
|
|
898 case 'c':
|
|
|
899 /* Match locale's date and time format. */
|
|
|
900 #ifdef _NL_CURRENT
|
|
|
901 if (*decided != raw)
|
|
|
902 {
|
|
|
903 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
|
|
|
904 {
|
|
|
905 if (*decided == loc)
|
|
|
906 return NULL;
|
|
|
907 else
|
|
|
908 rp = rp_backup;
|
|
|
909 }
|
|
|
910 else
|
|
|
911 {
|
|
|
912 if (*decided == not &&
|
|
|
913 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
|
|
|
914 *decided = loc;
|
|
|
915 want_xday = 1;
|
|
|
916 break;
|
|
|
917 }
|
|
|
918 *decided = raw;
|
|
|
919 }
|
|
|
920 #endif
|
|
|
921 if (!recursive (HERE_D_T_FMT))
|
|
|
922 return NULL;
|
|
|
923 want_xday = 1;
|
|
|
924 break;
|
|
|
925 case 'C':
|
|
|
926 /* Match century number. */
|
|
|
927 #ifdef _NL_CURRENT
|
|
|
928 match_century:
|
|
|
929 #endif
|
|
|
930 get_number (0, 99, 2);
|
|
|
931 century = val;
|
|
|
932 want_xday = 1;
|
|
|
933 break;
|
|
|
934 case 'd':
|
|
|
935 case 'e':
|
|
|
936 /* Match day of month. */
|
|
|
937 get_number (1, 31, 2);
|
|
|
938 tm->tm_mday = val;
|
|
|
939 have_mday = 1;
|
|
|
940 want_xday = 1;
|
|
|
941 break;
|
|
|
942 case 'F':
|
|
|
943 if (!recursive ("%Y-%m-%d"))
|
|
|
944 return NULL;
|
|
|
945 want_xday = 1;
|
|
|
946 break;
|
|
|
947 case 'x':
|
|
|
948 #ifdef _NL_CURRENT
|
|
|
949 if (*decided != raw)
|
|
|
950 {
|
|
|
951 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
|
|
|
952 {
|
|
|
953 if (*decided == loc)
|
|
|
954 return NULL;
|
|
|
955 else
|
|
|
956 rp = rp_backup;
|
|
|
957 }
|
|
|
958 else
|
|
|
959 {
|
|
|
960 if (*decided == not
|
|
|
961 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
|
|
|
962 *decided = loc;
|
|
|
963 want_xday = 1;
|
|
|
964 break;
|
|
|
965 }
|
|
|
966 *decided = raw;
|
|
|
967 }
|
|
|
968 #endif
|
|
|
969 /* Fall through. */
|
|
|
970 case 'D':
|
|
|
971 /* Match standard day format. */
|
|
|
972 if (!recursive (HERE_D_FMT))
|
|
|
973 return NULL;
|
|
|
974 want_xday = 1;
|
|
|
975 break;
|
|
|
976 case 'k':
|
|
|
977 case 'H':
|
|
|
978 /* Match hour in 24-hour clock. */
|
|
|
979 get_number (0, 23, 2);
|
|
|
980 tm->tm_hour = val;
|
|
|
981 have_I = 0;
|
|
|
982 break;
|
|
|
983 case 'I':
|
|
|
984 /* Match hour in 12-hour clock. */
|
|
|
985 get_number (1, 12, 2);
|
|
|
986 tm->tm_hour = val % 12;
|
|
|
987 have_I = 1;
|
|
|
988 break;
|
|
|
989 case 'j':
|
|
|
990 /* Match day number of year. */
|
|
|
991 get_number (1, 366, 3);
|
|
|
992 tm->tm_yday = val - 1;
|
|
|
993 have_yday = 1;
|
|
|
994 break;
|
|
|
995 case 'm':
|
|
|
996 /* Match number of month. */
|
|
|
997 get_number (1, 12, 2);
|
|
|
998 tm->tm_mon = val - 1;
|
|
|
999 have_mon = 1;
|
|
|
1000 want_xday = 1;
|
|
|
1001 break;
|
|
|
1002 case 'M':
|
|
|
1003 /* Match minute. */
|
|
|
1004 get_number (0, 59, 2);
|
|
|
1005 tm->tm_min = val;
|
|
|
1006 break;
|
|
|
1007 case 'n':
|
|
|
1008 case 't':
|
|
|
1009 /* Match any white space. */
|
|
|
1010 while (isspace (*rp))
|
|
|
1011 ++rp;
|
|
|
1012 break;
|
|
|
1013 case 'p':
|
|
|
1014 /* Match locale's equivalent of AM/PM. */
|
|
|
1015 #ifdef _NL_CURRENT
|
|
|
1016 if (*decided != raw)
|
|
|
1017 {
|
|
|
1018 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
|
|
|
1019 {
|
|
|
1020 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
|
|
|
1021 *decided = loc;
|
|
|
1022 break;
|
|
|
1023 }
|
|
|
1024 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
|
|
|
1025 {
|
|
|
1026 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
|
|
|
1027 *decided = loc;
|
|
|
1028 is_pm = 1;
|
|
|
1029 break;
|
|
|
1030 }
|
|
|
1031 *decided = raw;
|
|
|
1032 }
|
|
|
1033 #endif
|
|
|
1034 if (!match_string (HERE_AM_STR, rp))
|
|
|
1035 if (match_string (HERE_PM_STR, rp))
|
|
|
1036 is_pm = 1;
|
|
|
1037 else
|
|
|
1038 return NULL;
|
|
|
1039 break;
|
|
|
1040 case 'r':
|
|
|
1041 #ifdef _NL_CURRENT
|
|
|
1042 if (*decided != raw)
|
|
|
1043 {
|
|
|
1044 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
|
|
|
1045 {
|
|
|
1046 if (*decided == loc)
|
|
|
1047 return NULL;
|
|
|
1048 else
|
|
|
1049 rp = rp_backup;
|
|
|
1050 }
|
|
|
1051 else
|
|
|
1052 {
|
|
|
1053 if (*decided == not &&
|
|
|
1054 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
|
|
|
1055 HERE_T_FMT_AMPM))
|
|
|
1056 *decided = loc;
|
|
|
1057 break;
|
|
|
1058 }
|
|
|
1059 *decided = raw;
|
|
|
1060 }
|
|
|
1061 #endif
|
|
|
1062 if (!recursive (HERE_T_FMT_AMPM))
|
|
|
1063 return NULL;
|
|
|
1064 break;
|
|
|
1065 case 'R':
|
|
|
1066 if (!recursive ("%H:%M"))
|
|
|
1067 return NULL;
|
|
|
1068 break;
|
|
|
1069 case 's':
|
|
|
1070 {
|
|
|
1071 /* The number of seconds may be very high so we cannot use
|
|
|
1072 the `get_number' macro. Instead read the number
|
|
|
1073 character for character and construct the result while
|
|
|
1074 doing this. */
|
|
|
1075 time_t secs = 0;
|
|
|
1076 if (*rp < '0' || *rp > '9')
|
|
|
1077 /* We need at least one digit. */
|
|
|
1078 return NULL;
|
|
|
1079
|
|
|
1080 do
|
|
|
1081 {
|
|
|
1082 secs *= 10;
|
|
|
1083 secs += *rp++ - '0';
|
|
|
1084 }
|
|
|
1085 while (*rp >= '0' && *rp <= '9');
|
|
|
1086
|
|
|
1087 if (localtime_r (&secs, tm) == NULL)
|
|
|
1088 /* Error in function. */
|
|
|
1089 return NULL;
|
|
|
1090 }
|
|
|
1091 break;
|
|
|
1092 case 'S':
|
|
|
1093 get_number (0, 61, 2);
|
|
|
1094 tm->tm_sec = val;
|
|
|
1095 break;
|
|
|
1096 case 'X':
|
|
|
1097 #ifdef _NL_CURRENT
|
|
|
1098 if (*decided != raw)
|
|
|
1099 {
|
|
|
1100 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
|
|
|
1101 {
|
|
|
1102 if (*decided == loc)
|
|
|
1103 return NULL;
|
|
|
1104 else
|
|
|
1105 rp = rp_backup;
|
|
|
1106 }
|
|
|
1107 else
|
|
|
1108 {
|
|
|
1109 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
|
|
|
1110 *decided = loc;
|
|
|
1111 break;
|
|
|
1112 }
|
|
|
1113 *decided = raw;
|
|
|
1114 }
|
|
|
1115 #endif
|
|
|
1116 /* Fall through. */
|
|
|
1117 case 'T':
|
|
|
1118 if (!recursive (HERE_T_FMT))
|
|
|
1119 return NULL;
|
|
|
1120 break;
|
|
|
1121 case 'u':
|
|
|
1122 get_number (1, 7, 1);
|
|
|
1123 tm->tm_wday = val % 7;
|
|
|
1124 have_wday = 1;
|
|
|
1125 break;
|
|
|
1126 case 'g':
|
|
|
1127 get_number (0, 99, 2);
|
|
|
1128 /* XXX This cannot determine any field in TM. */
|
|
|
1129 break;
|
|
|
1130 case 'G':
|
|
|
1131 if (*rp < '0' || *rp > '9')
|
|
|
1132 return NULL;
|
|
|
1133 /* XXX Ignore the number since we would need some more
|
|
|
1134 information to compute a real date. */
|
|
|
1135 do
|
|
|
1136 ++rp;
|
|
|
1137 while (*rp >= '0' && *rp <= '9');
|
|
|
1138 break;
|
|
|
1139 case 'U':
|
|
|
1140 case 'V':
|
|
|
1141 case 'W':
|
|
|
1142 get_number (0, 53, 2);
|
|
|
1143 /* XXX This cannot determine any field in TM without some
|
|
|
1144 information. */
|
|
|
1145 break;
|
|
|
1146 case 'w':
|
|
|
1147 /* Match number of weekday. */
|
|
|
1148 get_number (0, 6, 1);
|
|
|
1149 tm->tm_wday = val;
|
|
|
1150 have_wday = 1;
|
|
|
1151 break;
|
|
|
1152 case 'y':
|
|
|
1153 #ifdef _NL_CURRENT
|
|
|
1154 match_year_in_century:
|
|
|
1155 #endif
|
|
|
1156 /* Match year within century. */
|
|
|
1157 get_number (0, 99, 2);
|
|
|
1158 /* The "Year 2000: The Millennium Rollover" paper suggests that
|
|
|
1159 values in the range 69-99 refer to the twentieth century. */
|
|
|
1160 tm->tm_year = val >= 69 ? val : val + 100;
|
|
|
1161 /* Indicate that we want to use the century, if specified. */
|
|
|
1162 want_century = 1;
|
|
|
1163 want_xday = 1;
|
|
|
1164 break;
|
|
|
1165 case 'Y':
|
|
|
1166 /* Match year including century number. */
|
|
|
1167 get_number (0, 9999, 4);
|
|
|
1168 tm->tm_year = val - 1900;
|
|
|
1169 want_century = 0;
|
|
|
1170 want_xday = 1;
|
|
|
1171 break;
|
|
|
1172 case 'Z':
|
|
|
1173 /* XXX How to handle this? */
|
|
|
1174 break;
|
|
|
1175 case 'E':
|
|
|
1176 #ifdef _NL_CURRENT
|
|
|
1177 switch (*fmt++)
|
|
|
1178 {
|
|
|
1179 case 'c':
|
|
|
1180 /* Match locale's alternate date and time format. */
|
|
|
1181 if (*decided != raw)
|
|
|
1182 {
|
|
|
1183 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
|
|
|
1184
|
|
|
1185 if (*fmt == '\0')
|
|
|
1186 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
|
|
|
1187
|
|
|
1188 if (!recursive (fmt))
|
|
|
1189 {
|
|
|
1190 if (*decided == loc)
|
|
|
1191 return NULL;
|
|
|
1192 else
|
|
|
1193 rp = rp_backup;
|
|
|
1194 }
|
|
|
1195 else
|
|
|
1196 {
|
|
|
1197 if (strcmp (fmt, HERE_D_T_FMT))
|
|
|
1198 *decided = loc;
|
|
|
1199 want_xday = 1;
|
|
|
1200 break;
|
|
|
1201 }
|
|
|
1202 *decided = raw;
|
|
|
1203 }
|
|
|
1204 /* The C locale has no era information, so use the
|
|
|
1205 normal representation. */
|
|
|
1206 if (!recursive (HERE_D_T_FMT))
|
|
|
1207 return NULL;
|
|
|
1208 want_xday = 1;
|
|
|
1209 break;
|
|
|
1210 case 'C':
|
|
|
1211 if (*decided != raw)
|
|
|
1212 {
|
|
|
1213 if (era_cnt >= 0)
|
|
|
1214 {
|
|
|
1215 era = _nl_select_era_entry (era_cnt);
|
|
|
1216 if (match_string (era->era_name, rp))
|
|
|
1217 {
|
|
|
1218 *decided = loc;
|
|
|
1219 break;
|
|
|
1220 }
|
|
|
1221 else
|
|
|
1222 return NULL;
|
|
|
1223 }
|
|
|
1224 else
|
|
|
1225 {
|
|
|
1226 num_eras = _NL_CURRENT_WORD (LC_TIME,
|
|
|
1227 _NL_TIME_ERA_NUM_ENTRIES);
|
|
|
1228 for (era_cnt = 0; era_cnt < (int) num_eras;
|
|
|
1229 ++era_cnt, rp = rp_backup)
|
|
|
1230 {
|
|
|
1231 era = _nl_select_era_entry (era_cnt);
|
|
|
1232 if (match_string (era->era_name, rp))
|
|
|
1233 {
|
|
|
1234 *decided = loc;
|
|
|
1235 break;
|
|
|
1236 }
|
|
|
1237 }
|
|
|
1238 if (era_cnt == (int) num_eras)
|
|
|
1239 {
|
|
|
1240 era_cnt = -1;
|
|
|
1241 if (*decided == loc)
|
|
|
1242 return NULL;
|
|
|
1243 }
|
|
|
1244 else
|
|
|
1245 break;
|
|
|
1246 }
|
|
|
1247
|
|
|
1248 *decided = raw;
|
|
|
1249 }
|
|
|
1250 /* The C locale has no era information, so use the
|
|
|
1251 normal representation. */
|
|
|
1252 goto match_century;
|
|
|
1253 case 'y':
|
|
|
1254 if (*decided == raw)
|
|
|
1255 goto match_year_in_century;
|
|
|
1256
|
|
|
1257 get_number(0, 9999, 4);
|
|
|
1258 tm->tm_year = val;
|
|
|
1259 want_era = 1;
|
|
|
1260 want_xday = 1;
|
|
|
1261 break;
|
|
|
1262 case 'Y':
|
|
|
1263 if (*decided != raw)
|
|
|
1264 {
|
|
|
1265 num_eras = _NL_CURRENT_WORD (LC_TIME,
|
|
|
1266 _NL_TIME_ERA_NUM_ENTRIES);
|
|
|
1267 for (era_cnt = 0; era_cnt < (int) num_eras;
|
|
|
1268 ++era_cnt, rp = rp_backup)
|
|
|
1269 {
|
|
|
1270 era = _nl_select_era_entry (era_cnt);
|
|
|
1271 if (recursive (era->era_format))
|
|
|
1272 break;
|
|
|
1273 }
|
|
|
1274 if (era_cnt == (int) num_eras)
|
|
|
1275 {
|
|
|
1276 era_cnt = -1;
|
|
|
1277 if (*decided == loc)
|
|
|
1278 return NULL;
|
|
|
1279 else
|
|
|
1280 rp = rp_backup;
|
|
|
1281 }
|
|
|
1282 else
|
|
|
1283 {
|
|
|
1284 *decided = loc;
|
|
|
1285 era_cnt = -1;
|
|
|
1286 break;
|
|
|
1287 }
|
|
|
1288
|
|
|
1289 *decided = raw;
|
|
|
1290 }
|
|
|
1291 get_number (0, 9999, 4);
|
|
|
1292 tm->tm_year = val - 1900;
|
|
|
1293 want_century = 0;
|
|
|
1294 want_xday = 1;
|
|
|
1295 break;
|
|
|
1296 case 'x':
|
|
|
1297 if (*decided != raw)
|
|
|
1298 {
|
|
|
1299 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
|
|
|
1300
|
|
|
1301 if (*fmt == '\0')
|
|
|
1302 fmt = _NL_CURRENT (LC_TIME, D_FMT);
|
|
|
1303
|
|
|
1304 if (!recursive (fmt))
|
|
|
1305 {
|
|
|
1306 if (*decided == loc)
|
|
|
1307 return NULL;
|
|
|
1308 else
|
|
|
1309 rp = rp_backup;
|
|
|
1310 }
|
|
|
1311 else
|
|
|
1312 {
|
|
|
1313 if (strcmp (fmt, HERE_D_FMT))
|
|
|
1314 *decided = loc;
|
|
|
1315 break;
|
|
|
1316 }
|
|
|
1317 *decided = raw;
|
|
|
1318 }
|
|
|
1319 if (!recursive (HERE_D_FMT))
|
|
|
1320 return NULL;
|
|
|
1321 break;
|
|
|
1322 case 'X':
|
|
|
1323 if (*decided != raw)
|
|
|
1324 {
|
|
|
1325 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
|
|
|
1326
|
|
|
1327 if (*fmt == '\0')
|
|
|
1328 fmt = _NL_CURRENT (LC_TIME, T_FMT);
|
|
|
1329
|
|
|
1330 if (!recursive (fmt))
|
|
|
1331 {
|
|
|
1332 if (*decided == loc)
|
|
|
1333 return NULL;
|
|
|
1334 else
|
|
|
1335 rp = rp_backup;
|
|
|
1336 }
|
|
|
1337 else
|
|
|
1338 {
|
|
|
1339 if (strcmp (fmt, HERE_T_FMT))
|
|
|
1340 *decided = loc;
|
|
|
1341 break;
|
|
|
1342 }
|
|
|
1343 *decided = raw;
|
|
|
1344 }
|
|
|
1345 if (!recursive (HERE_T_FMT))
|
|
|
1346 return NULL;
|
|
|
1347 break;
|
|
|
1348 default:
|
|
|
1349 return NULL;
|
|
|
1350 }
|
|
|
1351 break;
|
|
|
1352 #else
|
|
|
1353 /* We have no information about the era format. Just use
|
|
|
1354 the normal format. */
|
|
|
1355 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
|
|
|
1356 && *fmt != 'x' && *fmt != 'X')
|
|
|
1357 /* This is an illegal format. */
|
|
|
1358 return NULL;
|
|
|
1359
|
|
|
1360 goto start_over;
|
|
|
1361 #endif
|
|
|
1362 case 'O':
|
|
|
1363 switch (*fmt++)
|
|
|
1364 {
|
|
|
1365 case 'd':
|
|
|
1366 case 'e':
|
|
|
1367 /* Match day of month using alternate numeric symbols. */
|
|
|
1368 get_alt_number (1, 31, 2);
|
|
|
1369 tm->tm_mday = val;
|
|
|
1370 have_mday = 1;
|
|
|
1371 want_xday = 1;
|
|
|
1372 break;
|
|
|
1373 case 'H':
|
|
|
1374 /* Match hour in 24-hour clock using alternate numeric
|
|
|
1375 symbols. */
|
|
|
1376 get_alt_number (0, 23, 2);
|
|
|
1377 tm->tm_hour = val;
|
|
|
1378 have_I = 0;
|
|
|
1379 break;
|
|
|
1380 case 'I':
|
|
|
1381 /* Match hour in 12-hour clock using alternate numeric
|
|
|
1382 symbols. */
|
|
|
1383 get_alt_number (1, 12, 2);
|
|
|
1384 tm->tm_hour = val - 1;
|
|
|
1385 have_I = 1;
|
|
|
1386 break;
|
|
|
1387 case 'm':
|
|
|
1388 /* Match month using alternate numeric symbols. */
|
|
|
1389 get_alt_number (1, 12, 2);
|
|
|
1390 tm->tm_mon = val - 1;
|
|
|
1391 have_mon = 1;
|
|
|
1392 want_xday = 1;
|
|
|
1393 break;
|
|
|
1394 case 'M':
|
|
|
1395 /* Match minutes using alternate numeric symbols. */
|
|
|
1396 get_alt_number (0, 59, 2);
|
|
|
1397 tm->tm_min = val;
|
|
|
1398 break;
|
|
|
1399 case 'S':
|
|
|
1400 /* Match seconds using alternate numeric symbols. */
|
|
|
1401 get_alt_number (0, 61, 2);
|
|
|
1402 tm->tm_sec = val;
|
|
|
1403 break;
|
|
|
1404 case 'U':
|
|
|
1405 case 'V':
|
|
|
1406 case 'W':
|
|
|
1407 get_alt_number (0, 53, 2);
|
|
|
1408 /* XXX This cannot determine any field in TM without
|
|
|
1409 further information. */
|
|
|
1410 break;
|
|
|
1411 case 'w':
|
|
|
1412 /* Match number of weekday using alternate numeric symbols. */
|
|
|
1413 get_alt_number (0, 6, 1);
|
|
|
1414 tm->tm_wday = val;
|
|
|
1415 have_wday = 1;
|
|
|
1416 break;
|
|
|
1417 case 'y':
|
|
|
1418 /* Match year within century using alternate numeric symbols. */
|
|
|
1419 get_alt_number (0, 99, 2);
|
|
|
1420 tm->tm_year = val >= 69 ? val : val + 100;
|
|
|
1421 want_xday = 1;
|
|
|
1422 break;
|
|
|
1423 default:
|
|
|
1424 return NULL;
|
|
|
1425 }
|
|
|
1426 break;
|
|
|
1427 default:
|
|
|
1428 return NULL;
|
|
|
1429 }
|
|
|
1430 }
|
|
|
1431
|
|
|
1432 if (have_I && is_pm)
|
|
|
1433 tm->tm_hour += 12;
|
|
|
1434
|
|
|
1435 if (century != -1)
|
|
|
1436 {
|
|
|
1437 if (want_century)
|
|
|
1438 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
|
|
|
1439 else
|
|
|
1440 /* Only the century, but not the year. Strange, but so be it. */
|
|
|
1441 tm->tm_year = (century - 19) * 100;
|
|
|
1442 }
|
|
|
1443
|
|
|
1444 #ifdef _NL_CURRENT
|
|
|
1445 if (era_cnt != -1)
|
|
|
1446 {
|
|
|
1447 era = _nl_select_era_entry(era_cnt);
|
|
|
1448 if (want_era)
|
|
|
1449 tm->tm_year = (era->start_date[0]
|
|
|
1450 + ((tm->tm_year - era->offset)
|
|
|
1451 * era->absolute_direction));
|
|
|
1452 else
|
|
|
1453 /* Era start year assumed. */
|
|
|
1454 tm->tm_year = era->start_date[0];
|
|
|
1455 }
|
|
|
1456 else
|
|
|
1457 #endif
|
|
|
1458 if (want_era)
|
|
|
1459 return NULL;
|
|
|
1460
|
|
|
1461 if (want_xday && !have_wday)
|
|
|
1462 {
|
|
|
1463 if ( !(have_mon && have_mday) && have_yday)
|
|
|
1464 {
|
|
|
1465 /* We don't have tm_mon and/or tm_mday, compute them. */
|
|
|
1466 int t_mon = 0;
|
|
|
1467 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
|
|
|
1468 t_mon++;
|
|
|
1469 if (!have_mon)
|
|
|
1470 tm->tm_mon = t_mon - 1;
|
|
|
1471 if (!have_mday)
|
|
|
1472 tm->tm_mday =
|
|
|
1473 (tm->tm_yday
|
|
|
1474 - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1);
|
|
|
1475 }
|
|
|
1476 day_of_the_week (tm);
|
|
|
1477 }
|
|
|
1478 if (want_xday && !have_yday)
|
|
|
1479 day_of_the_year (tm);
|
|
|
1480
|
|
|
1481 return (char *) rp;
|
|
|
1482 }
|
|
|
1483
|
|
|
1484
|
|
|
1485 char *
|
|
|
1486 msn_strptime (buf, format, tm)
|
|
|
1487 const char *buf;
|
|
|
1488 const char *format;
|
|
|
1489 struct tm *tm;
|
|
|
1490 {
|
|
|
1491 enum locale_status decided;
|
|
|
1492
|
|
|
1493 #ifdef _NL_CURRENT
|
|
|
1494 decided = not;
|
|
|
1495 #else
|
|
|
1496 decided = raw;
|
|
|
1497 #endif
|
|
|
1498 return strptime_internal (buf, format, tm, &decided, -1);
|
|
|
1499 }
|
|
|
1500 #else
|
|
|
1501 #define msn_strptime strptime
|
|
|
1502 #endif
|