Eliminating the getopt disgrace.
This commit is contained in:
40
date.c
40
date.c
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user