Eliminating the getopt disgrace.

This commit is contained in:
Christoph Lohmann
2013-06-14 20:20:47 +02:00
parent 75c97de593
commit 4d38f60685
28 changed files with 724 additions and 476 deletions

40
date.c
View File

@@ -5,10 +5,17 @@
#include <unistd.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-u] [-d format] [+FORMAT]\n", argv0);
exit(1);
}
int
main(int argc, char *argv[])
{
char buf[BUFSIZ], c;
char buf[BUFSIZ];
char *fmt = "%c";
struct tm *now = NULL;
struct tm *(*tztime)(const time_t *) = localtime;
@@ -16,24 +23,25 @@ main(int argc, char *argv[])
time_t t;
t = time(NULL);
while((c = getopt(argc, argv, "d:u")) != -1)
switch(c) {
case 'd':
t = estrtol(optarg, 0);
break;
case 'u':
tztime = gmtime;
tz = "gm";
break;
default:
exit(EXIT_FAILURE);
}
if(optind < argc && argv[optind][0] == '+')
fmt = &argv[optind][1];
ARGBEGIN {
case 'd':
t = estrtol(EARGF(usage()), 0);
break;
case 'u':
tztime = gmtime;
tz = "gm";
break;
default:
usage();
} ARGEND;
if(argc > 0 && argv[0][0] == '+')
fmt = &argv[0][1];
if(!(now = tztime(&t)))
eprintf("%stime failed\n", tz);
strftime(buf, sizeof buf, fmt, now);
puts(buf);
return EXIT_SUCCESS;
return 0;
}