summaryrefslogtreecommitdiffstats
path: root/usr.sbin/zdump/zdump.c
diff options
context:
space:
mode:
authorderaadt <deraadt@openbsd.org>2015-04-23 05:26:33 +0000
committerderaadt <deraadt@openbsd.org>2015-04-23 05:26:33 +0000
commitf0dc9fba1d4acef94eb24f0208cb8741e14c0e94 (patch)
tree6bc78b55f7a6d226e64966b0ca643e4520e19184 /usr.sbin/zdump/zdump.c
parentAdd a simple regression test for sshd's configuration parser. Right now, (diff)
downloadwireguard-openbsd-f0dc9fba1d4acef94eb24f0208cb8741e14c0e94.tar.xz
wireguard-openbsd-f0dc9fba1d4acef94eb24f0208cb8741e14c0e94.zip
KNF and other style adjustments to make this more scrutible.
reviewed by millert
Diffstat (limited to 'usr.sbin/zdump/zdump.c')
-rw-r--r--usr.sbin/zdump/zdump.c356
1 files changed, 127 insertions, 229 deletions
diff --git a/usr.sbin/zdump/zdump.c b/usr.sbin/zdump/zdump.c
index 081300df532..20261861858 100644
--- a/usr.sbin/zdump/zdump.c
+++ b/usr.sbin/zdump/zdump.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: zdump.c,v 1.9 2015/02/10 22:44:29 deraadt Exp $ */
+/* $OpenBSD: zdump.c,v 1.10 2015/04/23 05:26:33 deraadt Exp $ */
/*
** This file is in the public domain, so clarified as of
** 2009-05-17 by Arthur David Olson.
@@ -10,64 +10,34 @@
** You can use this code to help in verifying other implementations.
*/
-#include <ctype.h> /* for isalpha et al. */
-#include <float.h> /* for FLT_MAX and DBL_MAX */
-#include <stdio.h> /* for stdout, stderr, perror */
-#include <string.h> /* for strlcpy */
-#include <stdlib.h> /* for exit, malloc, atoi */
-#include <time.h> /* for struct tm */
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
-#ifndef ZDUMP_LO_YEAR
#define ZDUMP_LO_YEAR (-500)
-#endif /* !defined ZDUMP_LO_YEAR */
-
-#ifndef ZDUMP_HI_YEAR
#define ZDUMP_HI_YEAR 2500
-#endif /* !defined ZDUMP_HI_YEAR */
-#ifndef MAX_STRING_LENGTH
#define MAX_STRING_LENGTH 1024
-#endif /* !defined MAX_STRING_LENGTH */
-#ifndef TRUE
#define TRUE 1
-#endif /* !defined TRUE */
-
-#ifndef FALSE
#define FALSE 0
-#endif /* !defined FALSE */
-#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 SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
+#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
+#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
+
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
-#endif /* !defined isleap */
#ifndef isleap_sum
/*
@@ -76,83 +46,31 @@
#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
#endif /* !defined isleap_sum */
-#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
-#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
-#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
-
-#define _(msgid) msgid
-
-#ifndef TZ_DOMAIN
-#define TZ_DOMAIN "tz"
-#endif /* !defined TZ_DOMAIN */
-
-extern char ** environ;
-extern int getopt(int argc, char * const argv[],
- const char * options);
-extern char * optarg;
-extern int optind;
-extern char * tzname[2];
-
-static time_t absolute_min_time;
-static time_t absolute_max_time;
-static size_t longest;
-static char * progname;
-static int warned;
-
-static char * abbr(struct tm * tmp);
-static void abbrok(const char * abbrp, const char * zone);
-static long delta(struct tm * newp, struct tm * oldp);
-static void dumptime(const struct tm * tmp);
-static time_t hunt(char * name, time_t lot, time_t hit);
-static void setabsolutes(void);
-static void show(char * zone, time_t t, int v);
-static const char * tformat(void);
-static time_t yeartot(long y);
-
-#ifndef TYPECHECK
-#define my_localtime localtime
-#else /* !defined TYPECHECK */
-static struct tm *
-my_localtime(tp)
-time_t * tp;
-{
- struct tm * tmp;
-
- tmp = localtime(tp);
- if (tp != NULL && tmp != NULL) {
- struct tm tm;
- time_t t;
-
- tm = *tmp;
- t = mktime(&tm);
- if (t - *tp >= 1 || *tp - t >= 1) {
- 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");
- }
- }
- return tmp;
-}
-#endif /* !defined TYPECHECK */
+extern char **environ;
+extern char *tzname[2];
+extern char *__progname;
+
+time_t absolute_min_time;
+time_t absolute_max_time;
+size_t longest;
+int warned;
+
+static char *abbr(struct tm *tmp);
+static void abbrok(const char *abbrp, const char *zone);
+static long delta(struct tm *newp, struct tm *oldp);
+static void dumptime(const struct tm *tmp);
+static time_t hunt(char *name, time_t lot, time_t hit);
+static void setabsolutes(void);
+static void show(char *zone, time_t t, int v);
+static const char *tformat(void);
+static time_t yeartot(long y);
+static void usage(void);
static void
-abbrok(abbrp, zone)
-const char * const abbrp;
-const char * const zone;
+abbrok(const char * const abbrp, const char * const zone)
{
- const char * cp;
- char * wp;
+ const char *cp;
+ char *wp;
if (warned)
return;
@@ -161,85 +79,79 @@ const char * const zone;
while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
++cp;
if (cp - abbrp == 0)
- wp = _("lacks alphabetic at start");
+ wp = "lacks alphabetic at start";
else if (cp - abbrp < 3)
- wp = _("has fewer than 3 alphabetics");
+ wp = "has fewer than 3 alphabetics";
else if (cp - abbrp > 6)
- wp = _("has more than 6 alphabetics");
+ wp = "has more than 6 alphabetics";
if (wp == NULL && (*cp == '+' || *cp == '-')) {
++cp;
if (isascii((unsigned char) *cp) &&
- isdigit((unsigned char) *cp))
- if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
- ++cp;
+ isdigit((unsigned char) *cp))
+ if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
+ ++cp;
if (*cp != '\0')
- wp = _("differs from POSIX standard");
+ wp = "differs from POSIX standard";
}
if (wp == NULL)
return;
fflush(stdout);
- fprintf(stderr, _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
- progname, zone, abbrp, wp);
+ fprintf(stderr, "%s: warning: zone \"%s\" abbreviation \"%s\" %s\n",
+ __progname, zone, abbrp, wp);
warned = TRUE;
}
static void
usage(void)
{
- fprintf(stderr, _("usage: %s [-v] [-c [loyear,]hiyear] zonename ...\n"), progname);
+ fprintf(stderr, "usage: %s [-v] [-c [loyear,]hiyear] zonename ...\n",
+ __progname);
exit(EXIT_FAILURE);
}
int
-main(argc, argv)
-int argc;
-char * argv[];
+main(int argc, char *argv[])
{
- int i;
- int c;
- int vflag;
- char * cutarg;
+ int i, c, vflag = 0;
+ char *cutarg = NULL;
long cutloyear = ZDUMP_LO_YEAR;
long cuthiyear = ZDUMP_HI_YEAR;
- time_t cutlotime;
- time_t cuthitime;
- char ** fakeenv;
- time_t now;
- time_t t;
- time_t newt;
- struct tm tm;
- struct tm newtm;
- struct tm * tmp;
- struct tm * newtmp;
-
- cutlotime = 0;
- cuthitime = 0;
- progname = argv[0];
- vflag = 0;
- cutarg = NULL;
- while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
- if (c == 'v')
+ time_t cutlotime = 0, cuthitime = 0;
+ time_t now, t, newt;
+ struct tm tm, newtm, *tmp, *newtmp;
+ char **fakeenv;
+
+ while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v') {
+ switch (c) {
+ case 'v':
vflag = 1;
- else cutarg = optarg;
- if ((c != EOF && c != -1) ||
- (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
+ break;
+ case 'c':
+ cutarg = optarg;
+ break;
+ default:
usage();
+ break;
+ }
+ }
+ if (c != -1 ||
+ (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
+ usage();
}
if (vflag) {
if (cutarg != NULL) {
- long lo;
- long hi;
+ long lo, hi;
char dummy;
if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
cuthiyear = hi;
} else if (sscanf(cutarg, "%ld,%ld%c",
- &lo, &hi, &dummy) == 2) {
- cutloyear = lo;
- cuthiyear = hi;
+ &lo, &hi, &dummy) == 2) {
+ cutloyear = lo;
+ cuthiyear = hi;
} else {
- fprintf(stderr, _("%s: wild -c argument %s\n"),
- progname, cutarg);
+ fprintf(stderr, "%s: wild -c argument %s\n",
+ __progname, cutarg);
exit(EXIT_FAILURE);
}
}
@@ -252,17 +164,17 @@ char * argv[];
for (i = optind; i < argc; ++i)
if (strlen(argv[i]) > longest)
longest = strlen(argv[i]);
+
{
- int from;
- int to;
+ int from, to;
for (i = 0; environ[i] != NULL; ++i)
continue;
fakeenv = reallocarray(NULL, i + 2, sizeof *fakeenv);
if (fakeenv == NULL ||
- (fakeenv[0] = malloc(longest + 4)) == NULL) {
- perror(progname);
- exit(EXIT_FAILURE);
+ (fakeenv[0] = malloc(longest + 4)) == NULL) {
+ perror(__progname);
+ exit(EXIT_FAILURE);
}
to = 0;
strlcpy(fakeenv[to++], "TZ=", longest + 4);
@@ -273,7 +185,7 @@ char * argv[];
environ = fakeenv;
}
for (i = optind; i < argc; ++i) {
- static char buf[MAX_STRING_LENGTH];
+ char buf[MAX_STRING_LENGTH];
strlcpy(&fakeenv[0][3], argv[i], longest + 1);
if (!vflag) {
@@ -287,7 +199,7 @@ char * argv[];
show(argv[i], t, TRUE);
if (t < cutlotime)
t = cutlotime;
- tmp = my_localtime(&t);
+ tmp = localtime(&t);
if (tmp != NULL) {
tm = *tmp;
strlcpy(buf, abbr(&tm), sizeof buf);
@@ -300,16 +212,15 @@ char * argv[];
if (newtmp != NULL)
newtm = *newtmp;
if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
- (delta(&newtm, &tm) != (newt - t) ||
- newtm.tm_isdst != tm.tm_isdst ||
- strcmp(abbr(&newtm), buf) != 0)) {
- newt = hunt(argv[i], t, newt);
- newtmp = localtime(&newt);
- if (newtmp != NULL) {
- newtm = *newtmp;
- strlcpy(buf, abbr(&newtm),
- sizeof buf);
- }
+ (delta(&newtm, &tm) != (newt - t) ||
+ newtm.tm_isdst != tm.tm_isdst ||
+ strcmp(abbr(&newtm), buf) != 0)) {
+ newt = hunt(argv[i], t, newt);
+ newtmp = localtime(&newt);
+ if (newtmp != NULL) {
+ newtm = *newtmp;
+ strlcpy(buf, abbr(&newtm), sizeof buf);
+ }
}
t = newt;
tm = newtm;
@@ -322,8 +233,8 @@ char * argv[];
show(argv[i], t, TRUE);
}
if (fflush(stdout) || ferror(stdout)) {
- fprintf(stderr, "%s: ", progname);
- perror(_("Error writing to standard output"));
+ fprintf(stderr, "%s: ", __progname);
+ perror("Error writing to standard output");
exit(EXIT_FAILURE);
}
return 0;
@@ -332,11 +243,7 @@ char * argv[];
static void
setabsolutes(void)
{
- /*
- ** time_t is signed. Assume overflow wraps around.
- */
- time_t t = 0;
- time_t t1 = 1;
+ time_t t = 0, t1 = 1;
while (t < t1) {
t = t1;
@@ -351,15 +258,11 @@ setabsolutes(void)
}
static time_t
-yeartot(y)
-const long y;
+yeartot(const long y)
{
- long myy;
- long seconds;
- time_t t;
+ long myy = EPOCH_YEAR, seconds;
+ time_t t = 0;
- myy = EPOCH_YEAR;
- t = 0;
while (myy != y) {
if (myy < y) {
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
@@ -387,13 +290,11 @@ hunt(char *name, time_t lot, time_t hit)
{
time_t t;
long diff;
- struct tm lotm;
- struct tm * lotmp;
- struct tm tm;
- struct tm * tmp;
+ struct tm lotm, *lotmp;
+ struct tm tm, *tmp;
char loab[MAX_STRING_LENGTH];
- lotmp = my_localtime(&lot);
+ lotmp = localtime(&lot);
if (lotmp != NULL) {
lotm = *lotmp;
strlcpy(loab, abbr(&lotm), sizeof loab);
@@ -408,17 +309,18 @@ hunt(char *name, time_t lot, time_t hit)
++t;
else if (t >= hit)
--t;
- tmp = my_localtime(&t);
+ tmp = localtime(&t);
if (tmp != NULL)
tm = *tmp;
if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
- (delta(&tm, &lotm) == (t - lot) &&
- tm.tm_isdst == lotm.tm_isdst &&
- strcmp(abbr(&tm), loab) == 0)) {
- lot = t;
- lotm = tm;
- lotmp = tmp;
- } else hit = t;
+ (delta(&tm, &lotm) == (t - lot) &&
+ tm.tm_isdst == lotm.tm_isdst &&
+ strcmp(abbr(&tm), loab) == 0)) {
+ lot = t;
+ lotm = tm;
+ lotmp = tmp;
+ } else
+ hit = t;
}
show(name, lot, TRUE);
show(name, hit, TRUE);
@@ -430,9 +332,7 @@ hunt(char *name, time_t lot, time_t hit)
*/
static long
-delta(newp, oldp)
-struct tm * newp;
-struct tm * oldp;
+delta(struct tm *newp, struct tm *oldp)
{
long result;
int tmy;
@@ -455,7 +355,7 @@ struct tm * oldp;
static void
show(char *zone, time_t t, int v)
{
- struct tm * tmp;
+ struct tm *tmp;
printf("%-*s ", (int) longest, zone);
if (v) {
@@ -468,7 +368,7 @@ show(char *zone, time_t t, int v)
}
printf(" = ");
}
- tmp = my_localtime(&t);
+ tmp = localtime(&t);
dumptime(tmp);
if (tmp != NULL) {
if (*abbr(tmp) != '\0')
@@ -486,10 +386,9 @@ show(char *zone, time_t t, int v)
}
static char *
-abbr(tmp)
-struct tm * tmp;
+abbr(struct tm *tmp)
{
- char * result;
+ char *result;
static char nada;
if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
@@ -510,20 +409,17 @@ tformat(void)
}
static void
-dumptime(timeptr)
-const struct tm * timeptr;
+dumptime(const struct tm *timeptr)
{
- static const char wday_name[][3] = {
+ static const char wday_name[][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
- static const char mon_name[][3] = {
+ static const char mon_name[][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
- const char * wn;
- const char * mn;
- int lead;
- int trail;
+ const char *wn, *mn;
+ int lead, trail;
if (timeptr == NULL) {
printf("NULL");
@@ -535,17 +431,19 @@ const struct tm * timeptr;
** 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];
+ (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];
+ (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);
+ 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 +