diff options
author | Sami Kerola | 2011-06-29 22:44:35 +0200 |
---|---|---|
committer | Sami Kerola | 2011-06-30 00:21:21 +0200 |
commit | bded204c49951e1412fe9b4c6bc1ba9ba62d0b7d (patch) | |
tree | ec1333a641ec51384dfd5037b3bf9d047a26b0d8 /disk-utils/blockdev.c | |
parent | blockdev: remove progname (diff) | |
download | kernel-qcow2-util-linux-bded204c49951e1412fe9b4c6bc1ba9ba62d0b7d.tar.gz kernel-qcow2-util-linux-bded204c49951e1412fe9b4c6bc1ba9ba62d0b7d.tar.xz kernel-qcow2-util-linux-bded204c49951e1412fe9b4c6bc1ba9ba62d0b7d.zip |
blockdev: add --help option
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>
Diffstat (limited to 'disk-utils/blockdev.c')
-rw-r--r-- | disk-utils/blockdev.c | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/disk-utils/blockdev.c b/disk-utils/blockdev.c index d1fd24ac0..19a7aa720 100644 --- a/disk-utils/blockdev.c +++ b/disk-utils/blockdev.c @@ -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 |