]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blockdev: add --help option
authorSami Kerola <kerolasa@iki.fi>
Wed, 29 Jun 2011 20:44:35 +0000 (22:44 +0200)
committerSami Kerola <kerolasa@iki.fi>
Wed, 29 Jun 2011 22:21:21 +0000 (00:21 +0200)
The option is also accepted in format `-h'. As a side effect
usage function is now able to return success, and since it had to
be touched the printing is cleaned up.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
disk-utils/blockdev.c

index d1fd24ac0a10297c8d56e6bc767510881a29e985..19a7aa720ee6405ba75a2c740c39e7de2156582e 100644 (file)
@@ -175,30 +175,27 @@ static const struct bdc bdcms[] =
        }
 };
 
-static void
-usage(void) {
+static void __attribute__ ((__noreturn__)) usage(FILE * out)
+{
        int i;
-       fputc('\n', stderr);
-       fprintf(stderr, _("Usage:\n"));
-       fprintf(stderr, _("  %s -V\n"), program_invocation_short_name);
-       fprintf(stderr, _("  %s --report [devices]\n"), program_invocation_short_name);
-       fprintf(stderr, _("  %s [-v|-q] commands devices\n"), program_invocation_short_name);
-       fputc('\n', stderr);
-
-       fprintf(stderr, _("Available commands:\n"));
-       fprintf(stderr, "  %-25s %s\n", "--getsz",
-                       _("get size in 512-byte sectors"));
+       fprintf(out, _("\nUsage:\n"
+                      "  %1$s -V\n"
+                      "  %1$s --report [devices]\n"
+                      "  %1$s [-v|-q] commands devices\n\n"
+                      "Available commands:\n"), program_invocation_short_name);
+
+       fprintf(out, _("  %-25s get size in 512-byte sectors\n"), "--getsz");
        for (i = 0; i < ARRAY_SIZE(bdcms); i++) {
                if (bdcms[i].argname)
-                       fprintf(stderr, "  %s %-*s %s\n", bdcms[i].name,
-                                       (int) (24 - strlen(bdcms[i].name)),
-                                       bdcms[i].argname, _(bdcms[i].help));
+                       fprintf(out, "  %s %-*s %s\n", bdcms[i].name,
+                               (int)(24 - strlen(bdcms[i].name)),
+                               bdcms[i].argname, _(bdcms[i].help));
                else
-                       fprintf(stderr, "  %-25s %s\n", bdcms[i].name,
-                                       _(bdcms[i].help));
+                       fprintf(out, "  %-25s %s\n", bdcms[i].name,
+                               _(bdcms[i].help));
        }
-       fputc('\n', stderr);
-       exit(1);
+       fputc('\n', out);
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
 static int
@@ -229,13 +226,15 @@ main(int argc, char **argv) {
        textdomain(PACKAGE);
 
        if (argc < 2)
-               usage();
+               usage(stderr);
 
        /* -V not together with commands */
        if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
                printf(_("%s (%s)\n"), program_invocation_short_name, PACKAGE_STRING);
                exit(0);
        }
+       if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
+               usage(stdout);
 
        /* --report not together with other commands */
        if (!strcmp(argv[1], "--report")) {
@@ -269,7 +268,7 @@ main(int argc, char **argv) {
        }
 
        if (d >= argc)
-               usage();
+               usage(stderr);
 
        for (k = d; k < argc; k++) {
                fd = open(argv[k], O_RDONLY, 0);
@@ -299,7 +298,7 @@ do_commands(int fd, char **argv, int d) {
                if (!strcmp(argv[i], "-v")) {
                        verbose = 1;
                        continue;
-               }
+                }
                if (!strcmp(argv[i], "-q")) {
                        verbose = 0;
                        continue;
@@ -317,7 +316,7 @@ do_commands(int fd, char **argv, int d) {
                j = find_cmd(argv[i]);
                if (j == -1) {
                        warnx(_("Unknown command: %s"), argv[i]);
-                       usage();
+                       usage(stderr);
                }
 
                switch(bdcms[j].argtype) {
@@ -334,7 +333,7 @@ do_commands(int fd, char **argv, int d) {
                                if (i == d-1) {
                                        warnx(_("%s requires an argument"),
                                                bdcms[j].name);
-                                       usage();
+                                       usage(stderr);
                                }
                                iarg = atoi(argv[++i]);
                        } else