Subject: Re: bin/11695: chown no longer reports "invalid user name" when appropriate
To: None <simonb@wasabisystems.com>
From: Mike Pelley <mike@pelley.com>
List: netbsd-bugs
Date: 12/11/2000 20:40:00
Hello again,

> this is what user_from_uid() etc are.  see pwcache(3).

That's what I had in mind (from Simon's original reply) - I think the
following patch is good.  It's basically my original patch with
uid_from_user in place of getpwnam.  I have not used uid_from_user
before but it seems to do the right thing.

Mike.


Index: chown.c
===================================================================
RCS file: /usr/cvsroot/basesrc/usr.sbin/chown/chown.c,v
retrieving revision 1.24
diff -u -r1.24 chown.c
--- chown.c	2000/08/25 06:25:59	1.24
+++ chown.c	2000/12/12 01:32:00
@@ -64,7 +64,7 @@
 #include <unistd.h>
 
 static void	a_gid __P((const char *));
-static int	a_uid __P((const char *));
+static void	a_uid __P((const char *));
 static id_t	id __P((const char *, const char *));
 	int	main __P((int, char **));
 static void	usage __P((void));
@@ -153,13 +153,13 @@
 		}
 #ifdef SUPPORT_DOT
 		else if ((cp = strrchr(*argv, '.')) != NULL) {
-			if (a_uid(*argv) == -1) {
+			if (uid_from_user(*argv, &uid) == -1) {
 				*cp++ = '\0';
 				a_gid(cp);
 			}
 		}
 #endif
-		(void) a_uid(*argv);
+		a_uid(*argv);
 	} else
 		a_gid(*argv);
 
@@ -228,26 +228,24 @@
 
 	if (*s == '\0')			/* Argument was "uid[:.]". */
 		return;
-	gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;
+	gr = getgrnam(s);
+	if (gr == NULL)
+		gid = id(s, "group");
+	else
+		gid = gr->gr_gid;
+	return;
 }
 
-static int
+static void
 a_uid(s)
 	const char *s;
 {
-	struct passwd *pw;
-
 	if (*s == '\0')			/* Argument was "[:.]gid". */
-		return 0;
-	pw = getpwnam(s);
-	if (pw != NULL) {
-		uid = pw->pw_uid;
-		return 0;
+		return;
+	if (uid_from_user(s, &uid) == -1) {
+		uid = id(s, "user");
 	}
-	if (isalpha(*s))
-		return -1;
-	uid = id(s, "user");
-	return 0;
+	return;
 }
 
 static id_t