From 316cad616561837a2d99f54539c792add23f8f38 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 28 Jun 2011 23:22:47 +0200 Subject: mkfs: add long options Includes new help output. Signed-off-by: Sami Kerola --- disk-utils/mkfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 14 deletions(-) (limited to 'disk-utils/mkfs.c') diff --git a/disk-utils/mkfs.c b/disk-utils/mkfs.c index e22c5125e..a03f4cffb 100644 --- a/disk-utils/mkfs.c +++ b/disk-utils/mkfs.c @@ -2,8 +2,6 @@ * mkfs A simple generic frontend for the for the mkfs program * under Linux. See the manual page for details. * - * Usage: mkfs [-V] [-t fstype] [fs-options] device [size] - * * Authors: David Engel, * Fred N. van Kempen, * Ron Sommeling, @@ -15,7 +13,7 @@ * */ - +#include #include #include #include @@ -31,31 +29,61 @@ #define PROGNAME "mkfs.%s" +static void __attribute__ ((__noreturn__)) usage(FILE * out) +{ + fprintf(out, + _("Usage: %s [options] [-t type fs-options] device [size]\n"), + program_invocation_short_name); + + fprintf(out, _("\nOptions:\n" + " -t, --type=TYPE file system type, when undefined ext2 is used\n" + " fs-options parameters to real file system builder\n" + " device path to a device\n" + " size number of blocks on the device\n" + " -V, --verbose explain what is done\n" + " defining -V more than once will cause a dry-run\n" + " -V, --version output version information and exit\n" + " -V as version must be only option\n" + " -h, --help display this help and exit\n")); + + fprintf(out, _("\nFor more information see mkfs(8).\n")); + + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); +} + +static void __attribute__ ((__noreturn__)) print_version(void) +{ + printf(_("%s (%s)\n"), + program_invocation_short_name, PACKAGE_STRING); + exit(EXIT_SUCCESS); +} + int main(int argc, char *argv[]) { char *progname; /* name of executable to be called */ char *fstype = NULL; int i, more = 0, verbose = 0; char *oldpath, *newpath; - char *program_name, *p; - program_name = argv[0]; - if ((p = strrchr(program_name, '/')) != NULL) - program_name = p+1; + enum { VERSION_OPTION = CHAR_MAX + 1 }; + + static const struct option longopts[] = { + {"type", required_argument, NULL, 't'}, + {"version", no_argument, NULL, VERSION_OPTION}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - if (argc == 2 && - (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) { - printf(_("%s (%s)\n"), program_name, PACKAGE_STRING); - exit(EXIT_SUCCESS); - } + if (argc == 2 && !strcmp(argv[1], "-V")) + print_version(); /* Check commandline options. */ opterr = 0; - while ((more == 0) && ((i = getopt(argc, argv, "Vt:")) != -1)) + while ((more == 0) && ((i = getopt_long(argc, argv, "Vt:h", longopts, NULL)) != -1)) switch (i) { case 'V': verbose++; @@ -63,13 +91,17 @@ int main(int argc, char *argv[]) case 't': fstype = optarg; break; + case 'h': + usage(stdout); + case VERSION_OPTION: + print_version(); default: optind--; more = 1; break; /* start of specific arguments */ } if (optind == argc) - errx(EXIT_FAILURE, _("Usage: mkfs [-V] [-t fstype] [fs-options] device [size]")); + usage(stderr); /* If -t wasn't specified, use the default */ if (fstype == NULL) -- cgit v1.2.3-55-g7522