chiark / gitweb /
log: correct usage of logging API at a few places
[elogind.git] / src / machine-id-setup / machine-id-setup-main.c
index ca8af596f7b346886585bc17a07af641e486ff46..6373ebc2db250f0e355250d5f7dcb2571eaee8a4 100644 (file)
 
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdio.h>
+#include <getopt.h>
+#include <errno.h>
 
 #include "machine-id-setup.h"
 #include "log.h"
+#include "build.h"
+
+static int help(void) {
+
+        printf("%s [OPTIONS...]\n\n"
+               "Initialize /etc/machine-id from a random source.\n\n"
+               "  -h --help             Show this help\n"
+               "     --version          Show package version\n",
+               program_invocation_short_name);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        enum {
+                ARG_VERSION = 0x100
+        };
+
+        static const struct option options[] = {
+                { "help",      no_argument,       NULL, 'h'           },
+                { "version",   no_argument,       NULL, ARG_VERSION   },
+                { NULL,        0,                 NULL, 0             }
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0) {
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(DISTRIBUTION);
+                        puts(SYSTEMD_FEATURES);
+                        return 0;
+
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        log_error("Unknown option code %c", c);
+                        return -EINVAL;
+                }
+        }
+
+        if (optind < argc) {
+                help();
+                return -EINVAL;
+        }
+
+        return 1;
+}
 
 int main(int argc, char *argv[]) {
+        int r;
 
-        log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
         log_open();
 
+        r = parse_argv(argc, argv);
+        if (r <= 0)
+                return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+
         return machine_id_setup() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }