aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-03-12 11:11:08 -0500
committerRob Landley <rob@landley.net>2015-03-12 11:11:08 -0500
commitb8140d18800e7094cdacb0a61526f46181dc132d (patch)
treecb1060035cb162bf78aa4cbcefe0814b55e7f7d4 /lib
parent128928f123d7192d07538d6ea6bbda2b1a75fd5b (diff)
downloadtoybox-b8140d18800e7094cdacb0a61526f46181dc132d.tar.gz
Factor out xgetgrnamid() and xgetpwnamid() into xwrap.c.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.h2
-rw-r--r--lib/xwrap.c32
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/lib.h b/lib/lib.h
index e5667517..d8a1503e 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -123,6 +123,8 @@ struct passwd *xgetpwuid(uid_t uid);
struct group *xgetgrgid(gid_t gid);
struct passwd *xgetpwnam(char *name);
struct group *xgetgrnam(char *name);
+struct passwd *xgetpwnamid(char *user);
+struct group *xgetgrnamid(char *group);
void xsetuser(struct passwd *pwd);
char *xreadlink(char *name);
long xparsetime(char *arg, long units, long *fraction);
diff --git a/lib/xwrap.c b/lib/xwrap.c
index 44cd1684..54f2cbb5 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -477,6 +477,38 @@ struct group *xgetgrgid(gid_t gid)
return group;
}
+struct passwd *xgetpwnamid(char *user)
+{
+ struct passwd *up = getpwnam(user);
+ uid_t uid;
+
+ if (!up) {
+ char *s = 0;
+
+ uid = estrtol(user, &s, 10);
+ if (!errno && s && !*s) up = getpwuid(uid);
+ }
+ if (!up) perror_exit("user '%s'", user);
+
+ return up;
+}
+
+struct group *xgetgrnamid(char *group)
+{
+ struct group *gr = getgrnam(group);
+ gid_t gid;
+
+ if (!gr) {
+ char *s = 0;
+
+ gid = estrtol(group, &s, 10);
+ if (!errno && s && !*s) gr = getgrgid(gid);
+ }
+ if (!gr) perror_exit("group '%s'", group);
+
+ return gr;
+}
+
struct passwd *xgetpwnam(char *name)
{
struct passwd *up = getpwnam(name);