[hackers] [PATCH v2 2/2][sbase] chgrp: parse gid if operand is not group name

From: Richard Ipsum <richardipsum_AT_fastmail.co.uk>
Date: Wed, 3 Jul 2019 17:32:43 +0100

---
 chgrp.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/chgrp.c b/chgrp.c
index 7ee3553..ddced31 100644
--- a/chgrp.c
+++ b/chgrp.c
_AT_@ -44,6 +44,7 @@ int
 main(int argc, char *argv[])
 {
 	struct group *gr;
+	char *group;
 	struct recursor r = { .fn = chgrp, .hist = NULL, .depth = 0, .maxdepth = 1,
 	                      .follow = 'P', .flags = 0 };
 
_AT_@ -66,14 +67,21 @@ main(int argc, char *argv[])
 	if (argc < 2)
 		usage();
 
-	errno = 0;
-	if (!(gr = getgrnam(argv[0]))) {
-		if (errno)
-			eprintf("getgrnam %s:", argv[0]);
-		else
-			eprintf("getgrnam %s: no such group\n", argv[0]);
+	group = argv[0];
+	if (group && *group) {
+		errno = 0;
+		gr = getgrnam(group);
+		if (gr) {
+			gid = gr->gr_gid;
+		} else {
+			if (errno)
+				eprintf("getgrnam %s:", group);
+			gid = estrtonum(group, 0, UINT_MAX);
+		}
 	}
-	gid = gr->gr_gid;
+
+	if (gid == -1)
+		usage();
 
 	for (argc--, argv++; *argv; argc--, argv++)
 		recurse(*argv, NULL, &r);
-- 
2.21.0
Received on Wed Jul 03 2019 - 18:32:43 CEST

This archive was generated by hypermail 2.3.0 : Wed Jul 03 2019 - 18:36:24 CEST