amiga-tz/utility/zdump_amiga.c

726 lines
19 KiB
C

#include "tzversion.h"
#include "clib/timezone.h"
#include "inline/timezone.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
const char *vers = "\0$VER: ZDump" AMIGA_VERSION;
struct Library *DOSBase = NULL;
struct Library *TimezoneBase = NULL;
struct Locale *locale = NULL;
struct RDArgs *rdargs = NULL;
#if HAVE_STDINT_H
# include "stdint.h"
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifndef INT_FAST32_MAX
# if INT_MAX >> 31 == 0
typedef long int_fast32_t;
# else
typedef int int_fast32_t;
# endif
#endif
/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
#if !defined LLONG_MAX && defined __LONG_LONG_MAX__
# define LLONG_MAX __LONG_LONG_MAX__
#endif
#ifndef INTMAX_MAX
# ifdef LLONG_MAX
typedef long long intmax_t;
# define strtoimax strtoll
# define INTMAX_MAX LLONG_MAX
# else
typedef long intmax_t;
# define strtoimax strtol
# define INTMAX_MAX LONG_MAX
# endif
#endif
#ifndef PRIdMAX
# if INTMAX_MAX == LLONG_MAX
# define PRIdMAX "lld"
# else
# define PRIdMAX "ld"
# endif
#endif
#ifndef ZDUMP_LO_YEAR
# define ZDUMP_LO_YEAR (-500)
#endif
#ifndef ZDUMP_HI_YEAR
# define ZDUMP_HI_YEAR 2500
#endif
#ifndef MAX_STRING_LENGTH
# define MAX_STRING_LENGTH 1024
#endif
#if __STDC_VERSION__ < 199901
# define true 1
# define false 0
# define bool int
#else
# include <stdbool.h>
#endif
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif /* !defined EXIT_SUCCESS */
#ifndef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif /* !defined EXIT_FAILURE */
#ifndef SECSPERMIN
# define SECSPERMIN 60
#endif /* !defined SECSPERMIN */
#ifndef MINSPERHOUR
# define MINSPERHOUR 60
#endif /* !defined MINSPERHOUR */
#ifndef SECSPERHOUR
# define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
#endif /* !defined SECSPERHOUR */
#ifndef HOURSPERDAY
# define HOURSPERDAY 24
#endif /* !defined HOURSPERDAY */
#ifndef EPOCH_YEAR
# define EPOCH_YEAR 1970
#endif /* !defined EPOCH_YEAR */
#ifndef TM_YEAR_BASE
# define TM_YEAR_BASE 1900
#endif /* !defined TM_YEAR_BASE */
#ifndef DAYSPERNYEAR
# define DAYSPERNYEAR 365
#endif /* !defined DAYSPERNYEAR */
#ifndef isleap
# define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
#endif /* !defined isleap */
#ifndef isleap_sum
/*
** See tzfile.h for details on isleap_sum.
*/
# define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
#endif /* !defined isleap_sum */
#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
#define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \
+ SECSPERLYEAR * (intmax_t) (100 - 3))
/*
** True if SECSPER400YEARS is known to be representable as an
** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false
** even if SECSPER400YEARS is representable, because when that happens
** the code merely runs a bit more slowly, and this slowness doesn't
** occur on any practical platform.
*/
enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 };
#if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
# define ATTRIBUTE_PURE __attribute__ ((__pure__))
#else
# define ATTRIBUTE_PURE /* empty */
#endif
#define _(msgid) msgid
/* The minimum and maximum finite time values. */
enum { atime_shift = CHAR_BIT * sizeof (time_t) - 2 };
time_t const absolute_min_time =
((time_t) -1 < 0
? (- ((time_t) ~ (time_t) 0 < 0)
- (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift)))
: 0);
time_t const absolute_max_time =
((time_t) -1 < 0
? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))
: -1);
int longest;
bool warned;
bool errout;
static char *progname;
long long int strtoll(const char *nptr, char **endptr, int base);
char const *abbr(struct tm const *);
intmax_t delta(struct tm *, struct tm *) ATTRIBUTE_PURE;
static void dumptime(struct tm const *);
static time_t hunt(timezone_t, char *, time_t, time_t);
void show(timezone_t, char *, time_t, bool);
const char *tformat(void);
static time_t yeartot(intmax_t) ATTRIBUTE_PURE;
/* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
#define is_digit(c) ((unsigned)(c) - '0' <= 9)
/**
* @brief
* Is A an alphabetic character in the C locale?
*/
static bool is_alpha(char a)
{
switch (a) {
default:
return false;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
case 'V': case 'W': case 'X': case 'Y': case 'Z':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
case 'v': case 'w': case 'x': case 'y': case 'z':
return true;
}
}
static struct tm *
my_localtime_rz(timezone_t tz, time_t *tp, struct tm *tmp)
{
tmp = localtime_rz(tz, tp, tmp);
if (tmp) {
struct tm tm;
register time_t t;
tm = *tmp;
t = mktime_z(tz, &tm);
if (t != *tp) {
fflush(stdout);
fprintf(stderr, "\n%s: ", progname);
fprintf(stderr, tformat(), *tp);
fprintf(stderr, " ->");
fprintf(stderr, " year=%d", tmp->tm_year);
fprintf(stderr, " mon=%d", tmp->tm_mon);
fprintf(stderr, " mday=%d", tmp->tm_mday);
fprintf(stderr, " hour=%d", tmp->tm_hour);
fprintf(stderr, " min=%d", tmp->tm_min);
fprintf(stderr, " sec=%d", tmp->tm_sec);
fprintf(stderr, " isdst=%d", tmp->tm_isdst);
fprintf(stderr, " -> ");
fprintf(stderr, tformat(), t);
fprintf(stderr, "\n");
errout = true;
}
}
return tmp;
}
/**
* @brief
* Return A + B, exiting if the result would overflow.
*/
size_t sumsize(size_t a, size_t b)
{
size_t sum = a + b;
if (sum < a) {
fprintf(stderr, "size overflow\n");
exit(EXIT_FAILURE);
}
return sum;
}
void abbrok(const char *const abbrp, const char *const zone)
{
register const char *cp;
register const char *wp;
if (warned)
return;
cp = abbrp;
while (is_alpha(*cp) || is_digit(*cp) || *cp == '-' || *cp == '+')
++cp;
if (cp - abbrp < 3)
wp = _("has fewer than 3 characters");
else if (cp - abbrp > 6)
wp = _("has more than 6 characters");
else if (*cp)
wp = _("has characters other than ASCII alphanumerics, '-' or '+'");
else
return;
fflush(stdout);
fprintf(stderr,
_("Warning: zone \"%s\" abbreviation \"%s\" %s\n"),
zone, abbrp, wp);
warned = errout = true;
}
char const *saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp)
{
char const *ab = abbr(tmp);
return ab;
}
#define ARGSFORMAT "VERBOSE/S,VERBOSE2/S,C=CUTOFF/K,T=CUTOFFVERBOSE/K,ZONENAMES/M"
int amiga_open_libs();
void amiga_close_libs();
struct progargs {
long vflag;
long Vflag;
char *cutarg;
char *cuttimes;
char **zonenames;
};
int main(int argc, char *argv[])
{
int result;
struct tm tm;
struct tm newtm;
struct tm *newtmp;
char *abbrev = NULL;
size_t abbrevsize = 0;
int i = 0;
time_t cutlotime = absolute_min_time;
time_t cuthitime = absolute_max_time;
time_t now = 0;
time_t t = 0;
time_t newt = 0;
struct tm *tmp = NULL;
struct progargs args = {
FALSE,
FALSE,
NULL,
NULL,
NULL
};
if((result = amiga_open_libs()) != 0) {
exit(result);
}
progname = argv[0];
rdargs = ReadArgs((STRPTR)ARGSFORMAT, (APTR)&args, NULL);
if (!rdargs)
{
PrintFault(IoErr(), (APTR)argv[0]);
exit(5);
}
if (args.vflag | args.Vflag) {
intmax_t lo;
intmax_t hi;
char *loend, *hiend;
register intmax_t cutloyear = ZDUMP_LO_YEAR;
register intmax_t cuthiyear = ZDUMP_HI_YEAR;
if (args.cutarg != NULL) {
lo = strtoimax(args.cutarg, &loend, 10);
if (args.cutarg != loend && !*loend) {
hi = lo;
cuthiyear = hi;
} else if (args.cutarg != loend && *loend == ','
&& (hi = strtoimax(loend + 1, &hiend, 10),
loend + 1 != hiend && !*hiend)) {
cutloyear = lo;
cuthiyear = hi;
} else {
VPrintf((STRPTR)"Wild C argument %s\n", (IPTR*)args.cutarg);
return EXIT_FAILURE;
}
}
if (args.cutarg != NULL || args.cuttimes == NULL) {
cutlotime = yeartot(cutloyear);
cuthitime = yeartot(cuthiyear);
}
if (args.cuttimes != NULL) {
lo = strtoimax(args.cuttimes, &loend, 10);
if (args.cuttimes != loend && !*loend) {
hi = lo;
if (hi < cuthitime) {
if (hi < absolute_min_time)
hi = absolute_min_time;
cuthitime = hi;
}
} else if (args.cuttimes != loend && *loend == ','
&& (hi = strtoimax(loend + 1, &hiend, 10),
loend + 1 != hiend && !*hiend)) {
if (cutlotime < lo) {
if (absolute_max_time < lo)
lo = absolute_max_time;
cutlotime = lo;
}
if (hi < cuthitime) {
if (hi < absolute_min_time)
hi = absolute_min_time;
cuthitime = hi;
}
} else {
VPrintf((STRPTR)"Wild CTargument %s\n", (IPTR*)args.cuttimes);
return EXIT_FAILURE;
}
}
}
now = time(NULL);
longest = 0;
for (i = 0; args.zonenames[i]; i++) {
size_t arglen = strlen(args.zonenames[i]);
if (longest < arglen)
longest = arglen < INT_MAX ? arglen : INT_MAX;
}
for (i = 0; args.zonenames[i]; i++) {
timezone_t tz = tzalloc(args.zonenames[i]);
char const *ab = NULL;
if (!tz) {
perror(args.zonenames[i]);
return EXIT_FAILURE;
}
if (! (args.vflag | args.Vflag)) {
show(tz, args.zonenames[i], now, false);
tzfree(tz);
continue;
}
warned = false;
t = absolute_min_time;
if (!args.Vflag) {
show(tz, args.zonenames[i], t, true);
t += SECSPERDAY;
show(tz, args.zonenames[i], t, true);
}
if (t < cutlotime)
t = cutlotime;
tmp = localtime_rz(tz, &t, &tm);
if (tmp)
ab = saveabbr(&abbrev, &abbrevsize, &tm);
while (t < cuthitime) {
newt = ((t < absolute_max_time - SECSPERDAY / 2
&& t + SECSPERDAY / 2 < cuthitime)
? t + SECSPERDAY / 2
: cuthitime);
newtmp = localtime_rz(tz, &newt, &newtm);
if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
(delta(&newtm, &tm) != (newt - t) ||
newtm.tm_isdst != tm.tm_isdst ||
strcmp(abbr(&newtm), ab) != 0)) {
newt = hunt(tz, args.zonenames[i], t, newt);
newtmp = localtime_rz(tz, &newt, &newtm);
if (newtmp)
ab = saveabbr(&abbrev, &abbrevsize,
&newtm);
}
t = newt;
tm = newtm;
tmp = newtmp;
}
if (!args.Vflag) {
t = absolute_max_time;
t -= SECSPERDAY;
show(tz, args.zonenames[i], t, true);
t += SECSPERDAY;
show(tz, args.zonenames[i], t, true);
}
tzfree(tz);
}
return errout ? EXIT_FAILURE : EXIT_SUCCESS;
}
//--------------------------------------------------------------------------//
static time_t
yeartot(intmax_t y)
{
register intmax_t myy, seconds, years;
register time_t t;
myy = EPOCH_YEAR;
t = 0;
while (myy < y) {
if (SECSPER400YEARS_FITS && 400 <= y - myy) {
intmax_t diff400 = (y - myy) / 400;
if (INTMAX_MAX / SECSPER400YEARS < diff400)
return absolute_max_time;
seconds = diff400 * SECSPER400YEARS;
years = diff400 * 400;
} else {
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
years = 1;
}
myy += years;
if (t > absolute_max_time - seconds)
return absolute_max_time;
t += seconds;
}
while (y < myy) {
if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) {
intmax_t diff400 = (myy - y) / 400;
if (INTMAX_MAX / SECSPER400YEARS < diff400)
return absolute_min_time;
seconds = diff400 * SECSPER400YEARS;
years = diff400 * 400;
} else {
seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR;
years = 1;
}
myy -= years;
if (t < absolute_min_time + seconds)
return absolute_min_time;
t -= seconds;
}
return t;
}
static time_t
hunt(timezone_t tz, char *name, time_t lot, time_t hit)
{
static char * loab;
static size_t loabsize;
char const * ab = NULL;
time_t t;
struct tm lotm;
struct tm tm;
bool lotm_ok = my_localtime_rz(tz, &lot, &lotm) != NULL;
bool tm_ok;
if (lotm_ok)
ab = saveabbr(&loab, &loabsize, &lotm);
for ( ; ; ) {
time_t diff = hit - lot;
if (diff < 2)
break;
t = lot;
t += diff / 2;
if (t <= lot)
++t;
else if (t >= hit)
--t;
tm_ok = my_localtime_rz(tz, &t, &tm) != NULL;
if (lotm_ok & tm_ok
? (delta(&tm, &lotm) == t - lot
&& tm.tm_isdst == lotm.tm_isdst
&& strcmp(abbr(&tm), ab) == 0)
: lotm_ok == tm_ok) {
lot = t;
if (tm_ok)
lotm = tm;
} else hit = t;
}
return hit;
}
intmax_t delta(struct tm * newp, struct tm *oldp)
{
register intmax_t result;
register int tmy;
if (newp->tm_year < oldp->tm_year)
return -delta(oldp, newp);
result = 0;
for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
result += newp->tm_yday - oldp->tm_yday;
result *= HOURSPERDAY;
result += newp->tm_hour - oldp->tm_hour;
result *= MINSPERHOUR;
result += newp->tm_min - oldp->tm_min;
result *= SECSPERMIN;
result += newp->tm_sec - oldp->tm_sec;
return result;
}
long gmtoff(struct tm const *a, struct tm const *b)
{
return a->tm_gmtoff;
}
void show(timezone_t tz, char *zone, time_t t, bool v)
{
struct tm *tmp;
struct tm *gmtmp;
struct tm tm, gmtm;
gmtmp = NULL;
printf("%-*s ", longest, zone);
if (v) {
gmtmp = gmtime_r(&t, &gmtm);
if (gmtmp == NULL) {
printf(tformat(), t);
} else {
dumptime(gmtmp);
printf(" UT");
}
printf(" = ");
}
tmp = localtime_rz(tz, &t, &tm);
dumptime(tmp);
if (tmp != NULL) {
if (*abbr(tmp) != '\0')
printf(" %s", abbr(tmp));
if (v) {
long off = gmtoff(tmp, gmtmp);
printf(" isdst=%d", tmp->tm_isdst);
if (off != LONG_MIN)
printf(" gmtoff=%ld", off);
}
}
printf("\n");
if (tmp != NULL && *abbr(tmp) != '\0')
abbrok(abbr(tmp), zone);
}
char const * abbr(struct tm const *tmp)
{
return tmp->tm_zone;
}
const char *tformat(void)
{
if (0 > (time_t) -1) { /* signed */
if (sizeof (time_t) == sizeof (intmax_t))
return "%"PRIdMAX;
if (sizeof (time_t) > sizeof (long))
return "%lld";
if (sizeof (time_t) > sizeof (int))
return "%ld";
return "%d";
}
#ifdef PRIuMAX
if (sizeof (time_t) == sizeof (uintmax_t))
return "%"PRIuMAX;
#endif
if (sizeof (time_t) > sizeof (unsigned long))
return "%llu";
if (sizeof (time_t) > sizeof (unsigned int))
return "%lu";
return "%u";
}
static void
dumptime(register const struct tm *timeptr)
{
static const char wday_name[][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char mon_name[][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
register const char* wn;
register const char* mn;
register int lead;
register int trail;
if (timeptr == NULL) {
printf("NULL");
return;
}
/*
** The packaged localtime_rz and gmtime_r never put out-of-range
** values in tm_wday or tm_mon, but since this code might be compiled
** with other (perhaps experimental) versions, paranoia is in order.
*/
if (timeptr->tm_wday < 0 || timeptr->tm_wday >= (int) (sizeof wday_name / sizeof wday_name[0]))
wn = "???";
else
wn = wday_name[timeptr->tm_wday];
if (timeptr->tm_mon < 0 || timeptr->tm_mon >= (int) (sizeof mon_name / sizeof mon_name[0]))
mn = "???";
else
mn = mon_name[timeptr->tm_mon];
printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
wn, mn,
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec);
#define DIVISOR 10
trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
trail / DIVISOR;
trail %= DIVISOR;
if (trail < 0 && lead > 0) {
trail += DIVISOR;
--lead;
} else if (lead < 0 && trail > 0) {
trail -= DIVISOR;
++lead;
}
if (lead == 0)
printf("%d", trail);
else
printf("%d%d", lead, ((trail < 0) ? -trail : trail));
}
void amiga_open_lib_error(char *name, int version)
{
FPrintf(Output(), (STRPTR)OPEN_VER_ERROR, name, version);
}
int amiga_open_libs()
{
atexit(amiga_close_libs);
DOSBase = OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV);
if(!DOSBase) {
amiga_open_lib_error(DOSLIB_NAME, DOSLIB_REV);
return 5;
}
TimezoneBase = OpenLibrary((CONST_STRPTR)TIMEZONELIB_NAME, TIMEZONELIB_REV);
if(!TimezoneBase) {
amiga_open_lib_error(TIMEZONELIB_NAME, TIMEZONELIB_REV);
return 5;
}
return 0;
}
void amiga_close_libs()
{
if(rdargs) {
FreeArgs(rdargs);
}
if (TimezoneBase != NULL) {
CloseLibrary(TimezoneBase);
TimezoneBase = NULL;
}
if (DOSBase != NULL) {
CloseLibrary(DOSBase);
DOSBase = NULL;
}
}