From 752a340419a7e2c1d3b624199f35435aecaf5828 Mon Sep 17 00:00:00 2001 From: Ricardo Branco Date: Fri, 2 Feb 2024 17:12:22 -0700 Subject: [PATCH] quot: Use getopt(3) and show usage() if no arguments Also update the man page and usage to be a little more accurate with the -a flag. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/983 --- usr.sbin/quot/quot.8 | 4 +-- usr.sbin/quot/quot.c | 63 ++++++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/usr.sbin/quot/quot.8 b/usr.sbin/quot/quot.8 index 0338457f6ae..81abe28b41d 100644 --- a/usr.sbin/quot/quot.8 +++ b/usr.sbin/quot/quot.8 @@ -35,8 +35,8 @@ .Nd display disk space occupied by each user .Sh SYNOPSIS .Nm -.Op Fl acfhknv -.Op Ar filesystem ... +.Op Fl cfhknv +.Op Fl a | Ar filesystem ... .Sh DESCRIPTION The .Nm diff --git a/usr.sbin/quot/quot.c b/usr.sbin/quot/quot.c index 99aff61ae93..7ca8110bef7 100644 --- a/usr.sbin/quot/quot.c +++ b/usr.sbin/quot/quot.c @@ -526,9 +526,9 @@ static void usage(void) { #ifdef COMPAT - fprintf(stderr,"usage: quot [-nfcvha] [filesystem ...]\n"); + fprintf(stderr, "usage: quot [-cfhnv] [-a | filesystem ...]\n"); #else /* COMPAT */ - fprintf(stderr,"usage: quot [-acfhknv] [filesystem ...]\n"); + fprintf(stderr, "usage: quot [-cfhknv] [-a | filesystem ...]\n"); #endif /* COMPAT */ exit(1); } @@ -575,42 +575,47 @@ main(int argc, char *argv[]) struct statfs *mp; struct fstab *fs; int cnt; + int ch; func = douser; #ifndef COMPAT header = getbsize(&headerlen,&blocksize); #endif - while (--argc > 0 && **++argv == '-') { - while (*++*argv) { - switch (**argv) { - case 'n': - func = donames; - break; - case 'c': - func = dofsizes; - break; - case 'a': - all = 1; - break; - case 'f': - count = 1; - break; - case 'h': - estimate = 1; - break; + while ((ch = getopt(argc, argv, "acfhknv")) != -1) { + switch (ch) { + case 'a': + all = 1; + break; + case 'c': + func = dofsizes; + break; + case 'f': + count = 1; + break; + case 'h': + estimate = 1; + break; #ifndef COMPAT - case 'k': - blocksize = 1024; - break; + case 'k': + blocksize = 1024; + break; #endif /* COMPAT */ - case 'v': - unused = 1; - break; - default: - usage(); - } + case 'n': + func = donames; + break; + case 'v': + unused = 1; + break; + default: + usage(); } } + argc -= optind; + argv += optind; + + if ((argc == 0 && !all) || (all && argc)) + usage(); + if (all) { cnt = getmntinfo(&mp,MNT_NOWAIT); for (; --cnt >= 0; mp++) {