From f038b34ec3b5c1fb5021cf02b25d0180a031f3a2 Mon Sep 17 00:00:00 2001 From: hjp Date: Wed, 16 Feb 2000 19:05:31 +0000 Subject: [PATCH] New option -f to specify output format Default format doesn't print argument any more. --- time_t/time_t.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/time_t/time_t.c b/time_t/time_t.c index 81f52a5..9949f7a 100644 --- a/time_t/time_t.c +++ b/time_t/time_t.c @@ -1,6 +1,8 @@ +#include #include #include #include +#include char *cmnd; @@ -12,20 +14,35 @@ static void usage(void) { int main(int argc, char **argv) { int i; + char *format = "%Y-%m-%d %H:%M:%S %Z"; + int c; cmnd = argv[0]; - if (argc <= 1) usage(); + while ((c = getopt(argc, argv, "f:")) != EOF) { + switch(c) { + case 'f': + format = optarg; + break; + case '?': + usage(); + default: + assert("this" == "unreachable"); + } + } - for (i = 1; i < argc; i++) { + if (optind >= argc) usage(); + + for (i = optind; i < argc; i++) { time_t t = strtoul(argv[i], NULL, 0); struct tm *tmp; - char buf[32]; + char buf[1024]; - printf("%lu\t", t); tmp = localtime(&t); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", tmp); + strftime(buf, sizeof(buf), format, tmp); printf("%s\n", buf); } return 0; } +/* vim:sw=4 + */