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 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. */
46 /* Some systems lack certain functions normally taken for granted.
47 For example, Windows doesn't have strptime, and some systems don't
48 have a usable fnmatch. This file should contain fallback
49 implementations of such missing functions. It should *not* define
50 new Wget-specific interfaces -- those should be placed in utils.c
53 /* strcasecmp and strncasecmp apparently originated with BSD 4.4.
54 SUSv3 seems to be the only standard out there (that I can find)
55 that requires their existence, so in theory there might be systems
56 still in use that lack them. Note that these don't get defined
57 under Windows because mswindows.h defines them to the equivalent
58 Windows functions stricmp and strnicmp. */
60 #ifndef HAVE_STRCASECMP
62 /* Compare S1 and S2, ignoring case, returning less than, equal to or
63 greater than zero if S1 is lexiographically less than,
64 equal to or greater than S2. */
66 strcasecmp (const char *s1, const char *s2)
68 register const unsigned char *p1 = (const unsigned char *) s1;
69 register const unsigned char *p2 = (const unsigned char *) s2;
86 #endif /* not HAVE_STRCASECMP */
88 #ifndef HAVE_STRNCASECMP
90 /* Compare no more than N characters of S1 and S2,
91 ignoring case, returning less than, equal to or
92 greater than zero if S1 is lexicographically less
93 than, equal to or greater than S2. */
95 strncasecmp (const char *s1, const char *s2, size_t n)
97 register const unsigned char *p1 = (const unsigned char *) s1;
98 register const unsigned char *p2 = (const unsigned char *) s2;
101 if (p1 == p2 || n == 0)
106 c1 = TOLOWER (*p1++);
107 c2 = TOLOWER (*p2++);
108 if (c1 == '\0' || c1 != c2)
114 #endif /* not HAVE_STRNCASECMP */
117 /* memrchr is a GNU extension. It is like the memchr function, except
118 that it searches backwards from the end of the n bytes pointed to
119 by s instead of forwards from the front. */
122 memrchr (const void *s, int c, size_t n)
125 const char *e = b + n;
133 /* strptime is required by POSIX, but it is missing from Windows,
134 which means we must keep a fallback implementation. It is
135 reportedly missing or broken on many older Unix systems as well, so
136 it's good to have around. */
138 #ifndef HAVE_STRPTIME
139 /* From GNU libc 2.1.3. */
140 /* Ulrich, thanks for helping me out with this! --hniksic */
142 /* strptime - Convert a string representation of time to a time value.
143 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
144 This file is part of the GNU C Library.
145 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. */
147 /* XXX This version of the implementation is not really complete.
148 Some of the fields cannot add information alone. But if seeing
149 some of them in the same format (such as year, week and weekday)
150 this is enough information for determining the date. */
153 # define __P(args) args
156 #if ! HAVE_LOCALTIME_R && ! defined localtime_r
158 # define localtime_r __localtime_r
160 /* Approximate localtime_r as best we can in its absence. */
161 # define localtime_r my_localtime_r
162 static struct tm *localtime_r __P ((const time_t *, struct tm *));
168 struct tm *l = localtime (t);
174 # endif /* ! _LIBC */
175 #endif /* ! HAVE_LOCALTIME_R && ! defined (localtime_r) */
178 #define match_char(ch1, ch2) if (ch1 != ch2) return NULL
179 #if defined __GNUC__ && __GNUC__ >= 2
180 # define match_string(cs1, s2) \
181 ({ size_t len = strlen (cs1); \
182 int result = strncasecmp ((cs1), (s2), len) == 0; \
183 if (result) (s2) += len; \
186 /* Oh come on. Get a reasonable compiler. */
187 # define match_string(cs1, s2) \
188 (strncasecmp ((cs1), (s2), strlen (cs1)) ? 0 : ((s2) += strlen (cs1), 1))
190 /* We intentionally do not use isdigit() for testing because this will
191 lead to problems with the wide character version. */
192 #define get_number(from, to, n) \
198 if (*rp < '0' || *rp > '9') \
202 val += *rp++ - '0'; \
203 } while (--__n > 0 && val * 10 <= to && *rp >= '0' && *rp <= '9'); \
204 if (val < from || val > to) \
208 /* Added check for __GNUC__ extensions here for Wget. --abbotti */
209 # if defined __GNUC__ && __GNUC__ >= 2
210 # define get_alt_number(from, to, n) \
212 __label__ do_normal; \
213 if (*decided != raw) \
215 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
223 while (*alts != '\0') \
225 size_t len = strlen (alts); \
226 if (strncasecmp (alts, rp, len) == 0) \
233 if (*decided == not && ! any) \
235 /* If we haven't read anything it's an error. */ \
238 /* Correct the premature multiplication. */ \
244 } while (--__n > 0 && val * 10 <= to); \
245 if (val < from || val > to) \
251 get_number (from, to, n); \
256 # define get_alt_number(from, to, n) \
258 if (*decided != raw) \
260 const char *alts = _NL_CURRENT (LC_TIME, ALT_DIGITS); \
268 while (*alts != '\0') \
270 size_t len = strlen (alts); \
271 if (strncasecmp (alts, rp, len) == 0) \
278 if (*decided == not && ! any) \
280 /* If we haven't read anything it's an error. */ \
283 /* Correct the premature multiplication. */ \
289 } while (--__n > 0 && val * 10 <= to); \
290 if (val < from || val > to) \
296 get_number (from, to, n); \
299 # endif /* defined __GNUC__ && __GNUC__ >= 2 */
301 # define get_alt_number(from, to, n) \
302 /* We don't have the alternate representation. */ \
303 get_number(from, to, n)
305 #define recursive(new_fmt) \
306 (*(new_fmt) != '\0' \
307 && (rp = strptime_internal (rp, (new_fmt), tm, decided)) != NULL)
311 /* This is defined in locale/C-time.c in the GNU libc. */
312 extern const struct locale_data _nl_C_LC_TIME;
313 extern const unsigned short int __mon_yday[2][13];
315 # define weekday_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (DAY_1)].string)
316 # define ab_weekday_name \
317 (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)].string)
318 # define month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (MON_1)].string)
319 # define ab_month_name (&_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)].string)
320 # define HERE_D_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_T_FMT)].string)
321 # define HERE_D_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (D_FMT)].string)
322 # define HERE_AM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (AM_STR)].string)
323 # define HERE_PM_STR (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (PM_STR)].string)
324 # define HERE_T_FMT_AMPM \
325 (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT_AMPM)].string)
326 # define HERE_T_FMT (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (T_FMT)].string)
328 # define strncasecmp(s1, s2, n) __strncasecmp (s1, s2, n)
330 static char const weekday_name[][10] =
332 "Sunday", "Monday", "Tuesday", "Wednesday",
333 "Thursday", "Friday", "Saturday"
335 static char const ab_weekday_name[][4] =
337 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
339 static char const month_name[][10] =
341 "January", "February", "March", "April", "May", "June",
342 "July", "August", "September", "October", "November", "December"
344 static char const ab_month_name[][4] =
346 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
347 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
349 # define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
350 # define HERE_D_FMT "%m/%d/%y"
351 # define HERE_AM_STR "AM"
352 # define HERE_PM_STR "PM"
353 # define HERE_T_FMT_AMPM "%I:%M:%S %p"
354 # define HERE_T_FMT "%H:%M:%S"
356 const unsigned short int __mon_yday[2][13];
357 # ifndef NEED_MON_YDAY
358 # define NEED_MON_YDAY
362 /* Status of lookup: do we use the locale data or the raw data? */
363 enum locale_status { not, loc, raw };
367 /* Nonzero if YEAR is a leap year (every 4 years,
368 except every 100th isn't, and every 400th is). */
369 # define __isleap(year) \
370 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
373 /* Compute the day of the week. */
375 day_of_the_week (struct tm *tm)
377 /* We know that January 1st 1970 was a Thursday (= 4). Compute the
378 the difference between this data in the one on TM and so determine
380 int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
382 + (365 * (tm->tm_year - 70))
384 - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
385 + (((corr_year / 4) / 25) / 4)
386 + __mon_yday[0][tm->tm_mon]
388 tm->tm_wday = ((wday % 7) + 7) % 7;
391 /* Compute the day of the year. */
393 day_of_the_year (struct tm *tm)
395 tm->tm_yday = (__mon_yday[__isleap (1900 + tm->tm_year)][tm->tm_mon]
396 + (tm->tm_mday - 1));
403 strptime_internal __P ((const char *buf, const char *format, struct tm *tm,
404 enum locale_status *decided));
410 strptime_internal (rp, fmt, tm, decided)
414 enum locale_status *decided;
417 const char *rp_backup;
422 int century, want_century;
423 int have_wday, want_xday;
425 int have_mon, have_mday;
430 have_wday = want_xday = have_yday = have_mon = have_mday = 0;
434 /* A white space in the format string matches 0 more or white
435 space in the input string. */
438 while (ISSPACE (*rp))
444 /* Any character but `%' must be matched by the same character
445 in the iput string. */
448 match_char (*fmt++, *rp++);
454 /* We need this for handling the `E' modifier. */
459 /* Make back up of current processing pointer. */
466 /* Match the `%' character itself. */
467 match_char ('%', *rp++);
471 /* Match day of week. */
472 for (cnt = 0; cnt < 7; ++cnt)
477 if (match_string (_NL_CURRENT (LC_TIME, DAY_1 + cnt), rp))
480 && strcmp (_NL_CURRENT (LC_TIME, DAY_1 + cnt),
485 if (match_string (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt), rp))
488 && strcmp (_NL_CURRENT (LC_TIME, ABDAY_1 + cnt),
489 ab_weekday_name[cnt]))
496 && (match_string (weekday_name[cnt], rp)
497 || match_string (ab_weekday_name[cnt], rp)))
504 /* Does not match a weekday name. */
512 /* Match month name. */
513 for (cnt = 0; cnt < 12; ++cnt)
518 if (match_string (_NL_CURRENT (LC_TIME, MON_1 + cnt), rp))
521 && strcmp (_NL_CURRENT (LC_TIME, MON_1 + cnt),
526 if (match_string (_NL_CURRENT (LC_TIME, ABMON_1 + cnt), rp))
529 && strcmp (_NL_CURRENT (LC_TIME, ABMON_1 + cnt),
536 if (match_string (month_name[cnt], rp)
537 || match_string (ab_month_name[cnt], rp))
544 /* Does not match a month name. */
550 /* Match locale's date and time format. */
554 if (!recursive (_NL_CURRENT (LC_TIME, D_T_FMT)))
563 if (*decided == not &&
564 strcmp (_NL_CURRENT (LC_TIME, D_T_FMT), HERE_D_T_FMT))
572 if (!recursive (HERE_D_T_FMT))
577 /* Match century number. */
578 get_number (0, 99, 2);
584 /* Match day of month. */
585 get_number (1, 31, 2);
591 if (!recursive ("%Y-%m-%d"))
599 if (!recursive (_NL_CURRENT (LC_TIME, D_FMT)))
609 && strcmp (_NL_CURRENT (LC_TIME, D_FMT), HERE_D_FMT))
619 /* Match standard day format. */
620 if (!recursive (HERE_D_FMT))
626 /* Match hour in 24-hour clock. */
627 get_number (0, 23, 2);
632 /* Match hour in 12-hour clock. */
633 get_number (1, 12, 2);
634 tm->tm_hour = val % 12;
638 /* Match day number of year. */
639 get_number (1, 366, 3);
640 tm->tm_yday = val - 1;
644 /* Match number of month. */
645 get_number (1, 12, 2);
646 tm->tm_mon = val - 1;
652 get_number (0, 59, 2);
657 /* Match any white space. */
658 while (ISSPACE (*rp))
662 /* Match locale's equivalent of AM/PM. */
666 if (match_string (_NL_CURRENT (LC_TIME, AM_STR), rp))
668 if (strcmp (_NL_CURRENT (LC_TIME, AM_STR), HERE_AM_STR))
672 if (match_string (_NL_CURRENT (LC_TIME, PM_STR), rp))
674 if (strcmp (_NL_CURRENT (LC_TIME, PM_STR), HERE_PM_STR))
682 if (!match_string (HERE_AM_STR, rp))
684 if (match_string (HERE_PM_STR, rp))
694 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT_AMPM)))
703 if (*decided == not &&
704 strcmp (_NL_CURRENT (LC_TIME, T_FMT_AMPM),
712 if (!recursive (HERE_T_FMT_AMPM))
716 if (!recursive ("%H:%M"))
721 /* The number of seconds may be very high so we cannot use
722 the `get_number' macro. Instead read the number
723 character for character and construct the result while
726 if (*rp < '0' || *rp > '9')
727 /* We need at least one digit. */
735 while (*rp >= '0' && *rp <= '9');
737 if (localtime_r (&secs, tm) == NULL)
738 /* Error in function. */
743 get_number (0, 61, 2);
750 if (!recursive (_NL_CURRENT (LC_TIME, T_FMT)))
759 if (strcmp (_NL_CURRENT (LC_TIME, T_FMT), HERE_T_FMT))
768 if (!recursive (HERE_T_FMT))
772 get_number (1, 7, 1);
773 tm->tm_wday = val % 7;
777 get_number (0, 99, 2);
778 /* XXX This cannot determine any field in TM. */
781 if (*rp < '0' || *rp > '9')
783 /* XXX Ignore the number since we would need some more
784 information to compute a real date. */
787 while (*rp >= '0' && *rp <= '9');
792 get_number (0, 53, 2);
793 /* XXX This cannot determine any field in TM without some
797 /* Match number of weekday. */
798 get_number (0, 6, 1);
803 /* Match year within century. */
804 get_number (0, 99, 2);
805 /* The "Year 2000: The Millennium Rollover" paper suggests that
806 values in the range 69-99 refer to the twentieth century. */
807 tm->tm_year = val >= 69 ? val : val + 100;
808 /* Indicate that we want to use the century, if specified. */
813 /* Match year including century number. */
814 get_number (0, 9999, 4);
815 tm->tm_year = val - 1900;
820 /* XXX How to handle this? */
827 /* Match locale's alternate date and time format. */
830 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT);
833 fmt = _NL_CURRENT (LC_TIME, D_T_FMT);
835 if (!recursive (fmt))
844 if (strcmp (fmt, HERE_D_T_FMT))
851 /* The C locale has no era information, so use the
852 normal representation. */
853 if (!recursive (HERE_D_T_FMT))
860 /* Match name of base year in locale's alternate
862 /* XXX This is currently not implemented. It should
863 use the value _NL_CURRENT (LC_TIME, ERA). */
868 const char *fmt = _NL_CURRENT (LC_TIME, ERA_D_FMT);
871 fmt = _NL_CURRENT (LC_TIME, D_FMT);
873 if (!recursive (fmt))
882 if (strcmp (fmt, HERE_D_FMT))
888 if (!recursive (HERE_D_FMT))
894 const char *fmt = _NL_CURRENT (LC_TIME, ERA_T_FMT);
897 fmt = _NL_CURRENT (LC_TIME, T_FMT);
899 if (!recursive (fmt))
908 if (strcmp (fmt, HERE_T_FMT))
914 if (!recursive (HERE_T_FMT))
922 /* We have no information about the era format. Just use
923 the normal format. */
924 if (*fmt != 'c' && *fmt != 'C' && *fmt != 'y' && *fmt != 'Y'
925 && *fmt != 'x' && *fmt != 'X')
926 /* This is an illegal format. */
936 /* Match day of month using alternate numeric symbols. */
937 get_alt_number (1, 31, 2);
943 /* Match hour in 24-hour clock using alternate numeric
945 get_alt_number (0, 23, 2);
950 /* Match hour in 12-hour clock using alternate numeric
952 get_alt_number (1, 12, 2);
953 tm->tm_hour = val - 1;
957 /* Match month using alternate numeric symbols. */
958 get_alt_number (1, 12, 2);
959 tm->tm_mon = val - 1;
964 /* Match minutes using alternate numeric symbols. */
965 get_alt_number (0, 59, 2);
969 /* Match seconds using alternate numeric symbols. */
970 get_alt_number (0, 61, 2);
976 get_alt_number (0, 53, 2);
977 /* XXX This cannot determine any field in TM without
978 further information. */
981 /* Match number of weekday using alternate numeric symbols. */
982 get_alt_number (0, 6, 1);
987 /* Match year within century using alternate numeric symbols. */
988 get_alt_number (0, 99, 2);
989 tm->tm_year = val >= 69 ? val : val + 100;
1001 if (have_I && is_pm)
1007 tm->tm_year = tm->tm_year % 100 + (century - 19) * 100;
1009 /* Only the century, but not the year. Strange, but so be it. */
1010 tm->tm_year = (century - 19) * 100;
1013 if (want_xday && !have_wday) {
1014 if ( !(have_mon && have_mday) && have_yday) {
1015 /* we don't have tm_mon and/or tm_mday, compute them */
1017 while (__mon_yday[__isleap(1900 + tm->tm_year)][t_mon] <= tm->tm_yday)
1020 tm->tm_mon = t_mon - 1;
1022 tm->tm_mday = tm->tm_yday - __mon_yday[__isleap(1900 + tm->tm_year)][t_mon - 1] + 1;
1024 day_of_the_week (tm);
1026 if (want_xday && !have_yday)
1027 day_of_the_year (tm);
1034 strptime (buf, format, tm)
1039 enum locale_status decided;
1045 return strptime_internal (buf, format, tm, &decided);
1047 #endif /* not HAVE_STRPTIME */
1049 #ifdef NEED_MON_YDAY
1050 const unsigned short int __mon_yday[2][13] =
1053 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
1055 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
1059 /* fnmatch is required by POSIX, but we include an implementation for
1060 the sake of systems that don't have it, most notably Windows. Some
1061 systems do have fnmatch, but Apache's installation process installs
1062 its own fnmatch.h (incompatible with the system one!) in a system
1063 include directory, effectively rendering fnmatch unusable. This
1064 has been fixed with Apache 2, where fnmatch has been moved to apr
1065 and given a prefix, but many systems out there are still (as of
1066 this writing in 2005) broken and we must cater to them.
1068 Additionally, according to some conventional wisdom, many
1069 historical implementations of fnmatch are buggy and unreliable. If
1070 yours is such, undefine SYSTEM_FNMATCH in sysdep.h and tell us
1073 #ifndef SYSTEM_FNMATCH
1075 #define __FNM_FLAGS (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD)
1077 /* Match STRING against the filename pattern PATTERN, returning zero
1078 if it matches, FNM_NOMATCH if not. This implementation comes from
1079 an earlier version of GNU Bash. (It doesn't make sense to update
1080 it with a newer version because those versions add a lot of
1081 features Wget doesn't use or care about.) */
1084 fnmatch (const char *pattern, const char *string, int flags)
1086 register const char *p = pattern, *n = string;
1089 if ((flags & ~__FNM_FLAGS) != 0)
1095 while ((c = *p++) != '\0')
1101 return (FNM_NOMATCH);
1102 else if ((flags & FNM_PATHNAME) && *n == '/')
1103 return (FNM_NOMATCH);
1104 else if ((flags & FNM_PERIOD) && *n == '.' &&
1105 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1106 return (FNM_NOMATCH);
1110 if (!(flags & FNM_NOESCAPE))
1113 return (FNM_NOMATCH);
1117 if ((flags & FNM_PERIOD) && *n == '.' &&
1118 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1119 return (FNM_NOMATCH);
1121 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
1122 if (((flags & FNM_PATHNAME) && *n == '/') ||
1123 (c == '?' && *n == '\0'))
1124 return (FNM_NOMATCH);
1130 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
1131 for (--p; *n != '\0'; ++n)
1132 if ((c == '[' || *n == c1) &&
1133 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
1135 return (FNM_NOMATCH);
1140 /* Nonzero if the sense of the character class is
1145 return (FNM_NOMATCH);
1147 if ((flags & FNM_PERIOD) && *n == '.' &&
1148 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1149 return (FNM_NOMATCH);
1151 /* Make sure there is a closing `]'. If there isn't,
1152 the `[' is just a character to be matched. */
1154 register const char *np;
1156 for (np = p; np && *np && *np != ']'; np++)
1162 return (FNM_NOMATCH);
1167 not = (*p == '!' || *p == '^');
1174 register char cstart = c, cend = c;
1176 if (!(flags & FNM_NOESCAPE) && c == '\\')
1177 cstart = cend = *p++;
1180 /* [ (unterminated) loses. */
1181 return (FNM_NOMATCH);
1185 if ((flags & FNM_PATHNAME) && c == '/')
1186 /* [/] can never match. */
1187 return (FNM_NOMATCH);
1189 if (c == '-' && *p != ']')
1192 if (!(flags & FNM_NOESCAPE) && cend == '\\')
1195 return (FNM_NOMATCH);
1199 if (*n >= cstart && *n <= cend)
1206 return (FNM_NOMATCH);
1212 /* Skip the rest of the [...] that already matched. */
1216 /* [... (unterminated) loses. */
1217 return (FNM_NOMATCH);
1220 if (!(flags & FNM_NOESCAPE) && c == '\\')
1221 /* 1003.2d11 is unclear if this is right. %%% */
1225 return (FNM_NOMATCH);
1231 return (FNM_NOMATCH);
1240 return (FNM_NOMATCH);
1243 #endif /* not SYSTEM_FNMATCH */
1246 /* timegm is a GNU extension, but lately also available on *BSD
1247 systems and possibly elsewhere. */
1249 /* True if YEAR is a leap year. */
1250 #define ISLEAP(year) \
1251 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
1253 /* Number of leap years in the range [y1, y2). */
1254 #define LEAPYEARS(y1, y2) \
1255 ((y2-1)/4 - (y1-1)/4) - ((y2-1)/100 - (y1-1)/100) + ((y2-1)/400 - (y1-1)/400)
1257 /* Inverse of gmtime: converts struct tm to time_t, assuming the data
1258 in tm is UTC rather than local timezone. This implementation
1259 returns the number of seconds elapsed since midnight 1970-01-01,
1260 converted to time_t. */
1263 timegm (struct tm *t)
1265 static const unsigned short int month_to_days[][13] = {
1266 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, /* normal */
1267 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } /* leap */
1269 const int year = 1900 + t->tm_year;
1270 unsigned long secs; /* until 2106-02-07 for 32-bit unsigned long */
1276 days = 365 * (year - 1970);
1277 /* Take into account leap years between 1970 and YEAR, not counting
1279 days += LEAPYEARS (1970, year);
1280 if (t->tm_mon < 0 || t->tm_mon >= 12)
1282 days += month_to_days[ISLEAP (year)][t->tm_mon];
1283 days += t->tm_mday - 1;
1285 secs = days * 86400 + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
1286 return (time_t) secs;
1288 #endif /* HAVE_TIMEGM */
1291 /* strtoll is required by C99 and used by Wget only on systems with
1292 LFS. Unfortunately, some systems have LFS, but no strtoll or
1293 equivalent. These include HPUX 11.0 and Windows.
1295 We use #ifdef NEED_STRTOLL instead of #ifndef HAVE_STRTOLL because
1296 of the systems which have a suitable replacement (e.g. _strtoi64 on
1297 Windows), on which Wget's str_to_wgint is instructed to use that
1301 char_value (char c, int base)
1306 if ('0' <= c && c <= '9')
1308 else if ('a' <= c && c <= 'z')
1309 value = c - 'a' + 10;
1310 else if ('A' <= c && c <= 'Z')
1311 value = c - 'A' + 10;
1319 #define STRTOLL_MAX TYPE_MAXIMUM (strtoll_type)
1320 /* This definition assumes two's complement arithmetic */
1321 #define STRTOLL_MIN (-STRTOLL_MAX - 1)
1323 /* Like a%b, but always returns a positive number when A is negative.
1324 (C doesn't guarantee the sign of the result.) */
1325 #define MOD(a, b) ((strtoll_type) -1 % 2 == 1 ? (a) % (b) : - ((a) % (b)))
1327 /* A strtoll-like replacement for systems that have an integral type
1328 larger than long but don't supply strtoll. This implementation
1329 makes no assumptions about the size of strtoll_type. */
1332 strtoll (const char *nptr, char **endptr, int base)
1334 strtoll_type result = 0;
1337 if (base != 0 && (base < 2 || base > 36))
1343 while (*nptr == ' ' || *nptr == '\t')
1350 else if (*nptr == '+')
1358 /* If BASE is 0, determine the real base based on the beginning on
1359 the number; octal numbers begin with "0", hexadecimal with "0x",
1360 and the others are considered octal. */
1363 if ((base == 0 || base == 16)
1365 (*(nptr + 1) == 'x' || *(nptr + 1) == 'X'))
1369 /* "0x" must be followed by at least one hex char. If not,
1370 return 0 and place ENDPTR on 'x'. */
1371 if (!ISXDIGIT (*nptr))
1385 /* Parse positive number, checking for overflow. */
1387 /* Overflow watermark. If RESULT exceeds it, overflow occurs on
1388 this digit. If result==WATERMARK, current digit may not
1389 exceed the last digit of maximum value. */
1390 const strtoll_type WATERMARK = STRTOLL_MAX / base;
1391 for (; (digit = char_value (*nptr, base)) != -1; ++nptr)
1393 if (result > WATERMARK
1394 || (result == WATERMARK && digit > STRTOLL_MAX % base))
1396 result = STRTOLL_MAX;
1400 result = base * result + digit;
1405 /* Parse negative number, checking for underflow. */
1407 const strtoll_type WATERMARK = STRTOLL_MIN / base;
1408 for (; (digit = char_value (*nptr, base)) != -1; ++nptr)
1410 if (result < WATERMARK
1411 || (result == WATERMARK && digit > MOD (STRTOLL_MIN, base)))
1413 result = STRTOLL_MIN;
1417 result = base * result - digit;
1422 *endptr = (char *) nptr;
1430 #endif /* NEED_STRTOLL */