Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/makefs - Use strsuftoull() from dd(1) to parse numb...



details:   https://anonhg.NetBSD.org/src/rev/0ccd4179040c
branches:  trunk
changeset: 518134:0ccd4179040c
user:      lukem <lukem%NetBSD.org@localhost>
date:      Sun Nov 25 11:22:09 2001 +0000

description:
- Use strsuftoull() from dd(1) to parse numbers, rather than home grown
  version. The new version supports products, 'g' (GB) and 't' (TB)
  suffices, and `b' now means `blocks' instead of `bytes'.
  Functionality requested by perry.
- debug is now a u_int instead of an int.
- Ensure that various numbers have sane upper limits (e.g, 99 for %, etc)

diffstat:

 usr.sbin/makefs/Makefile |   10 ++-
 usr.sbin/makefs/makefs.8 |   23 ++++++---
 usr.sbin/makefs/makefs.c |  102 +++++++++++-----------------------------------
 usr.sbin/makefs/makefs.h |    7 +-
 4 files changed, 49 insertions(+), 93 deletions(-)

diffs (294 lines):

diff -r a5538af678ae -r 0ccd4179040c usr.sbin/makefs/Makefile
--- a/usr.sbin/makefs/Makefile  Sun Nov 25 10:54:47 2001 +0000
+++ b/usr.sbin/makefs/Makefile  Sun Nov 25 11:22:09 2001 +0000
@@ -1,23 +1,25 @@
-#      $NetBSD: Makefile,v 1.3 2001/11/22 02:47:25 lukem Exp $
+#      $NetBSD: Makefile,v 1.4 2001/11/25 11:22:09 lukem Exp $
 #
 
 PROG=  makefs
 SRCS=  makefs.c walk.c \
        ffs.c mkfs.c buf.c \
-       misc.c spec.c pack_dev.c stat_flags.c \
+       misc.c spec.c pack_dev.c stat_flags.c strsuftoull.c \
        ffs_alloc.c ffs_balloc.c ffs_bswap.c ffs_subr.c ffs_tables.c ufs_bmap.c 
 MAN=   makefs.8
 DPADD+=        ${LIBUTIL}
 LDADD+=        -lutil
 
+DDSRC=         ${.CURDIR}/../../bin/dd
 LSSRC=         ${.CURDIR}/../../bin/ls
 MKNODSRC=      ${.CURDIR}/../../sbin/mknod
 MTREESRC=      ${.CURDIR}/../../usr.sbin/mtree
 SYSSRC=                ${.CURDIR}/../../sys
 
-CPPFLAGS+=     -I${.CURDIR} -I${SYSSRC} -I${LSSRC} -I${MKNODSRC} -I${MTREESRC}
+CPPFLAGS+=     -I${.CURDIR} -I${SYSSRC} \
+               -I${LSSRC} -I${MKNODSRC} -I${MTREESRC} -I${DDSRC} 
 .PATH:         ${.CURDIR}/ffs ${SYSSRC}/ufs/ffs \
-               ${LSSRC} ${MKNODSRC} ${MTREESRC}
+               ${LSSRC} ${MKNODSRC} ${MTREESRC} ${DDSRC} 
 
 WARNS?=        2
 
diff -r a5538af678ae -r 0ccd4179040c usr.sbin/makefs/makefs.8
--- a/usr.sbin/makefs/makefs.8  Sun Nov 25 10:54:47 2001 +0000
+++ b/usr.sbin/makefs/makefs.8  Sun Nov 25 11:22:09 2001 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: makefs.8,v 1.2 2001/11/02 03:12:48 lukem Exp $
+.\"    $NetBSD: makefs.8,v 1.3 2001/11/25 11:22:09 lukem Exp $
 .\"
 .\" Copyright (c) 2001 Wasabi Systems, Inc.
 .\" All rights reserved.
@@ -33,7 +33,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd October 26, 2001
+.Dd November 25, 2001
 .Dt MAKEFS 8
 .Os
 .Sh NAME
@@ -189,17 +189,24 @@
 isn't provided, the current file flags will be used.
 .El
 .Pp
-An optional suffix may be provided for numeric size arguments,
-which changes the intrepretation of the argument as follows:
+Where sizes are specified, a decimal number of bytes is expected.
+Two or more numbers may be separated by an
+.Dq x
+to indicate a product.
+Each number may have one of the following optional suffices:
 .Bl -tag -width 3n -offset indent -compact
 .It b
-Causes no modification. (Default; optional)
+Block; multiply by 512
 .It k
-Kilo; multiply the argument by 1024
+Kilo; multiply by 1024 (1 KB)
 .It m
-Mega; multiply the argument by 1048576
+Mega; multiply by 1048576 (1 MB)
 .It g
-Giga; multiply the argument by 1073741824
+Giga; multiply by 1073741824 (1 GB)
+.It t
+Tera; multiply by 1099511627776 (1 TB)
+.It w
+Word; multiply by the number of bytes in an integer
 .El
 .\"
 .\"
diff -r a5538af678ae -r 0ccd4179040c usr.sbin/makefs/makefs.c
--- a/usr.sbin/makefs/makefs.c  Sun Nov 25 10:54:47 2001 +0000
+++ b/usr.sbin/makefs/makefs.c  Sun Nov 25 11:22:09 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makefs.c,v 1.5 2001/11/08 12:24:12 simonb Exp $        */
+/*     $NetBSD: makefs.c,v 1.6 2001/11/25 11:22:09 lukem Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifndef __lint
-__RCSID("$NetBSD: makefs.c,v 1.5 2001/11/08 12:24:12 simonb Exp $");
+__RCSID("$NetBSD: makefs.c,v 1.6 2001/11/25 11:22:09 lukem Exp $");
 #endif /* !__lint */
 
 #include <assert.h>
@@ -51,6 +51,7 @@
 #include <unistd.h>
 
 #include "makefs.h"
+#include "strsuftoull.h"
 
 
 /*
@@ -68,12 +69,11 @@
        { NULL  },
 };
 
-int            debug;
+u_int          debug;
 struct timespec        start_time;
 
 
 static fstype_t *get_fstype(const char *);
-static long long strsuftoll(const char *, const char *, long long, long long);
 static void    usage(void);
 int            main(int, char *[]);
 
@@ -122,17 +122,18 @@
                        if (optarg[len] == '%') {
                                optarg[len] = '\0';
                                fsoptions.freeblockpc =
-                                   strsuftoll("free block percentage",
-                                   optarg, 0, LLONG_MAX);
+                                   strsuftoull("free block percentage",
+                                       optarg, 0, 99);
                        } else {
-                               fsoptions.freeblocks = strsuftoll("free blocks",
-                                   optarg, 0, LLONG_MAX);
+                               fsoptions.freeblocks =
+                                   strsuftoull("free blocks",
+                                       optarg, 0, LLONG_MAX);
                        }
                        break;
 
                case 'd':
-                       debug = (int)strsuftoll("debug mask", optarg,
-                           0, LLONG_MAX);
+                       debug =
+                           (int)strsuftoull("debug mask", optarg, 0, UINT_MAX);
                        break;
 
                case 'f':
@@ -140,11 +141,12 @@
                        if (optarg[len] == '%') {
                                optarg[len] = '\0';
                                fsoptions.freefilepc =
-                                   strsuftoll("free file percentage",
-                                   optarg, 0, LLONG_MAX);
+                                   strsuftoull("free file percentage",
+                                       optarg, 0, 99);
                        } else {
-                               fsoptions.freefiles = strsuftoll("free files",
-                                   optarg, 0, LLONG_MAX);
+                               fsoptions.freefiles =
+                                   strsuftoull("free files",
+                                       optarg, 0, LLONG_MAX);
                        }
                        break;
 
@@ -153,13 +155,13 @@
                        break;
 
                case 'M':
-                       fsoptions.minsize = strsuftoll("minimum size",
-                           optarg, 1LL, LLONG_MAX);
+                       fsoptions.minsize =
+                           strsuftoull("minimum size", optarg, 1LL, LLONG_MAX);
                        break;
 
                case 'm':
-                       fsoptions.maxsize = strsuftoll("maximum size",
-                           optarg, 1LL, LLONG_MAX);
+                       fsoptions.maxsize =
+                           strsuftoull("maximum size", optarg, 1LL, LLONG_MAX);
                        break;
                        
                case 'o':
@@ -177,12 +179,13 @@
 
                case 's':
                        fsoptions.minsize = fsoptions.maxsize =
-                           strsuftoll("size", optarg, 1LL, LLONG_MAX);
+                           strsuftoull("size", optarg, 1LL, LLONG_MAX);
                        break;
 
                case 'S':
-                       fsoptions.sectorsize = strsuftoll("sector size",
-                           optarg, 1LL, LLONG_MAX);
+                       fsoptions.sectorsize =
+                           (int)strsuftoull("sector size", optarg,
+                               1LL, INT_MAX);
                        break;
 
                case 't':
@@ -244,7 +247,7 @@
        for (i = 0; options[i].name != NULL; i++) {
                if (strcmp(options[i].name, var) != 0)
                        continue;
-               *options[i].value = (int)strsuftoll(options[i].desc, val,
+               *options[i].value = (int)strsuftoull(options[i].desc, val,
                    options[i].minimum, options[i].maximum);
                return (1);
        }
@@ -264,61 +267,6 @@
        return (NULL);
 }
 
-
-static long long
-strsuftoll(const char *desc, const char *arg, long long min, long long max)
-{
-       long long       result;
-       char            *ep;
-
-       assert(desc != NULL);
-       assert(arg != NULL);
-
-       errno = 0;
-       result = strtoll(arg, &ep, 0);
-       if ((result == LLONG_MIN || result == LLONG_MAX) && errno == ERANGE) {
-               warn("%s `%s'", desc, arg);
-               usage();
-       }
-       if (ep[0] != '\0' && ep[1] != '\0') {
-               warnx("`%s' is not a valid number for %s.", optarg, desc);
-               usage();
-       }
-       switch (tolower((unsigned char)ep[0])) {
-       case '\0':
-       case 'b':
-               break;
-       case 'k':
-               result <<= 10;
-               break;
-       case 'm':
-               result <<= 20;
-               break;
-       case 'g':
-               result <<= 30;
-               break;
-       default:
-               warnx("`%s' is not a valid suffix for %s.", ep, desc);
-               usage();
-       }
-
-       if (result < min) {
-               warnx("%s `%s' (%lld) is less than %lld.",
-                   desc, optarg, result, min);
-               usage();
-       }
-       if (result > max) {
-               warnx("%s `%s' (%lld) is greater than %lld.",
-                   desc, optarg, result, max);
-               usage();
-       }
-       if (debug & DEBUG_STRSUFTOLL)
-               printf("strsuftoll: got %lld for %s %s\n",
-                   result, desc, arg);
-       return (result);
-}
-
-
 static void
 usage(void)
 {
diff -r a5538af678ae -r 0ccd4179040c usr.sbin/makefs/makefs.h
--- a/usr.sbin/makefs/makefs.h  Sun Nov 25 10:54:47 2001 +0000
+++ b/usr.sbin/makefs/makefs.h  Sun Nov 25 11:22:09 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makefs.h,v 1.2 2001/11/02 03:12:48 lukem Exp $ */
+/*     $NetBSD: makefs.h,v 1.3 2001/11/25 11:22:09 lukem Exp $ */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -151,12 +151,11 @@
 
 
 
-extern int             debug;
+extern u_int           debug;
 extern struct timespec start_time;
 
 #define        DEBUG_TIME                      0x00000001
-               /* debug bits 1..2 reserved at this time */
-#define        DEBUG_STRSUFTOLL                0x00000008
+               /* debug bits 1..3 unused at this time */
 #define        DEBUG_WALK_DIR                  0x00000010
 #define        DEBUG_WALK_DIR_NODE             0x00000020
 #define        DEBUG_WALK_DIR_LINKCHECK        0x00000040



Home | Main Index | Thread Index | Old Index