changeset 1732:9adb0ccb617e

Make find accept numeric uid/gid, and simplify makedevs using the new infrastructure.
author Rob Landley <rob@landley.net>
date Thu, 12 Mar 2015 14:21:33 -0500
parents 5a00bc5e1c0f
children 0d9bad339158
files toys/other/makedevs.c toys/posix/find.c
diffstat 2 files changed, 5 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/makedevs.c	Thu Mar 12 14:20:10 2015 -0500
+++ b/toys/other/makedevs.c	Thu Mar 12 14:21:33 2015 -0500
@@ -41,7 +41,7 @@
 
 void makedevs_main()
 {
-  int value, fd = 0, line_no, i;
+  int fd = 0, line_no, i;
   char *line = NULL;
 
   // Open file and chdir, verbosely
@@ -78,27 +78,8 @@
       continue;
     } else mode |= (mode_t[]){S_IFIFO, S_IFCHR, S_IFBLK, 0, 0}[i];
 
-    if (*user) {
-      struct passwd *usr;
-
-      if (!(usr = getpwnam(user)) && isdigit(*user)) {
-        sscanf(user, "%u", &value);
-        usr = xgetpwuid(value);
-      }
-      if (!usr) error_exit("bad user '%s'", user);
-      uid = usr->pw_uid;
-    } else uid = getuid();
-
-    if (*group) {
-      struct group *grp;
-
-      if (!(grp = getgrnam(group)) && isdigit(*group)) {
-        sscanf (group, "%u", &value);
-        grp = getgrgid(value);
-      }
-      if (!grp) error_exit("bad group '%s'", group);
-      gid = grp->gr_gid;
-    } else gid = getgid();
+    uid = *user ? xgetpwnamid(user)->pw_uid : getuid();
+    gid = *group ? xgetgrnamid(group)->gr_gid : getgid();
 
     while (*node == '/') node++; // using relative path
 
--- a/toys/posix/find.c	Thu Mar 12 14:20:10 2015 -0500
+++ b/toys/posix/find.c	Thu Mar 12 14:21:33 2015 -0500
@@ -371,8 +371,8 @@
           udl = xmalloc(sizeof(*udl));
           dlist_add_nomalloc(&TT.argdata, (void *)udl);
 
-          if (*s == 'u') udl->u.uid = xgetpwnam(ss[1])->pw_uid;
-          else if (*s == 'g') udl->u.gid = xgetgrnam(ss[1])->gr_gid;
+          if (*s == 'u') udl->u.uid = xgetpwnamid(ss[1])->pw_uid;
+          else if (*s == 'g') udl->u.gid = xgetgrnamid(ss[1])->gr_gid;
           else {
             struct stat st;