Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/dkctl - pass the proper argument vector so that subcomm...



details:   https://anonhg.NetBSD.org/src/rev/7a29b35ce952
branches:  trunk
changeset: 333954:7a29b35ce952
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Nov 23 15:43:49 2014 +0000

description:
- pass the proper argument vector so that subcommands can use getopt.
- add -q and -e for listwedges.

diffstat:

 sbin/dkctl/dkctl.c |  185 ++++++++++++++++++++++++++++------------------------
 1 files changed, 99 insertions(+), 86 deletions(-)

diffs (truncated from 485 to 300 lines):

diff -r bc84403090a4 -r 7a29b35ce952 sbin/dkctl/dkctl.c
--- a/sbin/dkctl/dkctl.c        Sun Nov 23 13:42:06 2014 +0000
+++ b/sbin/dkctl/dkctl.c        Sun Nov 23 15:43:49 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dkctl.c,v 1.21 2014/11/04 08:00:44 mlelstv Exp $       */
+/*     $NetBSD: dkctl.c,v 1.22 2014/11/23 15:43:49 christos Exp $      */
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -41,10 +41,9 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: dkctl.c,v 1.21 2014/11/04 08:00:44 mlelstv Exp $");
+__RCSID("$NetBSD: dkctl.c,v 1.22 2014/11/23 15:43:49 christos Exp $");
 #endif
 
-
 #include <sys/param.h>
 #include <sys/ioctl.h>
 #include <sys/dkio.h>
@@ -55,6 +54,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>     
 #include <unistd.h>
 #include <util.h>
@@ -86,7 +86,6 @@
 static int     fd;                             /* file descriptor for device */
 static const   char *dvname;                   /* device name */
 static char    dvname_store[MAXPATHLEN];       /* for opendisk(3) */
-static const   char *cmdname;                  /* command user issued */
 
 static int dkw_sort(const void *, const void *);
 static int yesno(const char *);
@@ -177,15 +176,10 @@
        dvname = argv[1];
        if (argc == 2)
                showall();
-       else {
-               /* Skip program name, get and skip device name and command. */
-               cmdname = argv[2];
-               argv += 3;
-               argc -= 3;
-               run(argc, argv);
-       }
+       else
+               run(argc - 2, argv + 2);
 
-       exit(0);
+       return EXIT_SUCCESS;
 }
 
 static void
@@ -193,13 +187,13 @@
 {
        struct command *command;
 
-       command = lookup(cmdname);
+       command = lookup(argv[0]);
 
        /* Open the device. */
        fd = opendisk(dvname, command->open_flags, dvname_store,
            sizeof(dvname_store), 0);
        if (fd == -1)
-               err(1, "%s", dvname);
+               err(EXIT_FAILURE, "%s", dvname);
        dvname = dvname_store;
 
        (*command->cmd_func)(argc, argv);
@@ -218,7 +212,7 @@
                if (strcmp(name, commands[i].cmd_name) == 0)
                        break;
        if (commands[i].cmd_name == NULL)
-               errx(1, "unknown command: %s", name);
+               errx(EXIT_FAILURE, "unknown command: %s", name);
 
        return &commands[i];
 }
@@ -229,7 +223,7 @@
        int i;
 
        fprintf(stderr,
-           "usage: %s device\n"
+           "Usage: %s device\n"
            "       %s device command [arg [...]]\n",
            getprogname(), getprogname());
 
@@ -238,27 +232,23 @@
                fprintf(stderr, "\t%s %s\n", commands[i].cmd_name,
                    commands[i].arg_names);
 
-       exit(1);
+       exit(EXIT_FAILURE);
 }
 
 static void
 showall(void)
 {
-       printf("strategy:\n");
-       cmdname = "strategy";
-       run(0, NULL);
-
-       putchar('\n');
+       static const char *cmds[] = { "strategy", "getcache", "listwedges" };
+       size_t i;
+       char *args[2];
 
-       printf("cache:\n");
-       cmdname = "getcache";
-       run(0, NULL);
-
-       putchar('\n');
-
-       printf("wedges:\n");
-       cmdname = "listwedges";
-       run(0, NULL);
+       args[1] = NULL;
+       for (i = 0; i < __arraycount(cmds); i++) {
+               printf("%s:\n", cmds[i]);
+               args[0] = __UNCONST(cmds[i]);
+               run(1, args);
+               putchar('\n');
+       }
 }
 
 static void
@@ -274,17 +264,17 @@
 
        memset(&dks, 0, sizeof(dks));
        switch (argc) {
-       case 0:
+       case 1:
                /* show the buffer queue strategy used */
                printf("%s: %s\n", dvname, odks.dks_name);
                return;
-       case 1:
+       case 2:
                /* set the buffer queue strategy */
-               strlcpy(dks.dks_name, argv[0], sizeof(dks.dks_name));
+               strlcpy(dks.dks_name, argv[1], sizeof(dks.dks_name));
                if (ioctl(fd, DIOCSSTRATEGY, &dks) == -1) {
                        err(EXIT_FAILURE, "%s: DIOCSSTRATEGY", dvname);
                }
-               printf("%s: %s -> %s\n", dvname, odks.dks_name, argv[0]);
+               printf("%s: %s -> %s\n", dvname, odks.dks_name, argv[1]);
                break;
        default:
                usage();
@@ -298,7 +288,7 @@
        int bits;
 
        if (ioctl(fd, DIOCGCACHE, &bits) == -1)
-               err(1, "%s: getcache", dvname);
+               err(EXIT_FAILURE, "%s: getcache", dvname);
 
        if ((bits & (DKCACHE_READ|DKCACHE_WRITE)) == 0)
                printf("%s: No caches enabled\n", dvname);
@@ -323,29 +313,29 @@
 {
        int bits;
 
-       if (argc > 2 || argc == 0)
+       if (argc > 3 || argc == 1)
                usage();
 
-       if (strcmp(argv[0], "none") == 0)
+       if (strcmp(argv[1], "none") == 0)
                bits = 0;
-       else if (strcmp(argv[0], "r") == 0)
+       else if (strcmp(argv[1], "r") == 0)
                bits = DKCACHE_READ;
-       else if (strcmp(argv[0], "w") == 0)
+       else if (strcmp(argv[1], "w") == 0)
                bits = DKCACHE_WRITE;
-       else if (strcmp(argv[0], "rw") == 0)
+       else if (strcmp(argv[1], "rw") == 0)
                bits = DKCACHE_READ|DKCACHE_WRITE;
        else
                usage();
 
-       if (argc == 2) {
-               if (strcmp(argv[1], "save") == 0)
+       if (argc == 3) {
+               if (strcmp(argv[2], "save") == 0)
                        bits |= DKCACHE_SAVE;
                else
                        usage();
        }
 
        if (ioctl(fd, DIOCSCACHE, &bits) == -1)
-               err(1, "%s: setcache", dvname);
+               err(EXIT_FAILURE, "%s: %s", dvname, argv[0]);
 }
 
 static void
@@ -354,12 +344,12 @@
        int force;
 
        switch (argc) {
-       case 0:
+       case 1:
                force = 0;
                break;
 
-       case 1:
-               if (strcmp(argv[0], "force") == 0)
+       case 2:
+               if (strcmp(argv[1], "force") == 0)
                        force = 1;
                else
                        usage();
@@ -370,7 +360,7 @@
        }
 
        if (ioctl(fd, DIOCCACHESYNC, &force) == -1)
-               err(1, "%s: sync cache", dvname);
+               err(EXIT_FAILURE, "%s: %s", dvname, argv[0]);
 }
 
 static void
@@ -379,17 +369,17 @@
        int keep;
        int yn;
 
-       if (argc != 1)
+       if (argc != 2)
                usage();
 
-       yn = yesno(argv[0]);
+       yn = yesno(argv[1]);
        if (yn < 0)
                usage();
 
        keep = yn == YES;
 
        if (ioctl(fd, DIOCKLABEL, &keep) == -1)
-               err(1, "%s: keep label", dvname);
+               err(EXIT_FAILURE, "%s: %s", dvname, argv[0]);
 }
 
 
@@ -405,10 +395,10 @@
        u_char *block;
        time_t tm;
 
-       if (argc != 1)
+       if (argc != 2)
                usage();
 
-       if (strcmp(argv[0], "list") == 0) {
+       if (strcmp(argv[1], "list") == 0) {
                /*
                 * Copy the list of kernel bad sectors out in chunks that fit
                 * into buffer[].  Updating dbsi_skip means we don't sit here
@@ -422,7 +412,7 @@
 
                do {
                        if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
-                               err(1, "%s: badsectors list", dvname);
+                               err(EXIT_FAILURE, "%s: badsectors list", dvname);
 
                        dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
                        for (count = dbsi.dbsi_copied; count > 0; count--) {
@@ -435,11 +425,11 @@
                        dbsi.dbsi_skip += dbsi.dbsi_copied;
                } while (dbsi.dbsi_left != 0);
 
-       } else if (strcmp(argv[0], "flush") == 0) {
+       } else if (strcmp(argv[1], "flush") == 0) {
                if (ioctl(fd, DIOCBSFLUSH) == -1)
-                       err(1, "%s: badsectors flush", dvname);
+                       err(EXIT_FAILURE, "%s: badsectors flush", dvname);
 
-       } else if (strcmp(argv[0], "retry") == 0) {
+       } else if (strcmp(argv[1], "retry") == 0) {
                /*
                 * Enforce use of raw device here because the block device
                 * causes access to blocks to be clustered in a larger group,
@@ -447,7 +437,7 @@
                 * are the cause of a problem.
                 */ 
                if (fstat(fd, &sb) == -1)
-                       err(1, "fstat");
+                       err(EXIT_FAILURE, "fstat");
 
                if (!S_ISCHR(sb.st_mode)) {
                        fprintf(stderr, "'badsector retry' must be used %s\n",
@@ -470,13 +460,13 @@
 
                do {
                        if (ioctl(fd, DIOCBSLIST, (caddr_t)&dbsi) == -1)
-                               err(1, "%s: badsectors list", dvname);
+                               err(EXIT_FAILURE, "%s: badsectors list", dvname);
 
                        dbs = (struct disk_badsectors *)dbsi.dbsi_buffer;
                        for (count = dbsi.dbsi_copied; count > 0; count--) {
                                dbs2 = malloc(sizeof *dbs2);



Home | Main Index | Thread Index | Old Index