Source-Changes-HG archive

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

[src/trunk]: src/sbin/disklabel - simplify getinput.



details:   https://anonhg.NetBSD.org/src/rev/04042ee1d65d
branches:  trunk
changeset: 784043:04042ee1d65d
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Jan 15 23:52:48 2013 +0000

description:
- simplify getinput.
- add adjust command.

diffstat:

 sbin/disklabel/interact.c |  138 +++++++++++++++++++++++++++-------------------
 1 files changed, 81 insertions(+), 57 deletions(-)

diffs (truncated from 372 to 300 lines):

diff -r a536b150aea1 -r 04042ee1d65d sbin/disklabel/interact.c
--- a/sbin/disklabel/interact.c Tue Jan 15 22:42:14 2013 +0000
+++ b/sbin/disklabel/interact.c Tue Jan 15 23:52:48 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $        */
+/*     $NetBSD: interact.c,v 1.36 2013/01/15 23:52:48 christos Exp $   */
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: interact.c,v 1.35 2011/01/06 21:39:01 apb Exp $");
+__RCSID("$NetBSD: interact.c,v 1.36 2013/01/15 23:52:48 christos Exp $");
 #endif /* lint */
 
 #include <sys/param.h>
@@ -40,7 +40,9 @@
 #include <err.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
 #include <stdlib.h>
+#include <sys/ioctl.h>
 
 #if HAVE_NBTOOL_CONFIG_H
 #define        getmaxpartitions()      MAXPARTITIONS
@@ -53,6 +55,7 @@
 #include "extern.h"
 
 static void    cmd_help(struct disklabel *, char *, int);
+static void    cmd_adjust(struct disklabel *, char *, int);
 static void    cmd_chain(struct disklabel *, char *, int);
 static void    cmd_print(struct disklabel *, char *, int);
 static void    cmd_printall(struct disklabel *, char *, int);
@@ -63,7 +66,7 @@
 static void    cmd_name(struct disklabel *, char *, int);
 static void    cmd_listfstypes(struct disklabel *, char *, int);
 static int     runcmd(struct disklabel *, char *, int);
-static int     getinput(const char *, const char *, const char *, char *);
+static int     getinput(char *, const char *, ...) __printflike(2, 3);
 static int     alphacmp(const void *, const void *);
 static void    defnum(struct disklabel *, char *, uint32_t);
 static void    dumpnames(const char *, const char * const *, size_t);
@@ -78,6 +81,7 @@
        const char *help;
 } cmds[] = {
        { "?",  cmd_help,       "print this menu" },
+       { "A",  cmd_adjust,     "adjust the label size to the max disk size" },
        { "C",  cmd_chain,      "make partitions contiguous" },
        { "E",  cmd_printall,   "print disk label and current partition table"},
        { "I",  cmd_info,       "change label information" },
@@ -91,7 +95,6 @@
 };
        
        
-
 static void
 cmd_help(struct disklabel *lp, char *s, int fd)
 {
@@ -105,13 +108,39 @@
 
 
 static void
+cmd_adjust(struct disklabel *lp, char *s, int fd)
+{
+       struct disklabel dl;
+
+       if (ioctl(fd, DIOCGDEFLABEL, &dl) == -1) {
+               warn("Cannot get default label");
+               return;
+       }
+
+       if (dl.d_secperunit != lp->d_secperunit) {
+               char line[BUFSIZ];
+               int i = getinput(line, "Adjust disklabel sector from %" PRIu32
+                   " to %" PRIu32 " [n]? ", lp->d_secperunit, dl.d_secperunit);
+               if (i <= 0)
+                       return;
+               if (line[0] != 'Y' && line[0] != 'y')
+                       return;
+               lp->d_secperunit = dl.d_secperunit;
+               return;
+       } 
+
+       printf("Already at %" PRIu32 " sectors\n", dl.d_secperunit);
+       return;
+}
+
+static void
 cmd_chain(struct disklabel *lp, char *s, int fd)
 {
        int     i;
        char    line[BUFSIZ];
 
-       i = getinput(":", "Automatically adjust partitions",
-           chaining ? "yes" : "no", line);
+       i = getinput(line, "Automatically adjust partitions [%s]? ",
+           chaining ? "yes" : "no");
        if (i <= 0)
                return;
 
@@ -150,7 +179,6 @@
 cmd_info(struct disklabel *lp, char *s, int fd)
 {
        char    line[BUFSIZ];
-       char    def[BUFSIZ];
        int     v, i;
        u_int32_t u;
 
@@ -162,8 +190,7 @@
                i = lp->d_type;
                if (i < 0 || i >= DKMAXTYPES)
                        i = 0;
-               snprintf(def, sizeof(def), "%s", dktypenames[i]);
-               i = getinput(":", "Disk type [?]", def, line);
+               i = getinput(line, "Disk type [%s]: ", dktypenames[i]);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -190,9 +217,8 @@
        }
 
        /* d_typename */
-       snprintf(def, sizeof(def), "%.*s",
+       i = getinput(line, "Disk name [%.*s]: ", 
            (int) sizeof(lp->d_typename), lp->d_typename);
-       i = getinput(":", "Disk name", def, line);
        if (i == -1)
                return;
        else if (i == 1)
@@ -203,8 +229,8 @@
 
        /* d_npartitions */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu16, lp->d_npartitions);
-               i = getinput(":", "Number of partitions", def, line);
+               i = getinput(line, "Number of partitions [%" PRIu16 "]: ",
+                   lp->d_npartitions);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -219,8 +245,8 @@
 
        /* d_secsize */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_secsize);
-               i = getinput(":", "Sector size (bytes)", def, line);
+               i = getinput(line, "Sector size (bytes) [%" PRIu32 "]: ",
+                   lp->d_secsize);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -235,8 +261,8 @@
 
        /* d_nsectors */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_nsectors);
-               i = getinput(":", "Number of sectors per track", def, line);
+               i = getinput(line, "Number of sectors per track [%" PRIu32
+                   "]: ", lp->d_nsectors);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -251,8 +277,8 @@
 
        /* d_ntracks */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_ntracks);
-               i = getinput(":", "Number of tracks per cylinder", def, line);
+               i = getinput(line, "Number of tracks per cylinder [%" PRIu32
+                   "]: ", lp->d_ntracks);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -267,8 +293,8 @@
 
        /* d_secpercyl */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_secpercyl);
-               i = getinput(":", "Number of sectors/cylinder", def, line);
+               i = getinput(line, "Number of sectors/cylinder [%" PRIu32 "]: ",
+                   lp->d_secpercyl);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -284,8 +310,8 @@
 
        /* d_ncylinders */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_ncylinders);
-               i = getinput(":", "Total number of cylinders", def, line);
+               i = getinput(line, "Total number of cylinders [%" PRIu32 "]: ",
+                   lp->d_ncylinders);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -300,8 +326,8 @@
 
        /* d_secperunit */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_secperunit);
-               i = getinput(":", "Total number of sectors", def, line);
+               i = getinput(line, "Total number of sectors [%" PRIu32 "]: ",
+                   lp->d_secperunit);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -318,8 +344,8 @@
 
        /* d_interleave */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu16, lp->d_interleave);
-               i = getinput(":", "Hardware sectors interleave", def, line);
+               i = getinput(line, "Hardware sectors interleave [%" PRIu16
+                   "]: ", lp->d_interleave);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -334,8 +360,8 @@
 
        /* d_trackskew */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu16, lp->d_trackskew);
-               i = getinput(":", "Sector 0 skew, per track", def, line);
+               i = getinput(line, "Sector 0 skew, per track [%" PRIu16 "]: ",
+                   lp->d_trackskew);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -350,8 +376,8 @@
 
        /* d_cylskew */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu16, lp->d_cylskew);
-               i = getinput(":", "Sector 0 skew, per cylinder", def, line);
+               i = getinput(line, "Sector 0 skew, per cylinder [%" PRIu16
+                   "]: ", lp->d_cylskew);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -366,8 +392,8 @@
 
        /* d_headswitch */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_headswitch);
-               i = getinput(":", "Head switch time (usec)", def, line);
+               i = getinput(line, "Head switch time (usec) [%" PRIu32 "]: ",
+                   lp->d_headswitch);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -382,8 +408,8 @@
 
        /* d_trkseek */
        for (;;) {
-               snprintf(def, sizeof(def), "%" PRIu32, lp->d_trkseek);
-               i = getinput(":", "Track seek time (usec)", def, line);
+               i = getinput(line, "Track seek time (usec) [%" PRIu32 "]:",
+                   lp->d_trkseek);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -402,12 +428,10 @@
 cmd_name(struct disklabel *lp, char *s, int fd)
 {
        char    line[BUFSIZ];
-       char    def[BUFSIZ];
        int     i;
 
-       snprintf(def, sizeof(def), "%.*s",
+       i = getinput(line, "Label name [%.*s]: ",
            (int) sizeof(lp->d_packname), lp->d_packname);
-       i = getinput(":", "Label name", def, line);
        if (i <= 0)
                return;
        (void) strncpy(lp->d_packname, line, sizeof(lp->d_packname));
@@ -420,7 +444,8 @@
        int     i;
        char    line[BUFSIZ];
 
-       i = getinput(":", "Rounding", rounding ? "cylinders" : "sectors", line);
+       i = getinput(line, "Rounding [%s]: ", rounding ? "cylinders" :
+           "sectors");
        if (i <= 0)
                return;
 
@@ -461,8 +486,7 @@
                i = p->p_fstype;
                if (i < 0 || i >= FSMAXTYPES)
                        i = 0;
-               snprintf(def, sizeof(def), "%s", fstypenames[i]);
-               i = getinput(":", "Filesystem type [?]", def, line);
+               i = getinput(line, "Filesystem type [%s]: ", fstypenames[i]);
                if (i == -1)
                        return;
                else if (i == 0)
@@ -484,9 +508,8 @@
        }



Home | Main Index | Thread Index | Old Index