]> granicus.if.org Git - procps-ng/commitdiff
uptime: new usage & fix coding style
authorSami Kerola <kerolasa@iki.fi>
Sat, 4 Jun 2011 19:33:11 +0000 (21:33 +0200)
committerSami Kerola <kerolasa@iki.fi>
Tue, 20 Dec 2011 16:17:02 +0000 (17:17 +0100)
Coding style fixed and more readable help output.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
uptime.c

index 6a4d8bd8171fa1dc31c4c8f176c662f81a532334..b0b2b2f5a03bf4351c74f94490da91ffbf3779a8 100644 (file)
--- a/uptime.c
+++ b/uptime.c
@@ -1,17 +1,45 @@
+#include <errno.h>
+#include <getopt.h>
+#include <stdlib.h>
 #include <stdio.h>
-#include <string.h>
 #include "proc/whattime.h"
 #include "proc/version.h"
 
-int main(int argc, char *argv[]) {
-    if(argc == 1) {
-        print_uptime();
-        return 0;
-    }
-    if((argc == 2) && (!strcmp(argv[1], "-V"))) {
-        display_version();
-        return 0;
-    }
-    fprintf(stderr, "usage: uptime [-V]\n    -V    display version\n");
-    return 1;
+static void __attribute__ ((__noreturn__))
+    usage(FILE * out)
+{
+       fprintf(out,
+               "\nUsage: %s [options]\n"
+               "\nOptions:\n", program_invocation_short_name);
+       fprintf(out,
+               "  -h, --help          display this help text\n"
+               "  -V, --version       display version information and exit\n");
+       fprintf(out, "\nFor more information see uptime(1).\n");
+
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+int main(int argc, char **argv)
+{
+       int c;
+
+       static const struct option longopts[] = {
+               {"help", no_argument, NULL, 'h'},
+               {"version", no_argument, NULL, 'V'},
+               {NULL, 0, NULL, 0}
+       };
+
+       while ((c = getopt_long(argc, argv, "hV", longopts, NULL)) != -1)
+               switch (c) {
+               case 'h':
+                       usage(stdout);
+               case 'V':
+                       display_version();
+                       return EXIT_SUCCESS;
+               default:
+                       usage(stderr);
+               }
+
+       print_uptime();
+       return EXIT_SUCCESS;
 }