From bc1ccaccb5b7ec71775a5eb7e529bb93811956ce Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 13 Aug 2016 15:19:29 -0500 Subject: Move getusername/getgroupname to lib. (Return name or string representation of number, but never NULL. Both returned in static buffer good through next call.) --- lib/lib.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/lib.c') diff --git a/lib/lib.c b/lib/lib.c index 718ea3ac..8f8b4a3b 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1184,3 +1184,26 @@ int regexec0(regex_t *preg, char *string, long len, int nmatch, len -= ll; } } + +// Return user name or string representation of number, returned buffer +// lasts until next call. +char *getusername(uid_t uid) +{ + struct passwd *pw = bufgetpwuid(uid); + static char unum[12]; + + sprintf(unum, "%u", (unsigned)uid); + return pw ? pw->pw_name : unum; +} + +// Return group name or string representation of number, returned buffer +// lasts until next call. +char *getgroupname(gid_t gid) +{ + struct group *gr = bufgetgrgid(gid); + static char gnum[12]; + + sprintf(gnum, "%u", (unsigned)gid); + return gr ? gr->gr_name : gnum; +} + -- cgit v1.2.3