Mercurial > hg > toybox
comparison toys/posix/chgrp.c @ 1730:90a7ed7ec30e draft
Factor out xgetgrnamid() and xgetpwnamid() into xwrap.c.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 12 Mar 2015 11:11:08 -0500 |
parents | 88e6a100490e |
children |
comparison
equal
deleted
inserted
replaced
1729:801eba977271 | 1730:90a7ed7ec30e |
---|---|
80 TT.owner = TT.group = -1; | 80 TT.owner = TT.group = -1; |
81 | 81 |
82 // Distinguish chown from chgrp | 82 // Distinguish chown from chgrp |
83 if (ischown) { | 83 if (ischown) { |
84 char *grp; | 84 char *grp; |
85 struct passwd *p; | |
86 | 85 |
87 own = xstrdup(*toys.optargs); | 86 own = xstrdup(*toys.optargs); |
88 if ((grp = strchr(own, ':')) || (grp = strchr(own, '.'))) { | 87 if ((grp = strchr(own, ':')) || (grp = strchr(own, '.'))) { |
89 *(grp++) = 0; | 88 *(grp++) = 0; |
90 TT.group_name = grp; | 89 TT.group_name = grp; |
91 } | 90 } |
92 if (*own) { | 91 if (*own) TT.owner = xgetpwnamid(TT.owner_name = own)->pw_uid; |
93 TT.owner_name = own; | |
94 p = getpwnam(own); | |
95 // TODO: trailing garbage? | |
96 if (!p && isdigit(*own)) p=getpwuid(atoi(own)); | |
97 if (!p) error_exit("no user '%s'", own); | |
98 TT.owner = p->pw_uid; | |
99 } | |
100 } else TT.group_name = *toys.optargs; | 92 } else TT.group_name = *toys.optargs; |
101 | 93 |
102 if (TT.group_name && *TT.group_name) { | 94 if (TT.group_name && *TT.group_name) |
103 struct group *g; | 95 TT.group = xgetgrnamid(TT.group_name)->gr_gid; |
104 g = getgrnam(TT.group_name); | |
105 if (!g) g=getgrgid(atoi(TT.group_name)); | |
106 if (!g) error_exit("no group '%s'", TT.group_name); | |
107 TT.group = g->gr_gid; | |
108 } | |
109 | 96 |
110 for (s=toys.optargs+1; *s; s++) { | 97 for (s=toys.optargs+1; *s; s++) { |
111 struct dirtree *new = dirtree_add_node(0, *s, hl); | 98 struct dirtree *new = dirtree_add_node(0, *s, hl); |
112 if (new) dirtree_handle_callback(new, do_chgrp); | 99 if (new) dirtree_handle_callback(new, do_chgrp); |
113 else toys.exitval = 1; | 100 else toys.exitval = 1; |