1 /* Replacements for routines missing on some systems.
2 Copyright (C) 1995-2005 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
38 #endif /* HAVE_STRING_H */
40 #include <sys/types.h>
54 /* A strerror() clone, for systems that don't have it. */
58 /* This loses on a system without `sys_errlist'. */
59 extern char *sys_errlist[];
60 return sys_errlist[err];
62 #endif /* not HAVE_STRERROR */
64 /* Some systems don't have some str* functions in libc. Here we
65 define them. The same goes for strptime. */
67 #ifndef HAVE_STRCASECMP
69 /* Compare S1 and S2, ignoring case, returning less than, equal to or
70 greater than zero if S1 is lexiographically less than,
71 equal to or greater than S2. */
73 strcasecmp (const char *s1, const char *s2)
75 register const unsigned char *p1 = (const unsigned char *) s1;
76 register const unsigned char *p2 = (const unsigned char *) s2;
93 #endif /* not HAVE_STRCASECMP */
95 #ifndef HAVE_STRNCASECMP
97 /* Compare no more than N characters of S1 and S2,
98 ignoring case, returning less than, equal to or
99 greater than zero if S1 is lexicographically less
100 than, equal to or greater than S2. */
102 strncasecmp (const char *s1, const char *s2, size_t n)
104 register const unsigned char *p1 = (const unsigned char *) s1;
105 register const unsigned char *p2 = (const unsigned char *) s2;
106 unsigned char c1, c2;
108 if (p1 == p2 || n == 0)
113 c1 = TOLOWER (*p1++);
114 c2 = TOLOWER (*p2++);
115 if (c1 == '\0' || c1 != c2)
121 #endif /* not HAVE_STRNCASECMP */
124 /* From GNU libc 2.3.5. */
127 * My personal strstr() implementation that beats most other algorithms.
128 * Until someone tells me otherwise, I assume that this is the
129 * fastest implementation of strstr() in C.
130 * I deliberately chose not to comment it. You should have at least
131 * as much fun trying to understand it, as I had to write it :-).
133 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
135 typedef unsigned chartype;
140 strstr (const char *phaystack, const char *pneedle)
142 const unsigned char *haystack, *needle;
144 const unsigned char *rneedle;
146 haystack = (const unsigned char *) phaystack;
148 if ((b = *(needle = (const unsigned char *) pneedle)))
151 haystack--; /* possible ANSI violation */
156 if (!(a = *++haystack))
161 if (!(c = *++needle))
172 if ((a = *++haystack) == c)
179 for (; a != b; a = *++haystack)
183 if ((a = *++haystack) == b)
189 while ((a = *++haystack) != c);
195 const unsigned char *rhaystack;
196 if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
201 if (*++rhaystack != (a = *++needle))
206 while (*++rhaystack == (a = *++needle));
207 needle = rneedle; /* took the register-poor aproach */
215 return (char *) haystack;
219 #endif /* not HAVE_STRSTR */
222 /* Find the first ocurrence in S of any character in ACCEPT. */
224 strpbrk (const char *s, const char *accept)
228 const char *a = accept;
237 #endif /* HAVE_STRPBRK */
240 /* From GNU libc 2.0. */
242 /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
243 This file is part of the GNU C Library.
244 Contributed by Paul Eggert (eggert@twinsun.com). */
247 # define HAVE_LIMITS_H 1
248 # define HAVE_LOCALTIME_R 1
249 # define STDC_HEADERS 1
252 /* Assume that leap seconds are possible, unless told otherwise.
253 If the host has a `zic' command with a `-L leapsecondfilename' option,
254 then it supports leap seconds; otherwise it probably doesn't. */
255 #ifndef LEAP_SECONDS_POSSIBLE
256 # define LEAP_SECONDS_POSSIBLE 1
260 # define __P(args) PARAMS (args)
261 #endif /* Not __P. */
268 # define INT_MIN (~0 << (sizeof (int) * CHAR_BIT - 1))
271 # define INT_MAX (~0 - INT_MIN)
275 /* The outer cast to time_t works around a bug in Cray C 5.0.3.0. */
276 # define TIME_T_MIN ((time_t) \
277 (0 < (time_t) -1 ? (time_t) 0 \
278 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1)))
281 # define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
284 #define TM_YEAR_BASE 1900
285 #define EPOCH_YEAR 1970
288 /* Nonzero if YEAR is a leap year (every 4 years,
289 except every 100th isn't, and every 400th is). */
290 # define __isleap(year) \
291 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
294 /* How many days come before each month (0-12). */
295 /* __mon_yday[][] is common to mktime and strptime implementations.
297 const unsigned short int __mon_yday[2][13];
298 #ifndef NEED_MON_YDAY
299 # define NEED_MON_YDAY
302 static time_t ydhms_tm_diff __P ((int, int, int, int, int, const struct tm *));
303 time_t __mktime_internal __P ((struct tm *,
304 struct tm *(*) (const time_t *, struct tm *),
309 # define localtime_r __localtime_r
311 # if ! HAVE_LOCALTIME_R && ! defined localtime_r
312 /* Approximate localtime_r as best we can in its absence. */
313 # define localtime_r my_mktime_localtime_r
314 static struct tm *localtime_r __P ((const time_t *, struct tm *));
320 struct tm *l = localtime (t);
326 # endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
330 /* Yield the difference between (YEAR-YDAY HOUR:MIN:SEC) and (*TP),
331 measured in seconds, ignoring leap seconds.
332 YEAR uses the same numbering as TM->tm_year.
333 All values are in range, except possibly YEAR.
334 If overflow occurs, yield the low order bits of the correct answer. */
336 ydhms_tm_diff (year, yday, hour, min, sec, tp)
337 int year, yday, hour, min, sec;
340 /* Compute intervening leap days correctly even if year is negative.
341 Take care to avoid int overflow. time_t overflow is OK, since
342 only the low order bits of the correct time_t answer are needed.
343 Don't convert to time_t until after all divisions are done, since
344 time_t might be unsigned. */
345 int a4 = (year >> 2) + (TM_YEAR_BASE >> 2) - ! (year & 3);
346 int b4 = (tp->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (tp->tm_year & 3);
347 int a100 = a4 / 25 - (a4 % 25 < 0);
348 int b100 = b4 / 25 - (b4 % 25 < 0);
349 int a400 = a100 >> 2;
350 int b400 = b100 >> 2;
351 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
352 time_t years = year - (time_t) tp->tm_year;
353 time_t days = (365 * years + intervening_leap_days
354 + (yday - tp->tm_yday));
355 return (60 * (60 * (24 * days + (hour - tp->tm_hour))
356 + (min - tp->tm_min))
357 + (sec - tp->tm_sec));
361 static time_t localtime_offset;
363 /* Convert *TP to a time_t value. */
369 /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
370 time zone names contained in the external variable `tzname' shall
371 be set as if the tzset() function had been called. */
375 return __mktime_internal (tp, localtime_r, &localtime_offset);
378 /* Convert *TP to a time_t value, inverting
379 the monotonic and mostly-unit-linear conversion function CONVERT.
380 Use *OFFSET to keep track of a guess at the offset of the result,
381 compared to what the result would be for UTC without leap seconds.
382 If *OFFSET's guess is correct, only one CONVERT call is needed. */
384 __mktime_internal (tp, convert, offset)
386 struct tm *(*convert) __P ((const time_t *, struct tm *));
392 /* The maximum number of probes (calls to CONVERT) should be enough
393 to handle any combinations of time zone rule changes, solar time,
394 and leap seconds. Posix.1 prohibits leap seconds, but some hosts
396 int remaining_probes = 4;
398 /* Time requested. Copy it in case CONVERT modifies *TP; this can
399 occur if TP is localtime's returned value and CONVERT is localtime. */
400 int sec = tp->tm_sec;
401 int min = tp->tm_min;
402 int hour = tp->tm_hour;
403 int mday = tp->tm_mday;
404 int mon = tp->tm_mon;
405 int year_requested = tp->tm_year;
406 int isdst = tp->tm_isdst;
408 /* Ensure that mon is in range, and set year accordingly. */
409 int mon_remainder = mon % 12;
410 int negative_mon_remainder = mon_remainder < 0;
411 int mon_years = mon / 12 - negative_mon_remainder;
412 int year = year_requested + mon_years;
414 /* The other values need not be in range:
415 the remaining code handles minor overflows correctly,
416 assuming int and time_t arithmetic wraps around.
417 Major overflows are caught at the end. */
419 /* Calculate day of year from year, month, and day of month.
420 The result need not be in range. */
421 int yday = ((__mon_yday[__isleap (year + TM_YEAR_BASE)]
422 [mon_remainder + 12 * negative_mon_remainder])
425 int sec_requested = sec;
426 #if LEAP_SECONDS_POSSIBLE
427 /* Handle out-of-range seconds specially,
428 since ydhms_tm_diff assumes every minute has 60 seconds. */
435 /* Invert CONVERT by probing. First assume the same offset as last time.
436 Then repeatedly use the error to improve the guess. */
438 tm.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
439 tm.tm_yday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
440 t0 = ydhms_tm_diff (year, yday, hour, min, sec, &tm);
442 for (t = t0 + *offset;
443 (dt = ydhms_tm_diff (year, yday, hour, min, sec, (*convert) (&t, &tm)));
445 if (--remaining_probes == 0)
448 /* Check whether tm.tm_isdst has the requested value, if any. */
449 if (0 <= isdst && 0 <= tm.tm_isdst)
451 int dst_diff = (isdst != 0) - (tm.tm_isdst != 0);
454 /* Move two hours in the direction indicated by the disagreement,
455 probe some more, and switch to a new time if found.
456 The largest known fallback due to daylight savings is two hours:
457 once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */
458 time_t ot = t - 2 * 60 * 60 * dst_diff;
459 while (--remaining_probes != 0)
462 if (! (dt = ydhms_tm_diff (year, yday, hour, min, sec,
463 (*convert) (&ot, &otm))))
470 break; /* Avoid a redundant probe. */
477 #if LEAP_SECONDS_POSSIBLE
478 if (sec_requested != tm.tm_sec)
480 /* Adjust time to reflect the tm_sec requested, not the normalized value.
481 Also, repair any damage from a false match due to a leap second. */
482 t += sec_requested - sec + (sec == 0 && tm.tm_sec == 60);
483 (*convert) (&t, &tm);
487 if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3)
489 /* time_t isn't large enough to rule out overflows in ydhms_tm_diff,
490 so check for major overflows. A gross check suffices,
491 since if t has overflowed, it is off by a multiple of
492 TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of
493 the difference that is bounded by a small value. */
495 double dyear = (double) year_requested + mon_years - tm.tm_year;
496 double dday = 366 * dyear + mday;
497 double dsec = 60 * (60 * (24 * dday + hour) + min) + sec_requested;
499 if (TIME_T_MAX / 3 - TIME_T_MIN / 3 < (dsec < 0 ? - dsec : dsec))
508 weak_alias (mktime, timelocal)
510 #endif /* not HAVE_MKTIME */
513 #ifndef HAVE_STRPTIME
514 /* From GNU libc 2.1.3. */
515 /* Ulrich, thanks for helping me out with this! --hniksic */
517 /* strptime - Convert a string representation of time to a time value.
518 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
519 This file is part of the GNU C Library.
520 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. */
522 /* XXX This version of the implementation is not really complete.
523 Some of the fields cannot add information alone. But if seeing
524 some of them in the same format (such as year, week and weekday)
525 this is enough information for determining the date. */
528 # define __P(args) PARAMS (args)
531 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
533 # define localtime_r __localtime_r
535 /* Approximate localtime_r as best we can in its absence. */
536 # define localtime_r my_localtime_r
537 static struct tm *localtime_r __P ((const time_t *, struct tm *));
543 struct tm *l = localtime (t);
549 # endif /* ! _LIBC */
550 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
553 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
554 #if defined __GNUC__ && __GNUC__ >= 2
555 # define match_string(cs1, s2) \
556 ({ size_t len = strlen (cs1); \
557 int result = strncasecmp ((cs1), (s2), len) == 0; \
558 if (result) (s2) += len; \
561 /* Oh come on. Get a reasonable compiler. */
562 # define match_string(cs1, s2) \
563 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
565 /* We intentionally do not use isdigit() for testing because this will
566 lead to problems with the wide character version. */
567 #define get_number(from, to, n) \
573 if (*rp < '0' || *rp > '9') \
577 val += *rp++ - '0'; \
578 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
579 if (val < from || val > to) \
583 /* Added check for __GNUC__ extensions here for Wget. --abbotti */
584 # if defined __GNUC__ && __GNUC__ >= 2
585 # define get_alt_number(from, to, n) \
587 __label__ do_normal; \
588 if (*decided != raw) \
590 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
598 while (*alts != '\0') \
600 size_t len = strlen (alts); \
601 if (strncasecmp (alts, rp, len) == 0) \
608 if (*decided == not && ! any) \
610 /* If we haven't read anything it's an error. */ \
613 /* Correct the premature multiplication. */ \
619 } while (--__n > 0 && val * 10 <= to); \
620 if (val < from || val > to) \
626 get_number (from, to, n); \
631 # define get_alt_number(from, to, n) \
633 if (*decided != raw) \
635 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
643 while (*alts != '\0') \
645 size_t len = strlen (alts); \
646 if (strncasecmp (alts, rp, len) == 0) \
653 if (*decided == not && ! any) \
655 /* If we haven't read anything it's an error. */ \
658 /* Correct the premature multiplication. */ \
664 } while (--__n > 0 && val * 10 <= to); \
665 if (val < from || val > to) \
671 get_number (from, to, n); \
674 # endif /* defined __GNUC__ && __GNUC__ >= 2 */
676 # define get_alt_number(from, to, n) \
677 /* We don't have the alternate representation. */ \
678 get_number(from, to, n)
680 #define recursive(new_fmt) \
681 (*(new_fmt) != '\0' \
682 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
686 /* This is defined in locale/C-time.c in the GNU libc. */
687 extern const struct locale_data _nl_C_LC_TIME;
688 extern const unsigned short int __mon_yday[2][13];
690 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
691 # define ab_weekday_name \
692 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
693 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
694 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
695 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
696 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
697 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
698 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
699 # define HERE_T_FMT_AMPM \
700 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
701 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
703 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
705 static char const weekday_name[][10] =
707 "Sunday", "Monday", "Tuesday", "Wednesday",
708 "Thursday", "Friday", "Saturday"
710 static char const ab_weekday_name[][4] =
712 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
714 static char const month_name[][10] =
716 "January", "February", "March", "April", "May", "June",
717 "July", "August", "September", "October", "November", "December"
719 static char const ab_month_name[][4] =
721 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
722 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
724 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
725 # define HERE_D_FMT "%m/%d/%y"
726 # define HERE_AM_STR "AM"
727 # define HERE_PM_STR "PM"
728 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
729 # define HERE_T_FMT "%H:%M:%S"
731 /* __mon_yday[][] is common to mktime and strptime implementations.
733 const unsigned short int __mon_yday[2][13];
734 # ifndef NEED_MON_YDAY
735 # define NEED_MON_YDAY
739 /* Status of lookup: do we use the locale data or the raw data? */
740 enum locale_status { not, loc, raw };
744 /* Nonzero if YEAR is a leap year (every 4 years,
745 except every 100th isn't, and every 400th is). */
746 # define __isleap(year) \
747 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
750 /* Compute the day of the week. */
752 day_of_the_week (struct tm *tm)
754 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
755 the difference between this data in the one on TM and so determine
757 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
759 + (365 * (tm->tm_year - 70))
761 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
762 + (((corr_year / 4) / 25) / 4)
763 + __mon_yday[0][tm->tm_mon]
765 tm->tm_wday = ((wday % 7) + 7) % 7;
768 /* Compute the day of the year. */
770 day_of_the_year (struct tm *tm)
772 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
773 + (tm->tm_mday - 1));
780 strptime_internal __P ((const char *buf, const char *format, struct tm *tm,
781 enum locale_status *decided));
787 strptime_internal (rp, fmt, tm, decided)
791 enum locale_status *decided;
794 const char *rp_backup;
799 int century, want_century;
800 int have_wday, want_xday;
802 int have_mon, have_mday;
807 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
811 /* A white space in the format string matches 0 more or white
812 space in the input string. */
815 while (ISSPACE (*rp))
821 /* Any character but `%' must be matched by the same character
822 in the iput string. */
825 match_char (*fmt++, *rp++);
831 /* We need this for handling the `E' modifier. */
836 /* Make back up of current processing pointer. */
843 /* Match the `%' character itself. */
844 match_char ('%', *rp++);
848 /* Match day of week. */
849 for (cnt = 0; cnt < 7; ++cnt)
854 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
857 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
862 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
865 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
866 ab_weekday_name[cnt]))
873 && (match_string (weekday_name[cnt], rp)
874 || match_string (ab_weekday_name[cnt], rp)))
881 /* Does not match a weekday name. */
889 /* Match month name. */
890 for (cnt = 0; cnt < 12; ++cnt)
895 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
898 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
903 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
906 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
913 if (match_string (month_name[cnt], rp)
914 || match_string (ab_month_name[cnt], rp))
921 /* Does not match a month name. */
927 /* Match locale's date and time format. */
931 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
940 if (*decided == not &&
941 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
949 if (!recursive (HERE_D_T_FMT))
954 /* Match century number. */
955 get_number (0, 99, 2);
961 /* Match day of month. */
962 get_number (1, 31, 2);
968 if (!recursive ("%Y-%m-%d"))
976 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
986 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
996 /* Match standard day format. */
997 if (!recursive (HERE_D_FMT))
1003 /* Match hour in 24-hour clock. */
1004 get_number (0, 23, 2);
1009 /* Match hour in 12-hour clock. */
1010 get_number (1, 12, 2);
1011 tm->tm_hour = val % 12;
1015 /* Match day number of year. */
1016 get_number (1, 366, 3);
1017 tm->tm_yday = val - 1;
1021 /* Match number of month. */
1022 get_number (1, 12, 2);
1023 tm->tm_mon = val - 1;
1029 get_number (0, 59, 2);
1034 /* Match any white space. */
1035 while (ISSPACE (*rp))
1039 /* Match locale's equivalent of AM/PM. */
1041 if (*decided != raw)
1043 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
1045 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
1049 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
1051 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
1059 if (!match_string (HERE_AM_STR, rp))
1060 if (match_string (HERE_PM_STR, rp))
1067 if (*decided != raw)
1069 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
1071 if (*decided == loc)
1078 if (*decided == not &&
1079 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
1087 if (!recursive (HERE_T_FMT_AMPM))
1091 if (!recursive ("%H:%M"))
1096 /* The number of seconds may be very high so we cannot use
1097 the `get_number' macro. Instead read the number
1098 character for character and construct the result while
1101 if (*rp < '0' || *rp > '9')
1102 /* We need at least one digit. */
1108 secs += *rp++ - '0';
1110 while (*rp >= '0' && *rp <= '9');
1112 if (localtime_r (&secs, tm) == NULL)
1113 /* Error in function. */
1118 get_number (0, 61, 2);
1123 if (*decided != raw)
1125 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
1127 if (*decided == loc)
1134 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
1143 if (!recursive (HERE_T_FMT))
1147 get_number (1, 7, 1);
1148 tm->tm_wday = val % 7;
1152 get_number (0, 99, 2);
1153 /* XXX This cannot determine any field in TM. */
1156 if (*rp < '0' || *rp > '9')
1158 /* XXX Ignore the number since we would need some more
1159 information to compute a real date. */
1162 while (*rp >= '0' && *rp <= '9');
1167 get_number (0, 53, 2);
1168 /* XXX This cannot determine any field in TM without some
1172 /* Match number of weekday. */
1173 get_number (0, 6, 1);
1178 /* Match year within century. */
1179 get_number (0, 99, 2);
1180 /* The "Year 2000: The Millennium Rollover" paper suggests that
1181 values in the range 69-99 refer to the twentieth century. */
1182 tm->tm_year = val >= 69 ? val : val + 100;
1183 /* Indicate that we want to use the century, if specified. */
1188 /* Match year including century number. */
1189 get_number (0, 9999, 4);
1190 tm->tm_year = val - 1900;
1195 /* XXX How to handle this? */
1202 /* Match locale's alternate date and time format. */
1203 if (*decided != raw)
1205 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
1208 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
1210 if (!recursive (fmt))
1212 if (*decided == loc)
1219 if (strcmp (fmt, HERE_D_T_FMT))
1226 /* The C locale has no era information, so use the
1227 normal representation. */
1228 if (!recursive (HERE_D_T_FMT))
1235 /* Match name of base year in locale's alternate
1237 /* XXX This is currently not implemented. It should
1238 use the value _NL_CURRENT (LC_TIME, ERA). */
1241 if (*decided != raw)
1243 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
1246 fmt = _NL_CURRENT (LC_TIME, D_FMT);
1248 if (!recursive (fmt))
1250 if (*decided == loc)
1257 if (strcmp (fmt, HERE_D_FMT))
1263 if (!recursive (HERE_D_FMT))
1267 if (*decided != raw)
1269 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
1272 fmt = _NL_CURRENT (LC_TIME, T_FMT);
1274 if (!recursive (fmt))
1276 if (*decided == loc)
1283 if (strcmp (fmt, HERE_T_FMT))
1289 if (!recursive (HERE_T_FMT))
1297 /* We have no information about the era format. Just use
1298 the normal format. */
1299 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1300 && *fmt != 'x' && *fmt != 'X')
1301 /* This is an illegal format. */
1311 /* Match day of month using alternate numeric symbols. */
1312 get_alt_number (1, 31, 2);
1318 /* Match hour in 24-hour clock using alternate numeric
1320 get_alt_number (0, 23, 2);
1325 /* Match hour in 12-hour clock using alternate numeric
1327 get_alt_number (1, 12, 2);
1328 tm->tm_hour = val - 1;
1332 /* Match month using alternate numeric symbols. */
1333 get_alt_number (1, 12, 2);
1334 tm->tm_mon = val - 1;
1339 /* Match minutes using alternate numeric symbols. */
1340 get_alt_number (0, 59, 2);
1344 /* Match seconds using alternate numeric symbols. */
1345 get_alt_number (0, 61, 2);
1351 get_alt_number (0, 53, 2);
1352 /* XXX This cannot determine any field in TM without
1353 further information. */
1356 /* Match number of weekday using alternate numeric symbols. */
1357 get_alt_number (0, 6, 1);
1362 /* Match year within century using alternate numeric symbols. */
1363 get_alt_number (0, 99, 2);
1364 tm->tm_year = val >= 69 ? val : val + 100;
1376 if (have_I && is_pm)
1382 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1384 /* Only the century, but not the year. Strange, but so be it. */
1385 tm->tm_year = (century - 19) * 100;
1388 if (want_xday && !have_wday) {
1389 if ( !(have_mon && have_mday) && have_yday) {
1390 /* we don't have tm_mon and/or tm_mday, compute them */
1392 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1395 tm->tm_mon = t_mon - 1;
1397 tm->tm_mday = tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1;
1399 day_of_the_week (tm);
1401 if (want_xday && !have_yday)
1402 day_of_the_year (tm);
1409 strptime (buf, format, tm)
1414 enum locale_status decided;
1420 return strptime_internal (buf, format, tm, &decided);
1422 #endif /* not HAVE_STRPTIME */
1424 #ifdef NEED_MON_YDAY
1425 /* __mon_yday[][] is common to mktime and strptime implementations.
1427 const unsigned short int __mon_yday[2][13] =
1430 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
1432 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
1436 #ifndef HAVE_MEMMOVE
1438 memmove (char *dest, const char *source, unsigned length)
1442 /* Moving from low mem to hi mem; start at end. */
1443 for (source += length, dest += length; length; --length)
1444 *--dest = *--source;
1445 else if (source != dest)
1447 /* Moving from hi mem to low mem; start at beginning. */
1448 for (; length; --length)
1449 *dest++ = *source++;
1453 #endif /* not HAVE_MEMMOVE */
1455 /* fnmatch is a POSIX function, but we include an implementation for
1456 the sake of systems that don't have it. Furthermore, according to
1457 anecdotal evidence, historical implementations of fnmatch are buggy
1458 and unreliable. So we use our version, except when compiling under
1459 systems where fnmatch is known to work (currently glibc.) */
1461 #ifndef SYSTEM_FNMATCH
1463 #define __FNM_FLAGS (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD)
1465 /* Match STRING against the filename pattern PATTERN, returning zero
1466 if it matches, FNM_NOMATCH if not. This implementation comes from
1467 an earlier version of GNU Bash. (It doesn't make sense to update
1468 it with a newer version because it adds a lot of features Wget
1469 doesn't use or care about.) */
1472 fnmatch (const char *pattern, const char *string, int flags)
1474 register const char *p = pattern, *n = string;
1477 if ((flags & ~__FNM_FLAGS) != 0)
1483 while ((c = *p++) != '\0')
1489 return (FNM_NOMATCH);
1490 else if ((flags & FNM_PATHNAME) && *n == '/')
1491 return (FNM_NOMATCH);
1492 else if ((flags & FNM_PERIOD) && *n == '.' &&
1493 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1494 return (FNM_NOMATCH);
1498 if (!(flags & FNM_NOESCAPE))
1501 return (FNM_NOMATCH);
1505 if ((flags & FNM_PERIOD) && *n == '.' &&
1506 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1507 return (FNM_NOMATCH);
1509 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
1510 if (((flags & FNM_PATHNAME) && *n == '/') ||
1511 (c == '?' && *n == '\0'))
1512 return (FNM_NOMATCH);
1518 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
1519 for (--p; *n != '\0'; ++n)
1520 if ((c == '[' || *n == c1) &&
1521 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
1523 return (FNM_NOMATCH);
1528 /* Nonzero if the sense of the character class is
1533 return (FNM_NOMATCH);
1535 if ((flags & FNM_PERIOD) && *n == '.' &&
1536 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1537 return (FNM_NOMATCH);
1539 /* Make sure there is a closing `]'. If there isn't,
1540 the `[' is just a character to be matched. */
1542 register const char *np;
1544 for (np = p; np && *np && *np != ']'; np++);
1549 return (FNM_NOMATCH);
1554 not = (*p == '!' || *p == '^');
1561 register char cstart = c, cend = c;
1563 if (!(flags & FNM_NOESCAPE) && c == '\\')
1564 cstart = cend = *p++;
1567 /* [ (unterminated) loses. */
1568 return (FNM_NOMATCH);
1572 if ((flags & FNM_PATHNAME) && c == '/')
1573 /* [/] can never match. */
1574 return (FNM_NOMATCH);
1576 if (c == '-' && *p != ']')
1579 if (!(flags & FNM_NOESCAPE) && cend == '\\')
1582 return (FNM_NOMATCH);
1586 if (*n >= cstart && *n <= cend)
1593 return (FNM_NOMATCH);
1599 /* Skip the rest of the [...] that already matched. */
1603 /* [... (unterminated) loses. */
1604 return (FNM_NOMATCH);
1607 if (!(flags & FNM_NOESCAPE) && c == '\\')
1608 /* 1003.2d11 is unclear if this is right. %%% */
1612 return (FNM_NOMATCH);
1618 return (FNM_NOMATCH);
1627 return (FNM_NOMATCH);
1630 #endif /* not SYSTEM_FNMATCH */