[hackers] [sbase] chown: Support numeric uids/gids || Michael Forney
commit 62850af59436f70db7e61f7db4306d2b924f7140
Author: Michael Forney <mforney_AT_mforney.org>
Date: Sun Nov 2 22:24:53 2014 +0000
chown: Support numeric uids/gids
diff --git a/chown.c b/chown.c
index 3fd5f87..5d57801 100644
--- a/chown.c
+++ b/chown.c
_AT_@ -11,8 +11,8 @@
static void chownpwgr(const char *);
static bool rflag = false;
-static struct passwd *pw = NULL;
-static struct group *gr = NULL;
+static uid_t uid = -1;
+static gid_t gid = -1;
static int ret = 0;
static void
_AT_@ -24,7 +24,9 @@ usage(void)
int
main(int argc, char *argv[])
{
- char *owner, *group;
+ char *owner, *group, *end;
+ struct passwd *pw;
+ struct group *gr;
ARGBEGIN {
case 'R':
_AT_@ -47,20 +49,26 @@ main(int argc, char *argv[])
if(owner && *owner) {
errno = 0;
pw = getpwnam(owner);
- if (!pw) {
+ if(pw) {
+ uid = pw->pw_uid;
+ } else {
if(errno != 0)
eprintf("getpwnam %s:", owner);
- else
+ uid = strtoul(owner, &end, 10);
+ if(*end != '\0')
eprintf("getpwnam %s: no such user\n", owner);
}
}
if(group && *group) {
errno = 0;
gr = getgrnam(group);
- if (!gr) {
+ if(gr) {
+ gid = gr->gr_gid;
+ } else {
if(errno != 0)
eprintf("getgrnam %s:", group);
- else
+ gid = strtoul(group, &end, 10);
+ if(*end != '\0')
eprintf("getgrnam %s: no such group\n", group);
}
}
_AT_@ -73,8 +81,7 @@ main(int argc, char *argv[])
void
chownpwgr(const char *path)
{
- if(chown(path, pw ? pw->pw_uid : (uid_t)-1,
- gr ? gr->gr_gid : (gid_t)-1) == -1) {
+ if(chown(path, uid, gid) == -1) {
weprintf("chown %s:", path);
ret = 1;
}
Received on Mon Nov 03 2014 - 11:15:45 CET
This archive was generated by hypermail 2.3.0
: Mon Nov 03 2014 - 11:24:17 CET