Source-Changes-HG archive

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

[src/trunk]: src/sbin/mount_nfs - centralize number parsing code



details:   https://anonhg.NetBSD.org/src/rev/ce92e0174daf
branches:  trunk
changeset: 976778:ce92e0174daf
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Oct 03 18:42:20 2020 +0000

description:
- centralize number parsing code
- enable -g
- KNF

diffstat:

 sbin/mount_nfs/mount_nfs.c |  102 ++++++++++++++++++++------------------------
 1 files changed, 46 insertions(+), 56 deletions(-)

diffs (254 lines):

diff -r 0f43bedc1b8e -r ce92e0174daf sbin/mount_nfs/mount_nfs.c
--- a/sbin/mount_nfs/mount_nfs.c        Sat Oct 03 18:35:21 2020 +0000
+++ b/sbin/mount_nfs/mount_nfs.c        Sat Oct 03 18:42:20 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mount_nfs.c,v 1.73 2020/10/03 18:06:37 christos Exp $  */
+/*     $NetBSD: mount_nfs.c,v 1.74 2020/10/03 18:42:20 christos Exp $  */
 
 /*
  * Copyright (c) 1992, 1993, 1994
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)mount_nfs.c        8.11 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: mount_nfs.c,v 1.73 2020/10/03 18:06:37 christos Exp $");
+__RCSID("$NetBSD: mount_nfs.c,v 1.74 2020/10/03 18:42:20 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -52,7 +52,8 @@
 #include <sys/stat.h>
 #include <syslog.h>
 
-#include <nfs/rpcv2.h>
+#include <rpc/rpc.h>
+#include <nfs/rpcv2.h> /* XXX: redefines enums */
 #include <nfs/nfsproto.h>
 #include <nfs/nfs.h>
 #include <nfs/nfsmount.h>
@@ -181,21 +182,36 @@
                nfsargsp->addrlen = sizeof(sa);
        } else {
                if ((tspec = strdup(spec)) == NULL) {
-                       err(1, "strdup");
+                       err(EXIT_FAILURE, "strdup");
                }
                if (!getnfsargs(tspec, nfsargsp)) {
-                       exit(1);
+                       exit(EXIT_FAILURE);
                }
                free(tspec);
        }
 }
 
+static int
+getnum(const char *s, int c)
+{
+       char *es;
+       long num = strtol(s, &es, 10);
+       if (*es || num <= 0 || num > INT_MAX)
+               errx(EXIT_FAILURE, "Illegal value `%s' for option -%c", s, c);
+       return (int)num;
+}
+
+static __dead void
+conflicting(void)
+{
+       errx(EXIT_FAILURE, "Conflicting version options");
+}
+
 void
 mount_nfs_parseargs(int argc, char *argv[],
        struct nfs_args *nfsargsp, int *mntflags,
        char *spec, char *name)
 {
-       char *p;
        int altflags, num;
        int c;
        mntoptparse_t mp;
@@ -210,12 +226,12 @@
                case '3':
                case 'q':
                        if (force2)
-                               errx(1, "conflicting version options");
+                               conflicting();
                        force3 = 1;
                        break;
                case '2':
                        if (force3)
-                               errx(1, "conflicting version options");
+                               conflicting();
                        force2 = 1;
                        nfsargsp->flags &= ~NFSMNT_NFSV3;
                        break;
@@ -223,10 +239,7 @@
                        nfsargsp->flags |= NFSMNT_NOAC;
                        break;
                case 'a':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num < 0)
-                               errx(1, "illegal -a value -- %s", optarg);
-                       nfsargsp->readahead = num;
+                       nfsargsp->readahead = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_READAHEAD;
                        break;
                case 'b':
@@ -239,30 +252,20 @@
                        nfsargsp->flags &= ~NFSMNT_NOCONN;
                        break;
                case 'D':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -D value -- %s", optarg);
-                       nfsargsp->deadthresh = num;
+                       nfsargsp->deadthresh = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_DEADTHRESH;
                        break;
                case 'd':
                        nfsargsp->flags |= NFSMNT_DUMBTIMR;
                        break;
-#if 0 /* XXXX */
                case 'g':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -g value -- %s", optarg);
+                       num = getnum(optarg, c);
                        set_rpc_maxgrouplist(num);
                        nfsargsp->maxgrouplist = num;
                        nfsargsp->flags |= NFSMNT_MAXGRPS;
                        break;
-#endif
                case 'I':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -I value -- %s", optarg);
-                       nfsargsp->readdirsize = num;
+                       nfsargsp->readdirsize = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_READDIRSIZE;
                        break;
                case 'i':
@@ -277,7 +280,7 @@
                case 'o':
                        mp = getmntopts(optarg, mopts, mntflags, &altflags);
                        if (mp == NULL)
-                               err(1, "getmntopts");
+                               err(EXIT_FAILURE, "getmntopts");
                        if (altflags & ALTF_BG)
                                opflags |= BGRND;
                        if (altflags & ALTF_CONN)
@@ -290,12 +293,12 @@
                                nfsargsp->flags |= NFSMNT_NOAC;
                        if (altflags & (ALTF_NFSV3|ALTF_NQNFS)) {
                                if (force2)
-                                       errx(1, "conflicting version options");
+                                       conflicting();
                                force3 = 1;
                        }
                        if (altflags & ALTF_NFSV2) {
                                if (force3)
-                                       errx(1, "conflicting version options");
+                                       conflicting();
                                force2 = 1;
                                nfsargsp->flags &= ~NFSMNT_NFSV3;
                        }
@@ -318,7 +321,7 @@
                                nfsargsp->sotype = SOCK_STREAM;
                        }
                        if (altflags & ALTF_PORT) {
-                               port = getmntoptnum(mp, "port");
+                               port = (int)getmntoptnum(mp, "port");
                        }
                        if (altflags & ALTF_RSIZE) {
                                nfsargsp->rsize =
@@ -378,16 +381,10 @@
                        nfsargsp->flags &= ~NFSMNT_RESVPORT;
                        break;
                case 'R':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -R value -- %s", optarg);
-                       retrycnt = num;
+                       retrycnt = getnum(optarg, c);
                        break;
                case 'r':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -r value -- %s", optarg);
-                       nfsargsp->rsize = num;
+                       nfsargsp->rsize = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_RSIZE;
                        break;
                case 's':
@@ -397,24 +394,15 @@
                        nfsargsp->sotype = SOCK_STREAM;
                        break;
                case 't':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -t value -- %s", optarg);
-                       nfsargsp->timeo = num;
+                       nfsargsp->timeo = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_TIMEO;
                        break;
                case 'w':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -w value -- %s", optarg);
-                       nfsargsp->wsize = num;
+                       nfsargsp->wsize = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_WSIZE;
                        break;
                case 'x':
-                       num = strtol(optarg, &p, 10);
-                       if (*p || num <= 0)
-                               errx(1, "illegal -x value -- %s", optarg);
-                       nfsargsp->retrans = num;
+                       nfsargsp->retrans = getnum(optarg, c);
                        nfsargsp->flags |= NFSMNT_RETRANS;
                        break;
                case 'X':
@@ -467,13 +455,13 @@
                }
        }
        if (retval == -1)
-               err(1, "%s on %s", spec, name);
+               err(EXIT_FAILURE, "%s on %s", spec, name);
        if (mntflags & MNT_GETARGS) {
                shownfsargs(&args);
-               return (0);
+               return EXIT_SUCCESS;
        }
                
-       exit(0);
+       exit(EXIT_SUCCESS);
 }
 
 static void
@@ -483,9 +471,11 @@
        char host[NI_MAXHOST], serv[NI_MAXSERV];
        int error;
 
-       (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
+       (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS,
+           (uint64_t)nfsargsp->flags);
        if (nfsargsp->addr != NULL) {
-               error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
+               error = getnameinfo(nfsargsp->addr,
+                   (socklen_t)nfsargsp->addrlen, host,
                    sizeof(host), serv, sizeof(serv),
                    NI_NUMERICHOST | NI_NUMERICSERV);
                if (error != 0)
@@ -518,11 +508,11 @@
 static void
 usage(void)
 {
-       (void)fprintf(stderr, "usage: %s %s\n%s\n%s\n%s\n%s\n", getprogname(),
+       (void)fprintf(stderr, "Usage: %s %s\n%s\n%s\n%s\n%s\n", getprogname(),
 "[-23bCcdilPpqsTUuX] [-a maxreadahead] [-D deadthresh]",
 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
 "\t[-w writesize] [-x retrans]",
 "\trhost:path node");
-       exit(1);
+       exit(EXIT_FAILURE);
 }



Home | Main Index | Thread Index | Old Index