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;
795 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. */
835 /* Make back up of current processing pointer. */
841 /* Match the `%' character itself. */
842 match_char ('%', *rp++);
846 /* Match day of week. */
847 for (cnt = 0; cnt < 7; ++cnt)
852 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
855 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
860 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
863 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
864 ab_weekday_name[cnt]))
871 && (match_string (weekday_name[cnt], rp)
872 || match_string (ab_weekday_name[cnt], rp)))
879 /* Does not match a weekday name. */
887 /* Match month name. */
888 for (cnt = 0; cnt < 12; ++cnt)
893 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
896 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
901 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
904 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
911 if (match_string (month_name[cnt], rp)
912 || match_string (ab_month_name[cnt], rp))
919 /* Does not match a month name. */
925 /* Match locale's date and time format. */
929 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
938 if (*decided == not &&
939 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
947 if (!recursive (HERE_D_T_FMT))
952 /* Match century number. */
953 get_number (0, 99, 2);
959 /* Match day of month. */
960 get_number (1, 31, 2);
966 if (!recursive ("%Y-%m-%d"))
974 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
984 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
994 /* Match standard day format. */
995 if (!recursive (HERE_D_FMT))
1001 /* Match hour in 24-hour clock. */
1002 get_number (0, 23, 2);
1007 /* Match hour in 12-hour clock. */
1008 get_number (1, 12, 2);
1009 tm->tm_hour = val % 12;
1013 /* Match day number of year. */
1014 get_number (1, 366, 3);
1015 tm->tm_yday = val - 1;
1019 /* Match number of month. */
1020 get_number (1, 12, 2);
1021 tm->tm_mon = val - 1;
1027 get_number (0, 59, 2);
1032 /* Match any white space. */
1033 while (ISSPACE (*rp))
1037 /* Match locale's equivalent of AM/PM. */
1039 if (*decided != raw)
1041 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
1043 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
1047 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
1049 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
1057 if (!match_string (HERE_AM_STR, rp))
1058 if (match_string (HERE_PM_STR, rp))
1065 if (*decided != raw)
1067 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
1069 if (*decided == loc)
1076 if (*decided == not &&
1077 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
1085 if (!recursive (HERE_T_FMT_AMPM))
1089 if (!recursive ("%H:%M"))
1094 /* The number of seconds may be very high so we cannot use
1095 the `get_number' macro. Instead read the number
1096 character for character and construct the result while
1099 if (*rp < '0' || *rp > '9')
1100 /* We need at least one digit. */
1106 secs += *rp++ - '0';
1108 while (*rp >= '0' && *rp <= '9');
1110 if (localtime_r (&secs, tm) == NULL)
1111 /* Error in function. */
1116 get_number (0, 61, 2);
1121 if (*decided != raw)
1123 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
1125 if (*decided == loc)
1132 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
1141 if (!recursive (HERE_T_FMT))
1145 get_number (1, 7, 1);
1146 tm->tm_wday = val % 7;
1150 get_number (0, 99, 2);
1151 /* XXX This cannot determine any field in TM. */
1154 if (*rp < '0' || *rp > '9')
1156 /* XXX Ignore the number since we would need some more
1157 information to compute a real date. */
1160 while (*rp >= '0' && *rp <= '9');
1165 get_number (0, 53, 2);
1166 /* XXX This cannot determine any field in TM without some
1170 /* Match number of weekday. */
1171 get_number (0, 6, 1);
1176 /* Match year within century. */
1177 get_number (0, 99, 2);
1178 /* The "Year 2000: The Millennium Rollover" paper suggests that
1179 values in the range 69-99 refer to the twentieth century. */
1180 tm->tm_year = val >= 69 ? val : val + 100;
1181 /* Indicate that we want to use the century, if specified. */
1186 /* Match year including century number. */
1187 get_number (0, 9999, 4);
1188 tm->tm_year = val - 1900;
1193 /* XXX How to handle this? */
1200 /* Match locale's alternate date and time format. */
1201 if (*decided != raw)
1203 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
1206 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
1208 if (!recursive (fmt))
1210 if (*decided == loc)
1217 if (strcmp (fmt, HERE_D_T_FMT))
1224 /* The C locale has no era information, so use the
1225 normal representation. */
1226 if (!recursive (HERE_D_T_FMT))
1233 /* Match name of base year in locale's alternate
1235 /* XXX This is currently not implemented. It should
1236 use the value _NL_CURRENT (LC_TIME, ERA). */
1239 if (*decided != raw)
1241 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
1244 fmt = _NL_CURRENT (LC_TIME, D_FMT);
1246 if (!recursive (fmt))
1248 if (*decided == loc)
1255 if (strcmp (fmt, HERE_D_FMT))
1261 if (!recursive (HERE_D_FMT))
1265 if (*decided != raw)
1267 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
1270 fmt = _NL_CURRENT (LC_TIME, T_FMT);
1272 if (!recursive (fmt))
1274 if (*decided == loc)
1281 if (strcmp (fmt, HERE_T_FMT))
1287 if (!recursive (HERE_T_FMT))
1295 /* We have no information about the era format. Just use
1296 the normal format. */
1297 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
1298 && *fmt != 'x' && *fmt != 'X')
1299 /* This is an illegal format. */
1309 /* Match day of month using alternate numeric symbols. */
1310 get_alt_number (1, 31, 2);
1316 /* Match hour in 24-hour clock using alternate numeric
1318 get_alt_number (0, 23, 2);
1323 /* Match hour in 12-hour clock using alternate numeric
1325 get_alt_number (1, 12, 2);
1326 tm->tm_hour = val - 1;
1330 /* Match month using alternate numeric symbols. */
1331 get_alt_number (1, 12, 2);
1332 tm->tm_mon = val - 1;
1337 /* Match minutes using alternate numeric symbols. */
1338 get_alt_number (0, 59, 2);
1342 /* Match seconds using alternate numeric symbols. */
1343 get_alt_number (0, 61, 2);
1349 get_alt_number (0, 53, 2);
1350 /* XXX This cannot determine any field in TM without
1351 further information. */
1354 /* Match number of weekday using alternate numeric symbols. */
1355 get_alt_number (0, 6, 1);
1360 /* Match year within century using alternate numeric symbols. */
1361 get_alt_number (0, 99, 2);
1362 tm->tm_year = val >= 69 ? val : val + 100;
1374 if (have_I && is_pm)
1380 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1382 /* Only the century, but not the year. Strange, but so be it. */
1383 tm->tm_year = (century - 19) * 100;
1386 if (want_xday && !have_wday) {
1387 if ( !(have_mon && have_mday) && have_yday) {
1388 /* we don't have tm_mon and/or tm_mday, compute them */
1390 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1393 tm->tm_mon = t_mon - 1;
1395 tm->tm_mday = tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1;
1397 day_of_the_week (tm);
1399 if (want_xday && !have_yday)
1400 day_of_the_year (tm);
1407 strptime (buf, format, tm)
1412 enum locale_status decided;
1418 return strptime_internal (buf, format, tm, &decided);
1420 #endif /* not HAVE_STRPTIME */
1422 #ifdef NEED_MON_YDAY
1423 /* __mon_yday[][] is common to mktime and strptime implementations.
1425 const unsigned short int __mon_yday[2][13] =
1428 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
1430 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
1437 /* A simple usleep implementation based on select(). For Unix and
1438 Unix-like systems. */
1441 usleep (unsigned long usec)
1446 select (0, NULL, NULL, NULL, &tm);
1450 #endif /* not WINDOWS */
1451 #endif /* not HAVE_USLEEP */
1454 /* Currently unused in Wget. Uncomment if we start using memmove
1458 #ifndef HAVE_MEMMOVE
1460 memmove (char *dest, const char *source, unsigned length)
1464 /* Moving from low mem to hi mem; start at end. */
1465 for (source += length, dest += length; length; --length)
1466 *--dest = *--source;
1467 else if (source != dest)
1469 /* Moving from hi mem to low mem; start at beginning. */
1470 for (; length; --length)
1471 *dest++ = *source++;
1475 #endif /* not HAVE_MEMMOVE */
1479 /* fnmatch is a POSIX function, but we include an implementation for
1480 the sake of systems that don't have it. Furthermore, according to
1481 anecdotal evidence, historical implementations of fnmatch are buggy
1482 and unreliable. So we use our version, except when compiling under
1483 systems where fnmatch is known to work (currently glibc.) */
1485 #ifndef SYSTEM_FNMATCH
1487 #define __FNM_FLAGS (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD)
1489 /* Match STRING against the filename pattern PATTERN, returning zero
1490 if it matches, FNM_NOMATCH if not. This implementation comes from
1491 an earlier version of GNU Bash. (It doesn't make sense to update
1492 it with a newer version because it adds a lot of features Wget
1493 doesn't use or care about.) */
1496 fnmatch (const char *pattern, const char *string, int flags)
1498 register const char *p = pattern, *n = string;
1501 if ((flags & ~__FNM_FLAGS) != 0)
1507 while ((c = *p++) != '\0')
1513 return (FNM_NOMATCH);
1514 else if ((flags & FNM_PATHNAME) && *n == '/')
1515 return (FNM_NOMATCH);
1516 else if ((flags & FNM_PERIOD) && *n == '.' &&
1517 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1518 return (FNM_NOMATCH);
1522 if (!(flags & FNM_NOESCAPE))
1525 return (FNM_NOMATCH);
1529 if ((flags & FNM_PERIOD) && *n == '.' &&
1530 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1531 return (FNM_NOMATCH);
1533 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
1534 if (((flags & FNM_PATHNAME) && *n == '/') ||
1535 (c == '?' && *n == '\0'))
1536 return (FNM_NOMATCH);
1542 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
1543 for (--p; *n != '\0'; ++n)
1544 if ((c == '[' || *n == c1) &&
1545 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
1547 return (FNM_NOMATCH);
1552 /* Nonzero if the sense of the character class is
1557 return (FNM_NOMATCH);
1559 if ((flags & FNM_PERIOD) && *n == '.' &&
1560 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1561 return (FNM_NOMATCH);
1563 /* Make sure there is a closing `]'. If there isn't,
1564 the `[' is just a character to be matched. */
1566 register const char *np;
1568 for (np = p; np && *np && *np != ']'; np++);
1573 return (FNM_NOMATCH);
1578 not = (*p == '!' || *p == '^');
1585 register char cstart = c, cend = c;
1587 if (!(flags & FNM_NOESCAPE) && c == '\\')
1588 cstart = cend = *p++;
1591 /* [ (unterminated) loses. */
1592 return (FNM_NOMATCH);
1596 if ((flags & FNM_PATHNAME) && c == '/')
1597 /* [/] can never match. */
1598 return (FNM_NOMATCH);
1600 if (c == '-' && *p != ']')
1603 if (!(flags & FNM_NOESCAPE) && cend == '\\')
1606 return (FNM_NOMATCH);
1610 if (*n >= cstart && *n <= cend)
1617 return (FNM_NOMATCH);
1623 /* Skip the rest of the [...] that already matched. */
1627 /* [... (unterminated) loses. */
1628 return (FNM_NOMATCH);
1631 if (!(flags & FNM_NOESCAPE) && c == '\\')
1632 /* 1003.2d11 is unclear if this is right. %%% */
1636 return (FNM_NOMATCH);
1642 return (FNM_NOMATCH);
1651 return (FNM_NOMATCH);
1654 #endif /* not SYSTEM_FNMATCH */