]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/pwdutils: add xgetpwuid
authorQuentin Rameau <quinq@fifth.space>
Sat, 21 Sep 2019 18:50:20 +0000 (20:50 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 26 Sep 2019 13:55:39 +0000 (15:55 +0200)
include/pwdutils.h
lib/pwdutils.c

index a69dd6b454273e1f0685afd44a83b2671e35ab02..bea46e57e3b04d4267b3e4fa25b23939b0c41f48 100644 (file)
@@ -5,6 +5,7 @@
 #include <pwd.h>
 
 extern struct passwd *xgetpwnam(const char *username, char **pwdbuf);
+extern struct passwd *xgetpwuid(uid_t uid, char **pwdbuf);
 extern char *xgetlogin(void);
 
 #endif /* UTIL_LINUX_PWDUTILS_H */
index 25b4daed0e859ba695fd112fb47b9758e3f5ccdf..d54458d65da6fd539e81df23a3c7b099883267c1 100644 (file)
@@ -36,6 +36,34 @@ failed:
        return NULL;
 }
 
+struct passwd *xgetpwuid(uid_t uid, char **pwdbuf)
+{
+       struct passwd *pwd = NULL, *res = NULL;
+       int rc;
+
+       if (!pwdbuf)
+               return NULL;
+
+       *pwdbuf = xmalloc(UL_GETPW_BUFSIZ);
+       pwd = xcalloc(1, sizeof(struct passwd));
+
+       errno = 0;
+       rc = getpwuid_r(uid, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res);
+       if (rc != 0) {
+               errno = rc;
+               goto failed;
+       }
+       if (!res) {
+               errno = EINVAL;
+               goto failed;
+       }
+       return pwd;
+failed:
+       free(pwd);
+       free(*pwdbuf);
+       return NULL;
+}
+
 char *xgetlogin(void)
 {
        struct passwd *pw = NULL;