git: 752a340419a7 - main - quot: Use getopt(3) and show usage() if no arguments

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sat, 03 Feb 2024 01:36:22 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=752a340419a7e2c1d3b624199f35435aecaf5828

commit 752a340419a7e2c1d3b624199f35435aecaf5828
Author:     Ricardo Branco <rbranco@suse.de>
AuthorDate: 2024-02-03 00:12:22 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-02-03 01:34:59 +0000

    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 0338457f6aeb..81abe28b41d5 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 99aff61ae934..7ca8110bef76 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++) {