From b8140d18800e7094cdacb0a61526f46181dc132d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 12 Mar 2015 11:11:08 -0500 Subject: Factor out xgetgrnamid() and xgetpwnamid() into xwrap.c. --- toys/posix/chgrp.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) (limited to 'toys/posix') diff --git a/toys/posix/chgrp.c b/toys/posix/chgrp.c index 556b0f19..fb82adbd 100644 --- a/toys/posix/chgrp.c +++ b/toys/posix/chgrp.c @@ -82,30 +82,17 @@ void chgrp_main(void) // Distinguish chown from chgrp if (ischown) { char *grp; - struct passwd *p; own = xstrdup(*toys.optargs); if ((grp = strchr(own, ':')) || (grp = strchr(own, '.'))) { *(grp++) = 0; TT.group_name = grp; } - if (*own) { - TT.owner_name = own; - p = getpwnam(own); - // TODO: trailing garbage? - if (!p && isdigit(*own)) p=getpwuid(atoi(own)); - if (!p) error_exit("no user '%s'", own); - TT.owner = p->pw_uid; - } + if (*own) TT.owner = xgetpwnamid(TT.owner_name = own)->pw_uid; } else TT.group_name = *toys.optargs; - if (TT.group_name && *TT.group_name) { - struct group *g; - g = getgrnam(TT.group_name); - if (!g) g=getgrgid(atoi(TT.group_name)); - if (!g) error_exit("no group '%s'", TT.group_name); - TT.group = g->gr_gid; - } + if (TT.group_name && *TT.group_name) + TT.group = xgetgrnamid(TT.group_name)->gr_gid; for (s=toys.optargs+1; *s; s++) { struct dirtree *new = dirtree_add_node(0, *s, hl); -- cgit v1.2.3