From e079c4e6ede2531ce11fb1b95e22efdaa4bf2084 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 20 Feb 2011 00:00:38 +0100 Subject: mkswap: support long options and check user inputs [kzak@redhat-com: - mimor changes to usage()] Signed-off-by: Sami Kerola Signed-off-by: Karel Zak --- disk-utils/mkswap.c | 147 +++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 72 deletions(-) (limited to 'disk-utils/mkswap.c') diff --git a/disk-utils/mkswap.c b/disk-utils/mkswap.c index 6d11250c4..2e11f74ef 100644 --- a/disk-utils/mkswap.c +++ b/disk-utils/mkswap.c @@ -42,6 +42,7 @@ #include #include #include +#include #ifdef HAVE_LIBSELINUX #include #include @@ -269,12 +270,25 @@ It is roughly 2GB on i386, PPC, m68k, ARM, 1GB on sparc, 512MB on mips, #define MAX_BADPAGES ((pagesize-1024-128*sizeof(int)-10)/sizeof(int)) #define MIN_GOODPAGES 10 -static void -usage(void) { - fprintf(stderr, - _("Usage: %s [-c] [-pPAGESZ] [-L label] [-U UUID] /dev/name [blocks]\n"), +static void __attribute__ ((__noreturn__)) usage(FILE *out) +{ + fprintf(out, + _("\nUsage:\n" + " %s [options] device [size]\n"), program_invocation_short_name); - exit(1); + + fprintf(out, _( + "\nOptions:\n" + " -c, --check check bad blocks before creating the swap area\n" + " -f, --force allow swap size area be larger than device\n" + " -p, --pagesize SIZE specify page size in bytes\n" + " -L, --label LABEL specify label\n" + " -v, --swapversion NUM specify swap-space version number\n" + " -U, --uuid UUID specify the uuid to use\n" + " -V, --version output version information and exit\n" + " -h, --help display this help and exit\n\n")); + + exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } static void @@ -328,12 +342,6 @@ get_size(const char *file) { return size; } -static int -isnzdigit(char c) { - return (c >= '1' && c <= '9'); -} - - /* * Check to make certain that our new filesystem won't be created on * an already mounted partition. Code adapted from mke2fs, Copyright @@ -425,7 +433,7 @@ int main(int argc, char ** argv) { struct stat statbuf; struct swap_header_v1_2 *hdr; - int i; + int c; unsigned long long maxpages; unsigned long long goodpages; unsigned long long sz; @@ -433,70 +441,70 @@ main(int argc, char ** argv) { int force = 0; int version = 1; char *block_count = 0; - char *pp; char *opt_label = NULL; unsigned char *uuid = NULL; #ifdef HAVE_LIBUUID const char *opt_uuid = NULL; uuid_t uuid_dat; #endif + struct option longopts[] = { + { "check", no_argument, 0, 'c' }, + { "force", no_argument, 0, 'f' }, + { "pagesize", required_argument, 0, 'p' }, + { "label", required_argument, 0, 'L' }, + { "swapversion", required_argument, 0, 'v' }, + { "uuid", required_argument, 0, 'U' }, + { "version", no_argument, 0, 'V' }, + { "help", no_argument, 0, 'h' }, + { NULL, 0, 0, 0 } + }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - if (argc == 2 && - (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) { - printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); - exit(0); - } - - for (i=1; i