From e3a4aaa7d3ce308172aa2c0cf2e241d310c762e3 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 25 Jun 2014 13:21:35 +0200 Subject: fdisk: add long options, cleanup man page and usage() Signed-off-by: Karel Zak --- disk-utils/fdisk.c | 94 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 33 deletions(-) (limited to 'disk-utils/fdisk.c') diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index 5da705293..88072810e 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -45,32 +45,6 @@ # include #endif -static void __attribute__ ((__noreturn__)) usage(FILE *out) -{ - fputs(USAGE_HEADER, out); - - fprintf(out, - _(" %1$s [options] change partition table\n" - " %1$s [options] -l [] list partition table(s)\n"), - program_invocation_short_name); - - fputs(USAGE_OPTIONS, out); - fputs(_(" -b sector size (512, 1024, 2048 or 4096)\n"), out); - fputs(_(" -c[=] compatible mode: 'dos' or 'nondos' (default)\n"), out); - fputs(_(" -h print this help text\n"), out); - fputs(_(" -c[=] compatible mode: 'dos' or 'nondos' (default)\n"), out); - fputs(_(" -L[=] colorize output (auto, always or never)\n"), out); - fputs(_(" -t force fdisk to recognize specified partition table type only\n"), out); - fputs(_(" -u[=] display units: 'cylinders' or 'sectors' (default)\n"), out); - fputs(_(" -v print program version\n"), out); - fputs(_(" -C specify the number of cylinders\n"), out); - fputs(_(" -H specify the number of heads\n"), out); - fputs(_(" -S specify the number of sectors per track\n"), out); - - fprintf(out, USAGE_MAN_TAIL("fdisk(8)")); - exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); -} - int get_user_reply(struct fdisk_context *cxt, const char *prompt, @@ -764,6 +738,38 @@ static sector_t get_dev_blocks(char *dev) return size/2; } +static void __attribute__ ((__noreturn__)) usage(FILE *out) +{ + fputs(USAGE_HEADER, out); + + fprintf(out, + _(" %1$s [options] change partition table\n" + " %1$s [options] -l [] list partition table(s)\n"), + program_invocation_short_name); + + fputs(USAGE_OPTIONS, out); + fputs(_(" -b, --sector-size physical and logical sector size\n"), out); + fputs(_(" -c, --compatibility[=] mode is 'dos' or 'nondos' (default)\n"), out); + fputs(_(" -L, --color[=] colorize output (auto, always or never)\n"), out); + fputs(_(" -l, --list display partitions end exit\n"), out); + fputs(_(" -t, --type recognize specified partition table type only\n"), out); + fputs(_(" -u, --units[=] display units: 'cylinders' or 'sectors' (default)\n"), out); + fputs(_(" -s, --getsz display device size in 512-byte sectors [DEPRECATED]\n"), out); + + fputs(USAGE_SEPARATOR, out); + fputs(_(" -C, --geom-cylinders specify the number of cylinders\n"), out); + fputs(_(" -H, --geom-heads specify the number of heads\n"), out); + fputs(_(" -S, --geom-sectors specify the number of sectors per track\n"), out); + + fputs(USAGE_SEPARATOR, out); + fputs(USAGE_HELP, out); + fputs(USAGE_VERSION, out); + + fprintf(out, USAGE_MAN_TAIL("fdisk(8)")); + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); +} + + enum { ACT_FDISK = 0, /* default */ ACT_LIST, @@ -776,6 +782,22 @@ int main(int argc, char **argv) int colormode = UL_COLORMODE_UNDEF; struct fdisk_context *cxt; + static const struct option longopts[] = { + { "color", optional_argument, NULL, 'L' }, + { "compatibility", optional_argument, NULL, 'c' }, + { "geom-cylinders", required_argument, NULL, 'C' }, + { "geom-heads", required_argument, NULL, 'H' }, + { "geom-sectors", required_argument, NULL, 'S' }, + { "getsz", no_argument, NULL, 's' }, + { "help", no_argument, NULL, 'h' }, + { "list", no_argument, NULL, 'l' }, + { "sector-size", required_argument, NULL, 'b' }, + { "type", required_argument, NULL, 't' }, + { "units", optional_argument, NULL, 'u' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -788,7 +810,8 @@ int main(int argc, char **argv) fdisk_context_set_ask(cxt, ask_callback, NULL); - while ((c = getopt(argc, argv, "b:c::C:hH:lL::sS:t:u::vV")) != -1) { + while ((c = getopt_long(argc, argv, "b:c::C:hH:lL::sS:t:u::vV", + longopts, NULL)) != -1) { switch (c) { case 'b': { @@ -808,16 +831,21 @@ int main(int argc, char **argv) case 'c': if (optarg) { /* this setting is independent on the current - * actively used label */ + * actively used label + */ + char *p = *optarg == '=' ? optarg + 1 : optarg; struct fdisk_label *lb = fdisk_context_get_label(cxt, "dos"); + if (!lb) err(EXIT_FAILURE, _("not found DOS label driver")); - if (strcmp(optarg, "=dos") == 0) + if (strcmp(p, "dos") == 0) fdisk_dos_enable_compatible(lb, TRUE); - else if (strcmp(optarg, "=nondos") == 0) + else if (strcmp(p, "nondos") == 0) fdisk_dos_enable_compatible(lb, FALSE); - else + else { + warnx(_("unknown compatibility mode '%s'"), p); usage(stderr); + } } /* use default if no optarg specified */ break; @@ -861,8 +889,8 @@ int main(int argc, char **argv) if (fdisk_context_set_unit(cxt, optarg) != 0) usage(stderr); break; - case 'V': - case 'v': + case 'V': /* preferred for util-linux */ + case 'v': /* for backward compatibility only */ printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': -- cgit v1.2.3-55-g7522