lib/pwdutils: add xgetpwuid

This commit is contained in:
Quentin Rameau 2019-09-21 20:50:20 +02:00 committed by Karel Zak
parent 135e03892b
commit cd083615b4
2 changed files with 29 additions and 0 deletions

View 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 */

View 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;