From 10f7eba97a3d38134bb2316a70a4b77ee60ec1c5 Mon Sep 17 00:00:00 2001 From: christos Date: Sat, 3 Oct 2020 18:42:20 +0000 Subject: [PATCH] - centralize number parsing code - enable -g - KNF --- sbin/mount_nfs/mount_nfs.c | 102 +++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/sbin/mount_nfs/mount_nfs.c b/sbin/mount_nfs/mount_nfs.c index 420dca4fe420..4c636af59ee2 100644 --- a/sbin/mount_nfs/mount_nfs.c +++ b/sbin/mount_nfs/mount_nfs.c @@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\ #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 @@ __RCSID("$NetBSD: mount_nfs.c,v 1.73 2020/10/03 18:06:37 christos Exp $"); #include #include -#include +#include +#include /* XXX: redefines enums */ #include #include #include @@ -181,21 +182,36 @@ mount_nfs_dogetargs(struct nfs_args *nfsargsp, int mntflags, const char *spec) 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs_parseargs(int argc, char *argv[], 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 @@ mount_nfs(int argc, char *argv[]) } } 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 @@ shownfsargs(const struct nfs_args *nfsargsp) 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 @@ shownfsargs(const struct nfs_args *nfsargsp) 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); }