1 /* Replacements for routines missing on some systems.
2 Copyright (C) 1995, 1996, 1997 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.0.6. */
125 /* Return the first ocurrence of NEEDLE in HAYSTACK. */
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 */
134 typedef unsigned chartype;
137 strstr (phaystack, pneedle)
138 const char *phaystack;
141 register const unsigned char *haystack, *needle;
142 register chartype b, c;
144 haystack = (const unsigned char *) phaystack;
145 needle = (const unsigned char *) pneedle;
150 haystack--; /* possible ANSI violation */
168 register const unsigned char *rhaystack, *rneedle;
183 jin: a = *++haystack;
190 rhaystack = haystack-- + 1;
208 while (*rhaystack == a);
210 needle = rneedle; /* took the register-poor approach */
217 return (char*) haystack;
221 #endif /* not HAVE_STRSTR */
224 /* Find the first ocurrence in S of any character in ACCEPT. */
226 strpbrk (const char *s, const char *accept)
230 const char *a = accept;
239 #endif /* HAVE_STRPBRK */
242 /* From GNU libc 2.0. */
244 /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
245 This file is part of the GNU C Library.
246 Contributed by Paul Eggert (eggert@twinsun.com). */
249 # define HAVE_LIMITS_H 1
250 # define HAVE_LOCALTIME_R 1
251 # define STDC_HEADERS 1
254 /* Assume that leap seconds are possible, unless told otherwise.
255 If the host has a `zic' command with a `-L leapsecondfilename' option,
256 then it supports leap seconds; otherwise it probably doesn't. */
257 #ifndef LEAP_SECONDS_POSSIBLE
258 # define LEAP_SECONDS_POSSIBLE 1
262 # define __P(args) PARAMS (args)
263 #endif /* Not __P. */
270 # define INT_MIN (~0 << (sizeof (int) * CHAR_BIT - 1))
273 # define INT_MAX (~0 - INT_MIN)
277 /* The outer cast to time_t works around a bug in Cray C 5.0.3.0. */
278 # define TIME_T_MIN ((time_t) \
279 (0 < (time_t) -1 ? (time_t) 0 \
280 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1)))
283 # define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
286 #define TM_YEAR_BASE 1900
287 #define EPOCH_YEAR 1970
290 /* Nonzero if YEAR is a leap year (every 4 years,
291 except every 100th isn't, and every 400th is). */
292 # define __isleap(year) \
293 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
296 /* How many days come before each month (0-12). */
297 /* __mon_yday[][] is common to mktime and strptime implementations.
299 const unsigned short int __mon_yday[2][13];
300 #ifndef NEED_MON_YDAY
301 # define NEED_MON_YDAY
304 static time_t ydhms_tm_diff __P ((int, int, int, int, int, const struct tm *));
305 time_t __mktime_internal __P ((struct tm *,
306 struct tm *(*) (const time_t *, struct tm *),
311 # define localtime_r __localtime_r
313 # if ! HAVE_LOCALTIME_R && ! defined localtime_r
314 /* Approximate localtime_r as best we can in its absence. */
315 # define localtime_r my_mktime_localtime_r
316 static struct tm *localtime_r __P ((const time_t *, struct tm *));
322 struct tm *l = localtime (t);
328 # endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
332 /* Yield the difference between (YEAR-YDAY HOUR:MIN:SEC) and (*TP),
333 measured in seconds, ignoring leap seconds.
334 YEAR uses the same numbering as TM->tm_year.
335 All values are in range, except possibly YEAR.
336 If overflow occurs, yield the low order bits of the correct answer. */
338 ydhms_tm_diff (year, yday, hour, min, sec, tp)
339 int year, yday, hour, min, sec;
342 /* Compute intervening leap days correctly even if year is negative.
343 Take care to avoid int overflow. time_t overflow is OK, since
344 only the low order bits of the correct time_t answer are needed.
345 Don't convert to time_t until after all divisions are done, since
346 time_t might be unsigned. */
347 int a4 = (year >> 2) + (TM_YEAR_BASE >> 2) - ! (year & 3);
348 int b4 = (tp->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (tp->tm_year & 3);
349 int a100 = a4 / 25 - (a4 % 25 < 0);
350 int b100 = b4 / 25 - (b4 % 25 < 0);
351 int a400 = a100 >> 2;
352 int b400 = b100 >> 2;
353 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
354 time_t years = year - (time_t) tp->tm_year;
355 time_t days = (365 * years + intervening_leap_days
356 + (yday - tp->tm_yday));
357 return (60 * (60 * (24 * days + (hour - tp->tm_hour))
358 + (min - tp->tm_min))
359 + (sec - tp->tm_sec));
363 static time_t localtime_offset;
365 /* Convert *TP to a time_t value. */
371 /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
372 time zone names contained in the external variable `tzname' shall
373 be set as if the tzset() function had been called. */
377 return __mktime_internal (tp, localtime_r, &localtime_offset);
380 /* Convert *TP to a time_t value, inverting
381 the monotonic and mostly-unit-linear conversion function CONVERT.
382 Use *OFFSET to keep track of a guess at the offset of the result,
383 compared to what the result would be for UTC without leap seconds.
384 If *OFFSET's guess is correct, only one CONVERT call is needed. */
386 __mktime_internal (tp, convert, offset)
388 struct tm *(*convert) __P ((const time_t *, struct tm *));
394 /* The maximum number of probes (calls to CONVERT) should be enough
395 to handle any combinations of time zone rule changes, solar time,
396 and leap seconds. Posix.1 prohibits leap seconds, but some hosts
398 int remaining_probes = 4;
400 /* Time requested. Copy it in case CONVERT modifies *TP; this can
401 occur if TP is localtime's returned value and CONVERT is localtime. */
402 int sec = tp->tm_sec;
403 int min = tp->tm_min;
404 int hour = tp->tm_hour;
405 int mday = tp->tm_mday;
406 int mon = tp->tm_mon;
407 int year_requested = tp->tm_year;
408 int isdst = tp->tm_isdst;
410 /* Ensure that mon is in range, and set year accordingly. */
411 int mon_remainder = mon % 12;
412 int negative_mon_remainder = mon_remainder < 0;
413 int mon_years = mon / 12 - negative_mon_remainder;
414 int year = year_requested + mon_years;
416 /* The other values need not be in range:
417 the remaining code handles minor overflows correctly,
418 assuming int and time_t arithmetic wraps around.
419 Major overflows are caught at the end. */
421 /* Calculate day of year from year, month, and day of month.
422 The result need not be in range. */
423 int yday = ((__mon_yday[__isleap (year + TM_YEAR_BASE)]
424 [mon_remainder + 12 * negative_mon_remainder])
427 int sec_requested = sec;
428 #if LEAP_SECONDS_POSSIBLE
429 /* Handle out-of-range seconds specially,
430 since ydhms_tm_diff assumes every minute has 60 seconds. */
437 /* Invert CONVERT by probing. First assume the same offset as last time.
438 Then repeatedly use the error to improve the guess. */
440 tm.tm_year = EPOCH_YEAR - TM_YEAR_BASE;
441 tm.tm_yday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
442 t0 = ydhms_tm_diff (year, yday, hour, min, sec, &tm);
444 for (t = t0 + *offset;
445 (dt = ydhms_tm_diff (year, yday, hour, min, sec, (*convert) (&t, &tm)));
447 if (--remaining_probes == 0)
450 /* Check whether tm.tm_isdst has the requested value, if any. */
451 if (0 <= isdst && 0 <= tm.tm_isdst)
453 int dst_diff = (isdst != 0) - (tm.tm_isdst != 0);
456 /* Move two hours in the direction indicated by the disagreement,
457 probe some more, and switch to a new time if found.
458 The largest known fallback due to daylight savings is two hours:
459 once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */
460 time_t ot = t - 2 * 60 * 60 * dst_diff;
461 while (--remaining_probes != 0)
464 if (! (dt = ydhms_tm_diff (year, yday, hour, min, sec,
465 (*convert) (&ot, &otm))))
472 break; /* Avoid a redundant probe. */
479 #if LEAP_SECONDS_POSSIBLE
480 if (sec_requested != tm.tm_sec)
482 /* Adjust time to reflect the tm_sec requested, not the normalized value.
483 Also, repair any damage from a false match due to a leap second. */
484 t += sec_requested - sec + (sec == 0 && tm.tm_sec == 60);
485 (*convert) (&t, &tm);
489 if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3)
491 /* time_t isn't large enough to rule out overflows in ydhms_tm_diff,
492 so check for major overflows. A gross check suffices,
493 since if t has overflowed, it is off by a multiple of
494 TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of
495 the difference that is bounded by a small value. */
497 double dyear = (double) year_requested + mon_years - tm.tm_year;
498 double dday = 366 * dyear + mday;
499 double dsec = 60 * (60 * (24 * dday + hour) + min) + sec_requested;
501 if (TIME_T_MAX / 3 - TIME_T_MIN / 3 < (dsec < 0 ? - dsec : dsec))
510 weak_alias (mktime, timelocal)
512 #endif /* not HAVE_MKTIME */
515 #ifndef HAVE_STRPTIME
516 /* From GNU libc 2.1.3. */
517 /* Ulrich, thanks for helping me out with this! --hniksic */
519 /* strptime - Convert a string representation of time to a time value.
520 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
521 This file is part of the GNU C Library.
522 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. */
524 /* XXX This version of the implementation is not really complete.
525 Some of the fields cannot add information alone. But if seeing
526 some of them in the same format (such as year, week and weekday)
527 this is enough information for determining the date. */
530 # define __P(args) PARAMS (args)
533 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
535 # define localtime_r __localtime_r
537 /* Approximate localtime_r as best we can in its absence. */
538 # define localtime_r my_localtime_r
539 static struct tm *localtime_r __P ((const time_t *, struct tm *));
545 struct tm *l = localtime (t);
551 # endif /* ! _LIBC */
552 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
555 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
556 #if defined __GNUC__ && __GNUC__ >= 2
557 # define match_string(cs1, s2) \
558 ({ size_t len = strlen (cs1); \
559 int result = strncasecmp ((cs1), (s2), len) == 0; \
560 if (result) (s2) += len; \
563 /* Oh come on. Get a reasonable compiler. */
564 # define match_string(cs1, s2) \
565 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
567 /* We intentionally do not use isdigit() for testing because this will
568 lead to problems with the wide character version. */
569 #define get_number(from, to, n) \
575 if (*rp < '0' || *rp > '9') \
579 val += *rp++ - '0'; \
580 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
581 if (val < from || val > to) \
585 /* Added check for __GNUC__ extensions here for Wget. --abbotti */
586 # if defined __GNUC__ && __GNUC__ >= 2
587 # define get_alt_number(from, to, n) \
589 __label__ do_normal; \
590 if (*decided != raw) \
592 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
600 while (*alts != '\0') \
602 size_t len = strlen (alts); \
603 if (strncasecmp (alts, rp, len) == 0) \
610 if (*decided == not && ! any) \
612 /* If we haven't read anything it's an error. */ \
615 /* Correct the premature multiplication. */ \
621 } while (--__n > 0 && val * 10 <= to); \
622 if (val < from || val > to) \
628 get_number (from, to, n); \
633 # define get_alt_number(from, to, n) \
635 if (*decided != raw) \
637 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
645 while (*alts != '\0') \
647 size_t len = strlen (alts); \
648 if (strncasecmp (alts, rp, len) == 0) \
655 if (*decided == not && ! any) \
657 /* If we haven't read anything it's an error. */ \
660 /* Correct the premature multiplication. */ \
666 } while (--__n > 0 && val * 10 <= to); \
667 if (val < from || val > to) \
673 get_number (from, to, n); \
676 # endif /* defined __GNUC__ && __GNUC__ >= 2 */
678 # define get_alt_number(from, to, n) \
679 /* We don't have the alternate representation. */ \
680 get_number(from, to, n)
682 #define recursive(new_fmt) \
683 (*(new_fmt) != '\0' \
684 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
688 /* This is defined in locale/C-time.c in the GNU libc. */
689 extern const struct locale_data _nl_C_LC_TIME;
690 extern const unsigned short int __mon_yday[2][13];
692 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
693 # define ab_weekday_name \
694 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
695 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
696 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
697 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
698 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
699 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
700 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
701 # define HERE_T_FMT_AMPM \
702 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
703 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
705 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
707 static char const weekday_name[][10] =
709 "Sunday", "Monday", "Tuesday", "Wednesday",
710 "Thursday", "Friday", "Saturday"
712 static char const ab_weekday_name[][4] =
714 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
716 static char const month_name[][10] =
718 "January", "February", "March", "April", "May", "June",
719 "July", "August", "September", "October", "November", "December"
721 static char const ab_month_name[][4] =
723 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
724 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
726 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
727 # define HERE_D_FMT "%m/%d/%y"
728 # define HERE_AM_STR "AM"
729 # define HERE_PM_STR "PM"
730 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
731 # define HERE_T_FMT "%H:%M:%S"
733 /* __mon_yday[][] is common to mktime and strptime implementations.
735 const unsigned short int __mon_yday[2][13];
736 # ifndef NEED_MON_YDAY
737 # define NEED_MON_YDAY
741 /* Status of lookup: do we use the locale data or the raw data? */
742 enum locale_status { not, loc, raw };
746 /* Nonzero if YEAR is a leap year (every 4 years,
747 except every 100th isn't, and every 400th is). */
748 # define __isleap(year) \
749 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
752 /* Compute the day of the week. */
754 day_of_the_week (struct tm *tm)
756 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
757 the difference between this data in the one on TM and so determine
759 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
761 + (365 * (tm->tm_year - 70))
763 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
764 + (((corr_year / 4) / 25) / 4)
765 + __mon_yday[0][tm->tm_mon]
767 tm->tm_wday = ((wday % 7) + 7) % 7;
770 /* Compute the day of the year. */
772 day_of_the_year (struct tm *tm)
774 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
775 + (tm->tm_mday - 1));
782 strptime_internal __P ((const char *buf, const char *format, struct tm *tm,
783 enum locale_status *decided));
789 strptime_internal (rp, fmt, tm, decided)
793 enum locale_status *decided;
796 const char *rp_backup;
801 int century, want_century;
802 int have_wday, want_xday;
804 int have_mon, have_mday;
809 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
813 /* A white space in the format string matches 0 more or white
814 space in the input string. */
817 while (ISSPACE (*rp))
823 /* Any character but `%' must be matched by the same character
824 in the iput string. */
827 match_char (*fmt++, *rp++);
833 /* We need this for handling the `E' modifier. */
838 /* Make back up of current processing pointer. */
845 /* Match the `%' character itself. */
846 match_char ('%', *rp++);
850 /* Match day of week. */
851 for (cnt = 0; cnt < 7; ++cnt)
856 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
859 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
864 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
867 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
868 ab_weekday_name[cnt]))
875 && (match_string (weekday_name[cnt], rp)
876 || match_string (ab_weekday_name[cnt], rp)))
883 /* Does not match a weekday name. */
891 /* Match month name. */
892 for (cnt = 0; cnt < 12; ++cnt)
897 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
900 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
905 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
908 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
915 if (match_string (month_name[cnt], rp)
916 || match_string (ab_month_name[cnt], rp))
923 /* Does not match a month name. */
929 /* Match locale's date and time format. */
933 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
942 if (*decided == not &&
943 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
951 if (!recursive (HERE_D_T_FMT))
956 /* Match century number. */
957 get_number (0, 99, 2);
963 /* Match day of month. */
964 get_number (1, 31, 2);
970 if (!recursive ("%Y-%m-%d"))
978 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
988 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
998 /* Match standard day format. */
999 if (!recursive (HERE_D_FMT))
1005 /* Match hour in 24-hour clock. */
1006 get_number (0, 23, 2);
1011 /* Match hour in 12-hour clock. */
1012 get_number (1, 12, 2);
1013 tm->tm_hour = val % 12;
1017 /* Match day number of year. */
1018 get_number (1, 366, 3);
1019 tm->tm_yday = val - 1;
1023 /* Match number of month. */
1024 get_number (1, 12, 2);
1025 tm->tm_mon = val - 1;
1031 get_number (0, 59, 2);
1036 /* Match any white space. */
1037 while (ISSPACE (*rp))
1041 /* Match locale's equivalent of AM/PM. */
1043 if (*decided != raw)
1045 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
1047 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
1051 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
1053 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
1061 if (!match_string (HERE_AM_STR, rp))
1062 if (match_string (HERE_PM_STR, rp))
1069 if (*decided != raw)
1071 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
1073 if (*decided == loc)
1080 if (*decided == not &&
1081 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
1089 if (!recursive (HERE_T_FMT_AMPM))
1093 if (!recursive ("%H:%M"))
1098 /* The number of seconds may be very high so we cannot use
1099 the `get_number' macro. Instead read the number
1100 character for character and construct the result while
1103 if (*rp < '0' || *rp > '9')
1104 /* We need at least one digit. */
1110 secs += *rp++ - '0';
1112 while (*rp >= '0' && *rp <= '9');
1114 if (localtime_r (&secs, tm) == NULL)
1115 /* Error in function. */
1120 get_number (0, 61, 2);
1125 if (*decided != raw)
1127 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
1129 if (*decided == loc)
1136 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
1145 if (!recursive (HERE_T_FMT))
1149 get_number (1, 7, 1);
1150 tm->tm_wday = val % 7;
1154 get_number (0, 99, 2);
1155 /* XXX This cannot determine any field in TM. */
1158 if (*rp < '0' || *rp > '9')
1160 /* XXX Ignore the number since we would need some more
1161 information to compute a real date. */
1164 while (*rp >= '0' && *rp <= '9');
1169 get_number (0, 53, 2);
1170 /* XXX This cannot determine any field in TM without some
1174 /* Match number of weekday. */
1175 get_number (0, 6, 1);
1180 /* Match year within century. */
1181 get_number (0, 99, 2);
1182 /* The "Year 2000: The Millennium Rollover" paper suggests that
1183 values in the range 69-99 refer to the twentieth century. */
1184 tm->tm_year = val >= 69 ? val : val + 100;
1185 /* Indicate that we want to use the century, if specified. */
1190 /* Match year including century number. */
1191 get_number (0, 9999, 4);
1192 tm->tm_year = val - 1900;
1197 /* XXX How to handle this? */
1204 /* Match locale's alternate date and time format. */
1205 if (*decided != raw)
1207 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
1210 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
1212 if (!recursive (fmt))
1214 if (*decided == loc)
1221 if (strcmp (fmt, HERE_D_T_FMT))
1228 /* The C locale has no era information, so use the
1229 normal representation. */
1230 if (!recursive (HERE_D_T_FMT))
1237 /* Match name of base year in locale's alternate
1239 /* XXX This is currently not implemented. It should
1240 use the value _NL_CURRENT (LC_TIME, ERA). */
1243 if (*decided != raw)
1245 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
1248 fmt = _NL_CURRENT (LC_TIME, D_FMT);
1250 if (!recursive (fmt))
1252 if (*decided == loc)
1259 if (strcmp (fmt, HERE_D_FMT))
1265 if (!recursive (HERE_D_FMT))
1269 if (*decided != raw)
1271 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
1274 fmt = _NL_CURRENT (LC_TIME, T_FMT);
1276 if (!recursive (fmt))
1278 if (*decided == loc)
1285 if (strcmp (fmt, HERE_T_FMT))
1291 if (!recursive (HERE_T_FMT))
1299 /* We have no information about the era format. Just use
1300 the normal format. */
1301 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1302 && *fmt != 'x' && *fmt != 'X')
1303 /* This is an illegal format. */
1313 /* Match day of month using alternate numeric symbols. */
1314 get_alt_number (1, 31, 2);
1320 /* Match hour in 24-hour clock using alternate numeric
1322 get_alt_number (0, 23, 2);
1327 /* Match hour in 12-hour clock using alternate numeric
1329 get_alt_number (1, 12, 2);
1330 tm->tm_hour = val - 1;
1334 /* Match month using alternate numeric symbols. */
1335 get_alt_number (1, 12, 2);
1336 tm->tm_mon = val - 1;
1341 /* Match minutes using alternate numeric symbols. */
1342 get_alt_number (0, 59, 2);
1346 /* Match seconds using alternate numeric symbols. */
1347 get_alt_number (0, 61, 2);
1353 get_alt_number (0, 53, 2);
1354 /* XXX This cannot determine any field in TM without
1355 further information. */
1358 /* Match number of weekday using alternate numeric symbols. */
1359 get_alt_number (0, 6, 1);
1364 /* Match year within century using alternate numeric symbols. */
1365 get_alt_number (0, 99, 2);
1366 tm->tm_year = val >= 69 ? val : val + 100;
1378 if (have_I && is_pm)
1384 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1386 /* Only the century, but not the year. Strange, but so be it. */
1387 tm->tm_year = (century - 19) * 100;
1390 if (want_xday && !have_wday) {
1391 if ( !(have_mon && have_mday) && have_yday) {
1392 /* we don't have tm_mon and/or tm_mday, compute them */
1394 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1397 tm->tm_mon = t_mon - 1;
1399 tm->tm_mday = tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1;
1401 day_of_the_week (tm);
1403 if (want_xday && !have_yday)
1404 day_of_the_year (tm);
1411 strptime (buf, format, tm)
1416 enum locale_status decided;
1422 return strptime_internal (buf, format, tm, &decided);
1424 #endif /* not HAVE_STRPTIME */
1426 #ifdef NEED_MON_YDAY
1427 /* __mon_yday[][] is common to mktime and strptime implementations.
1429 const unsigned short int __mon_yday[2][13] =
1432 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
1434 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
1438 #ifndef HAVE_MEMMOVE
1440 memmove (char *dest, const char *source, unsigned length)
1444 /* Moving from low mem to hi mem; start at end. */
1445 for (source += length, dest += length; length; --length)
1446 *--dest = *--source;
1447 else if (source != dest)
1449 /* Moving from hi mem to low mem; start at beginning. */
1450 for (; length; --length)
1451 *dest++ = *source++;
1455 #endif /* not HAVE_MEMMOVE */
1457 /* fnmatch is a POSIX function, but we include an implementation for
1458 the sake of systems that don't have it. Furthermore, according to
1459 anecdotal evidence, historical implementations of fnmatch are buggy
1460 and unreliable. So we use our version, except when compiling under
1461 systems where fnmatch is known to work (currently glibc.) */
1463 #ifndef SYSTEM_FNMATCH
1465 #define __FNM_FLAGS (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD)
1467 /* Match STRING against the filename pattern PATTERN, returning zero
1468 if it matches, FNM_NOMATCH if not. This implementation comes from
1469 an earlier version of GNU Bash. (It doesn't make sense to update
1470 it with a newer version because it adds a lot of features Wget
1471 doesn't use or care about.) */
1474 fnmatch (const char *pattern, const char *string, int flags)
1476 register const char *p = pattern, *n = string;
1479 if ((flags & ~__FNM_FLAGS) != 0)
1485 while ((c = *p++) != '\0')
1491 return (FNM_NOMATCH);
1492 else if ((flags & FNM_PATHNAME) && *n == '/')
1493 return (FNM_NOMATCH);
1494 else if ((flags & FNM_PERIOD) && *n == '.' &&
1495 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1496 return (FNM_NOMATCH);
1500 if (!(flags & FNM_NOESCAPE))
1503 return (FNM_NOMATCH);
1507 if ((flags & FNM_PERIOD) && *n == '.' &&
1508 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1509 return (FNM_NOMATCH);
1511 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
1512 if (((flags & FNM_PATHNAME) && *n == '/') ||
1513 (c == '?' && *n == '\0'))
1514 return (FNM_NOMATCH);
1520 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
1521 for (--p; *n != '\0'; ++n)
1522 if ((c == '[' || *n == c1) &&
1523 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
1525 return (FNM_NOMATCH);
1530 /* Nonzero if the sense of the character class is
1535 return (FNM_NOMATCH);
1537 if ((flags & FNM_PERIOD) && *n == '.' &&
1538 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1539 return (FNM_NOMATCH);
1541 /* Make sure there is a closing `]'. If there isn't,
1542 the `[' is just a character to be matched. */
1544 register const char *np;
1546 for (np = p; np && *np && *np != ']'; np++);
1551 return (FNM_NOMATCH);
1556 not = (*p == '!' || *p == '^');
1563 register char cstart = c, cend = c;
1565 if (!(flags & FNM_NOESCAPE) && c == '\\')
1566 cstart = cend = *p++;
1569 /* [ (unterminated) loses. */
1570 return (FNM_NOMATCH);
1574 if ((flags & FNM_PATHNAME) && c == '/')
1575 /* [/] can never match. */
1576 return (FNM_NOMATCH);
1578 if (c == '-' && *p != ']')
1581 if (!(flags & FNM_NOESCAPE) && cend == '\\')
1584 return (FNM_NOMATCH);
1588 if (*n >= cstart && *n <= cend)
1595 return (FNM_NOMATCH);
1601 /* Skip the rest of the [...] that already matched. */
1605 /* [... (unterminated) loses. */
1606 return (FNM_NOMATCH);
1609 if (!(flags & FNM_NOESCAPE) && c == '\\')
1610 /* 1003.2d11 is unclear if this is right. %%% */
1614 return (FNM_NOMATCH);
1620 return (FNM_NOMATCH);
1629 return (FNM_NOMATCH);
1632 #endif /* not SYSTEM_FNMATCH */