1630
1630
#endif /* not SYSTEM_FNMATCH */
1633
/* timegm is a GNU extension, but lately also available on *BSD
1634
systems and possibly elsewhere. */
1636
/* True if YEAR is a leap year. */
1637
#define ISLEAP(year) \
1638
((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
1640
/* Number of leap years in the range [y1, y2). */
1641
#define LEAPYEARS(y1, y2) \
1642
((y2-1)/4 - (y1-1)/4) - ((y2-1)/100 - (y1-1)/100) + ((y2-1)/400 - (y1-1)/400)
1644
/* Inverse of gmtime: converts struct tm to time_t, assuming the data
1645
in tm is UTC rather than local timezone. This implementation
1646
returns the number of seconds elapsed since midnight 1970-01-01,
1647
converted to time_t. */
1650
timegm (struct tm *t)
1652
static const unsigned short int month_to_days[][13] = {
1653
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, /* normal */
1654
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } /* leap */
1656
const int year = 1900 + t->tm_year;
1657
unsigned long secs; /* until 2106-02-07 for 32-bit unsigned long */
1663
days = 365 * (year - 1970);
1664
/* Take into account leap years between 1970 and YEAR, not counting
1666
days += LEAPYEARS (1970, year);
1667
if (t->tm_mon < 0 || t->tm_mon >= 12)
1669
days += month_to_days[ISLEAP (year)][t->tm_mon];
1670
days += t->tm_mday - 1;
1672
secs = days * 86400 + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
1673
return (time_t) secs;
1675
#endif /* HAVE_TIMEGM */
1678
/* strtoll is required by C99 and used by Wget only on systems with
1679
LFS. Unfortunately, some systems have LFS, but no strtoll or
1680
equivalent. These include HPUX 11.0 and Windows.
1682
We use #ifdef NEED_STRTOLL instead of #ifndef HAVE_STRTOLL because
1683
of the systems which have a suitable replacement (e.g. _strtoi64 on
1684
Windows), on which Wget's str_to_wgint is instructed to use that
1688
char_value (char c, int base)
1693
if ('0' <= c && c <= '9')
1695
else if ('a' <= c && c <= 'z')
1696
value = c - 'a' + 10;
1697
else if ('A' <= c && c <= 'Z')
1698
value = c - 'A' + 10;
1706
#define LL strtoll_return /* long long or __int64 */
1708
/* These constants assume 64-bit strtoll_return. */
1710
/* A roundabout way of writing 2**63-1 = 9223372036854775807 */
1711
#define STRTOLL_OVERFLOW (((LL) 1 << 62) - 1 + ((LL) 1 << 62))
1712
/* A roundabout way of writing -2**63 = -9223372036854775808 */
1713
#define STRTOLL_UNDERFLOW (-STRTOLL_OVERFLOW - 1)
1715
/* A strtoll replacement for systems that have LFS but don't supply
1716
strtoll. The headers typedef strtoll_return to long long or to
1720
strtoll (const char *nptr, char **endptr, int base)
1722
strtoll_return result = 0;
1725
if (base != 0 && (base < 2 || base > 36))
1731
while (*nptr == ' ' || *nptr == '\t')
1738
else if (*nptr == '+')
1746
/* If base is 0, determine the real base based on the beginning on
1747
the number; octal numbers begin with "0", hexadecimal with "0x",
1748
and the others are considered octal. */
1751
if ((base == 0 || base == 16)
1753
(*(nptr + 1) == 'x' || *(nptr + 1) == 'X'))
1766
/* Parse positive number, checking for overflow. */
1768
for (; (val = char_value (*nptr, base)) != -1; ++nptr)
1770
strtoll_return newresult = base * result + val;
1771
if (newresult < result)
1773
result = STRTOLL_OVERFLOW;
1782
/* Parse negative number, checking for underflow. */
1784
for (; (val = char_value (*nptr, base)) != -1; ++nptr)
1786
strtoll_return newresult = base * result - val;
1787
if (newresult > result)
1789
result = STRTOLL_UNDERFLOW;
1797
*endptr = (char *) nptr;
1800
#endif /* NEED_STRTOLL */