1 /* Replacements for routines missing on some systems.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
44 /* Some systems lack certain functions normally taken for granted.
45 For example, Windows doesn't have strptime, and some systems don't
46 have a usable fnmatch. This file should contain fallback
47 implementations of such missing functions. It should *not* define
48 new Wget-specific interfaces -- those should be placed in utils.c
51 /* strcasecmp and strncasecmp apparently originated with BSD 4.4.
52 SUSv3 seems to be the only standard out there (that I can find)
53 that requires their existence, so in theory there might be systems
54 still in use that lack them. Note that these don't get defined
55 under Windows because mswindows.h defines them to the equivalent
56 Windows functions stricmp and strnicmp. */
58 #ifndef HAVE_STRCASECMP
60 /* Compare S1 and S2, ignoring case, returning less than, equal to or
61 greater than zero if S1 is lexiographically less than,
62 equal to or greater than S2. */
64 strcasecmp (const char *s1, const char *s2)
66 register const unsigned char *p1 = (const unsigned char *) s1;
67 register const unsigned char *p2 = (const unsigned char *) s2;
75 c1 = c_tolower (*p1++);
76 c2 = c_tolower (*p2++);
84 #endif /* not HAVE_STRCASECMP */
86 #ifndef HAVE_STRNCASECMP
88 /* Compare no more than N characters of S1 and S2,
89 ignoring case, returning less than, equal to or
90 greater than zero if S1 is lexicographically less
91 than, equal to or greater than S2. */
93 strncasecmp (const char *s1, const char *s2, size_t n)
95 register const unsigned char *p1 = (const unsigned char *) s1;
96 register const unsigned char *p2 = (const unsigned char *) s2;
99 if (p1 == p2 || n == 0)
104 c1 = c_tolower (*p1++);
105 c2 = c_tolower (*p2++);
106 if (c1 == '\0' || c1 != c2)
112 #endif /* not HAVE_STRNCASECMP */
115 /* memrchr is a GNU extension. It is like the memchr function, except
116 that it searches backwards from the end of the n bytes pointed to
117 by s instead of forwards from the front. */
120 memrchr (const void *s, int c, size_t n)
123 const char *e = b + n;
131 /* strptime is required by POSIX, but it is missing from Windows,
132 which means we must keep a fallback implementation. It is
133 reportedly missing or broken on many older Unix systems as well, so
134 it's good to have around. */
136 #ifndef HAVE_STRPTIME
137 /* From GNU libc 2.1.3. */
138 /* Ulrich, thanks for helping me out with this! --hniksic */
140 /* strptime - Convert a string representation of time to a time value.
141 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
142 This file is part of the GNU C Library.
143 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. */
145 /* XXX This version of the implementation is not really complete.
146 Some of the fields cannot add information alone. But if seeing
147 some of them in the same format (such as year, week and weekday)
148 this is enough information for determining the date. */
151 # define __P(args) args
154 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
156 # define localtime_r __localtime_r
158 /* Approximate localtime_r as best we can in its absence. */
159 # define localtime_r my_localtime_r
160 static struct tm *localtime_r __P ((const time_t *, struct tm *));
166 struct tm *l = localtime (t);
172 # endif /* ! _LIBC */
173 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
176 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
177 #if defined __GNUC__ && __GNUC__ >= 2
178 # define match_string(cs1, s2) \
179 ({ size_t len = strlen (cs1); \
180 int result = strncasecmp ((cs1), (s2), len) == 0; \
181 if (result) (s2) += len; \
184 /* Oh come on. Get a reasonable compiler. */
185 # define match_string(cs1, s2) \
186 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
188 /* We intentionally do not use isdigit() for testing because this will
189 lead to problems with the wide character version. */
190 #define get_number(from, to, n) \
196 if (*rp < '0' || *rp > '9') \
200 val += *rp++ - '0'; \
201 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
202 if (val < from || val > to) \
206 /* Added check for __GNUC__ extensions here for Wget. --abbotti */
207 # if defined __GNUC__ && __GNUC__ >= 2
208 # define get_alt_number(from, to, n) \
210 __label__ do_normal; \
211 if (*decided != raw) \
213 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
221 while (*alts != '\0') \
223 size_t len = strlen (alts); \
224 if (strncasecmp (alts, rp, len) == 0) \
231 if (*decided == not && ! any) \
233 /* If we haven't read anything it's an error. */ \
236 /* Correct the premature multiplication. */ \
242 } while (--__n > 0 && val * 10 <= to); \
243 if (val < from || val > to) \
249 get_number (from, to, n); \
254 # define get_alt_number(from, to, n) \
256 if (*decided != raw) \
258 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
266 while (*alts != '\0') \
268 size_t len = strlen (alts); \
269 if (strncasecmp (alts, rp, len) == 0) \
276 if (*decided == not && ! any) \
278 /* If we haven't read anything it's an error. */ \
281 /* Correct the premature multiplication. */ \
287 } while (--__n > 0 && val * 10 <= to); \
288 if (val < from || val > to) \
294 get_number (from, to, n); \
297 # endif /* defined __GNUC__ && __GNUC__ >= 2 */
299 # define get_alt_number(from, to, n) \
300 /* We don't have the alternate representation. */ \
301 get_number(from, to, n)
303 #define recursive(new_fmt) \
304 (*(new_fmt) != '\0' \
305 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
309 /* This is defined in locale/C-time.c in the GNU libc. */
310 extern const struct locale_data _nl_C_LC_TIME;
311 extern const unsigned short int __mon_yday[2][13];
313 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
314 # define ab_weekday_name \
315 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
316 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
317 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
318 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
319 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
320 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
321 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
322 # define HERE_T_FMT_AMPM \
323 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
324 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
326 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
328 static char const weekday_name[][10] =
330 "Sunday", "Monday", "Tuesday", "Wednesday",
331 "Thursday", "Friday", "Saturday"
333 static char const ab_weekday_name[][4] =
335 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
337 static char const month_name[][10] =
339 "January", "February", "March", "April", "May", "June",
340 "July", "August", "September", "October", "November", "December"
342 static char const ab_month_name[][4] =
344 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
345 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
347 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
348 # define HERE_D_FMT "%m/%d/%y"
349 # define HERE_AM_STR "AM"
350 # define HERE_PM_STR "PM"
351 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
352 # define HERE_T_FMT "%H:%M:%S"
354 const unsigned short int __mon_yday[2][13];
355 # ifndef NEED_MON_YDAY
356 # define NEED_MON_YDAY
360 /* Status of lookup: do we use the locale data or the raw data? */
361 enum locale_status { not, loc, raw };
365 /* Nonzero if YEAR is a leap year (every 4 years,
366 except every 100th isn't, and every 400th is). */
367 # define __isleap(year) \
368 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
371 /* Compute the day of the week. */
373 day_of_the_week (struct tm *tm)
375 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
376 the difference between this data in the one on TM and so determine
378 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
380 + (365 * (tm->tm_year - 70))
382 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
383 + (((corr_year / 4) / 25) / 4)
384 + __mon_yday[0][tm->tm_mon]
386 tm->tm_wday = ((wday % 7) + 7) % 7;
389 /* Compute the day of the year. */
391 day_of_the_year (struct tm *tm)
393 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
394 + (tm->tm_mday - 1));
401 strptime_internal __P ((const char *buf, const char *format, struct tm *tm,
402 enum locale_status *decided));
408 strptime_internal (rp, fmt, tm, decided)
412 enum locale_status *decided;
415 const char *rp_backup;
420 int century, want_century;
421 int have_wday, want_xday;
423 int have_mon, have_mday;
428 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
432 /* A white space in the format string matches 0 more or white
433 space in the input string. */
434 if (c_isspace (*fmt))
436 while (c_isspace (*rp))
442 /* Any character but `%' must be matched by the same character
443 in the iput string. */
446 match_char (*fmt++, *rp++);
452 /* We need this for handling the `E' modifier. */
457 /* Make back up of current processing pointer. */
464 /* Match the `%' character itself. */
465 match_char ('%', *rp++);
469 /* Match day of week. */
470 for (cnt = 0; cnt < 7; ++cnt)
475 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
478 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
483 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
486 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
487 ab_weekday_name[cnt]))
494 && (match_string (weekday_name[cnt], rp)
495 || match_string (ab_weekday_name[cnt], rp)))
502 /* Does not match a weekday name. */
510 /* Match month name. */
511 for (cnt = 0; cnt < 12; ++cnt)
516 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
519 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
524 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
527 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
534 if (match_string (month_name[cnt], rp)
535 || match_string (ab_month_name[cnt], rp))
542 /* Does not match a month name. */
548 /* Match locale's date and time format. */
552 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
561 if (*decided == not &&
562 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
570 if (!recursive (HERE_D_T_FMT))
575 /* Match century number. */
576 get_number (0, 99, 2);
582 /* Match day of month. */
583 get_number (1, 31, 2);
589 if (!recursive ("%Y-%m-%d"))
597 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
607 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
617 /* Match standard day format. */
618 if (!recursive (HERE_D_FMT))
624 /* Match hour in 24-hour clock. */
625 get_number (0, 23, 2);
630 /* Match hour in 12-hour clock. */
631 get_number (1, 12, 2);
632 tm->tm_hour = val % 12;
636 /* Match day number of year. */
637 get_number (1, 366, 3);
638 tm->tm_yday = val - 1;
642 /* Match number of month. */
643 get_number (1, 12, 2);
644 tm->tm_mon = val - 1;
650 get_number (0, 59, 2);
655 /* Match any white space. */
656 while (c_isspace (*rp))
660 /* Match locale's equivalent of AM/PM. */
664 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
666 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
670 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
672 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
680 if (!match_string (HERE_AM_STR, rp))
682 if (match_string (HERE_PM_STR, rp))
692 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
701 if (*decided == not &&
702 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
710 if (!recursive (HERE_T_FMT_AMPM))
714 if (!recursive ("%H:%M"))
719 /* The number of seconds may be very high so we cannot use
720 the `get_number' macro. Instead read the number
721 character for character and construct the result while
724 if (*rp < '0' || *rp > '9')
725 /* We need at least one digit. */
733 while (*rp >= '0' && *rp <= '9');
735 if (localtime_r (&secs, tm) == NULL)
736 /* Error in function. */
741 get_number (0, 61, 2);
748 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
757 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
766 if (!recursive (HERE_T_FMT))
770 get_number (1, 7, 1);
771 tm->tm_wday = val % 7;
775 get_number (0, 99, 2);
776 /* XXX This cannot determine any field in TM. */
779 if (*rp < '0' || *rp > '9')
781 /* XXX Ignore the number since we would need some more
782 information to compute a real date. */
785 while (*rp >= '0' && *rp <= '9');
790 get_number (0, 53, 2);
791 /* XXX This cannot determine any field in TM without some
795 /* Match number of weekday. */
796 get_number (0, 6, 1);
801 /* Match year within century. */
802 get_number (0, 99, 2);
803 /* The "Year 2000: The Millennium Rollover" paper suggests that
804 values in the range 69-99 refer to the twentieth century. */
805 tm->tm_year = val >= 69 ? val : val + 100;
806 /* Indicate that we want to use the century, if specified. */
811 /* Match year including century number. */
812 get_number (0, 9999, 4);
813 tm->tm_year = val - 1900;
818 /* XXX How to handle this? */
825 /* Match locale's alternate date and time format. */
828 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
831 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
833 if (!recursive (fmt))
842 if (strcmp (fmt, HERE_D_T_FMT))
849 /* The C locale has no era information, so use the
850 normal representation. */
851 if (!recursive (HERE_D_T_FMT))
858 /* Match name of base year in locale's alternate
860 /* XXX This is currently not implemented. It should
861 use the value _NL_CURRENT (LC_TIME, ERA). */
866 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
869 fmt = _NL_CURRENT (LC_TIME, D_FMT);
871 if (!recursive (fmt))
880 if (strcmp (fmt, HERE_D_FMT))
886 if (!recursive (HERE_D_FMT))
892 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
895 fmt = _NL_CURRENT (LC_TIME, T_FMT);
897 if (!recursive (fmt))
906 if (strcmp (fmt, HERE_T_FMT))
912 if (!recursive (HERE_T_FMT))
920 /* We have no information about the era format. Just use
921 the normal format. */
922 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
923 && *fmt != 'x' && *fmt != 'X')
924 /* This is an illegal format. */
934 /* Match day of month using alternate numeric symbols. */
935 get_alt_number (1, 31, 2);
941 /* Match hour in 24-hour clock using alternate numeric
943 get_alt_number (0, 23, 2);
948 /* Match hour in 12-hour clock using alternate numeric
950 get_alt_number (1, 12, 2);
951 tm->tm_hour = val - 1;
955 /* Match month using alternate numeric symbols. */
956 get_alt_number (1, 12, 2);
957 tm->tm_mon = val - 1;
962 /* Match minutes using alternate numeric symbols. */
963 get_alt_number (0, 59, 2);
967 /* Match seconds using alternate numeric symbols. */
968 get_alt_number (0, 61, 2);
974 get_alt_number (0, 53, 2);
975 /* XXX This cannot determine any field in TM without
976 further information. */
979 /* Match number of weekday using alternate numeric symbols. */
980 get_alt_number (0, 6, 1);
985 /* Match year within century using alternate numeric symbols. */
986 get_alt_number (0, 99, 2);
987 tm->tm_year = val >= 69 ? val : val + 100;
1005 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1007 /* Only the century, but not the year. Strange, but so be it. */
1008 tm->tm_year = (century - 19) * 100;
1011 if (want_xday && !have_wday) {
1012 if ( !(have_mon && have_mday) && have_yday) {
1013 /* we don't have tm_mon and/or tm_mday, compute them */
1015 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1018 tm->tm_mon = t_mon - 1;
1020 tm->tm_mday = tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1;
1022 day_of_the_week (tm);
1024 if (want_xday && !have_yday)
1025 day_of_the_year (tm);
1032 strptime (buf, format, tm)
1037 enum locale_status decided;
1043 return strptime_internal (buf, format, tm, &decided);
1045 #endif /* not HAVE_STRPTIME */
1047 #ifdef NEED_MON_YDAY
1048 const unsigned short int __mon_yday[2][13] =
1051 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
1053 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
1057 /* fnmatch is required by POSIX, but we include an implementation for
1058 the sake of systems that don't have it, most notably Windows. Some
1059 systems do have fnmatch, but Apache's installation process installs
1060 its own fnmatch.h (incompatible with the system one!) in a system
1061 include directory, effectively rendering fnmatch unusable. This
1062 has been fixed with Apache 2, where fnmatch has been moved to apr
1063 and given a prefix, but many systems out there are still (as of
1064 this writing in 2005) broken and we must cater to them.
1066 Additionally, according to some conventional wisdom, many
1067 historical implementations of fnmatch are buggy and unreliable. If
1068 yours is such, undefine SYSTEM_FNMATCH in sysdep.h and tell us
1071 #ifndef SYSTEM_FNMATCH
1073 #define __FNM_FLAGS (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD)
1075 /* Match STRING against the filename pattern PATTERN, returning zero
1076 if it matches, FNM_NOMATCH if not. This implementation comes from
1077 an earlier version of GNU Bash. (It doesn't make sense to update
1078 it with a newer version because those versions add a lot of
1079 features Wget doesn't use or care about.) */
1082 fnmatch (const char *pattern, const char *string, int flags)
1084 register const char *p = pattern, *n = string;
1087 if ((flags & ~__FNM_FLAGS) != 0)
1093 while ((c = *p++) != '\0')
1099 return (FNM_NOMATCH);
1100 else if ((flags & FNM_PATHNAME) && *n == '/')
1101 return (FNM_NOMATCH);
1102 else if ((flags & FNM_PERIOD) && *n == '.' &&
1103 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1104 return (FNM_NOMATCH);
1108 if (!(flags & FNM_NOESCAPE))
1111 return (FNM_NOMATCH);
1115 if ((flags & FNM_PERIOD) && *n == '.' &&
1116 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1117 return (FNM_NOMATCH);
1119 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
1120 if (((flags & FNM_PATHNAME) && *n == '/') ||
1121 (c == '?' && *n == '\0'))
1122 return (FNM_NOMATCH);
1128 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
1129 for (--p; *n != '\0'; ++n)
1130 if ((c == '[' || *n == c1) &&
1131 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
1133 return (FNM_NOMATCH);
1138 /* Nonzero if the sense of the character class is
1143 return (FNM_NOMATCH);
1145 if ((flags & FNM_PERIOD) && *n == '.' &&
1146 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1147 return (FNM_NOMATCH);
1149 /* Make sure there is a closing `]'. If there isn't,
1150 the `[' is just a character to be matched. */
1152 register const char *np;
1154 for (np = p; np && *np && *np != ']'; np++)
1160 return (FNM_NOMATCH);
1165 not = (*p == '!' || *p == '^');
1172 register char cstart = c, cend = c;
1174 if (!(flags & FNM_NOESCAPE) && c == '\\')
1175 cstart = cend = *p++;
1178 /* [ (unterminated) loses. */
1179 return (FNM_NOMATCH);
1183 if ((flags & FNM_PATHNAME) && c == '/')
1184 /* [/] can never match. */
1185 return (FNM_NOMATCH);
1187 if (c == '-' && *p != ']')
1190 if (!(flags & FNM_NOESCAPE) && cend == '\\')
1193 return (FNM_NOMATCH);
1197 if (*n >= cstart && *n <= cend)
1204 return (FNM_NOMATCH);
1210 /* Skip the rest of the [...] that already matched. */
1214 /* [... (unterminated) loses. */
1215 return (FNM_NOMATCH);
1218 if (!(flags & FNM_NOESCAPE) && c == '\\')
1219 /* 1003.2d11 is unclear if this is right. %%% */
1223 return (FNM_NOMATCH);
1229 return (FNM_NOMATCH);
1238 return (FNM_NOMATCH);
1241 #endif /* not SYSTEM_FNMATCH */
1244 /* timegm is a GNU extension, but lately also available on *BSD
1245 systems and possibly elsewhere. */
1247 /* True if YEAR is a leap year. */
1248 #define ISLEAP(year) \
1249 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
1251 /* Number of leap years in the range [y1, y2). */
1252 #define LEAPYEARS(y1, y2) \
1253 ((y2-1)/4 - (y1-1)/4) - ((y2-1)/100 - (y1-1)/100) + ((y2-1)/400 - (y1-1)/400)
1255 /* Inverse of gmtime: converts struct tm to time_t, assuming the data
1256 in tm is UTC rather than local timezone. This implementation
1257 returns the number of seconds elapsed since midnight 1970-01-01,
1258 converted to time_t. */
1261 timegm (struct tm *t)
1263 static const unsigned short int month_to_days[][13] = {
1264 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, /* normal */
1265 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } /* leap */
1267 const int year = 1900 + t->tm_year;
1268 unsigned long secs; /* until 2106-02-07 for 32-bit unsigned long */
1274 days = 365 * (year - 1970);
1275 /* Take into account leap years between 1970 and YEAR, not counting
1277 days += LEAPYEARS (1970, year);
1278 if (t->tm_mon < 0 || t->tm_mon >= 12)
1280 days += month_to_days[ISLEAP (year)][t->tm_mon];
1281 days += t->tm_mday - 1;
1283 secs = days * 86400 + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
1284 return (time_t) secs;
1286 #endif /* HAVE_TIMEGM */
1289 /* strtoll is required by C99 and used by Wget only on systems with
1290 LFS. Unfortunately, some systems have LFS, but no strtoll or
1291 equivalent. These include HPUX 11.0 and Windows.
1293 We use #ifdef NEED_STRTOLL instead of #ifndef HAVE_STRTOLL because
1294 of the systems which have a suitable replacement (e.g. _strtoi64 on
1295 Windows), on which Wget's str_to_wgint is instructed to use that
1299 char_value (char c, int base)
1304 if ('0' <= c && c <= '9')
1306 else if ('a' <= c && c <= 'z')
1307 value = c - 'a' + 10;
1308 else if ('A' <= c && c <= 'Z')
1309 value = c - 'A' + 10;
1317 #define STRTOLL_MAX TYPE_MAXIMUM (strtoll_type)
1318 /* This definition assumes two's complement arithmetic */
1319 #define STRTOLL_MIN (-STRTOLL_MAX - 1)
1321 /* Like a%b, but always returns a positive number when A is negative.
1322 (C doesn't guarantee the sign of the result.) */
1323 #define MOD(a, b) ((strtoll_type) -1 % 2 == 1 ? (a) % (b) : - ((a) % (b)))
1325 /* A strtoll-like replacement for systems that have an integral type
1326 larger than long but don't supply strtoll. This implementation
1327 makes no assumptions about the size of strtoll_type. */
1330 strtoll (const char *nptr, char **endptr, int base)
1332 strtoll_type result = 0;
1335 if (base != 0 && (base < 2 || base > 36))
1341 while (*nptr == ' ' || *nptr == '\t')
1348 else if (*nptr == '+')
1356 /* If BASE is 0, determine the real base based on the beginning on
1357 the number; octal numbers begin with "0", hexadecimal with "0x",
1358 and the others are considered octal. */
1361 if ((base == 0 || base == 16)
1363 (*(nptr + 1) == 'x' || *(nptr + 1) == 'X'))
1367 /* "0x" must be followed by at least one hex char. If not,
1368 return 0 and place ENDPTR on 'x'. */
1369 if (!c_isxdigit (*nptr))
1383 /* Parse positive number, checking for overflow. */
1385 /* Overflow watermark. If RESULT exceeds it, overflow occurs on
1386 this digit. If result==WATERMARK, current digit may not
1387 exceed the last digit of maximum value. */
1388 const strtoll_type WATERMARK = STRTOLL_MAX / base;
1389 for (; (digit = char_value (*nptr, base)) != -1; ++nptr)
1391 if (result > WATERMARK
1392 || (result == WATERMARK && digit > STRTOLL_MAX % base))
1394 result = STRTOLL_MAX;
1398 result = base * result + digit;
1403 /* Parse negative number, checking for underflow. */
1405 const strtoll_type WATERMARK = STRTOLL_MIN / base;
1406 for (; (digit = char_value (*nptr, base)) != -1; ++nptr)
1408 if (result < WATERMARK
1409 || (result == WATERMARK && digit > MOD (STRTOLL_MIN, base)))
1411 result = STRTOLL_MIN;
1415 result = base * result - digit;
1420 *endptr = (char *) nptr;
1428 #endif /* NEED_STRTOLL */