From 4f5f35fc83067b2c4874fce00ea0b53ef2488489 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 14 Oct 2016 13:21:17 +0200 Subject: login: add xgetpwnam() Signed-off-by: Karel Zak --- lib/pwdutils.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/pwdutils.c (limited to 'lib/pwdutils.c') diff --git a/lib/pwdutils.c b/lib/pwdutils.c new file mode 100644 index 000000000..58e75a45b --- /dev/null +++ b/lib/pwdutils.c @@ -0,0 +1,64 @@ +#include + +#include "c.h" +#include "pwdutils.h" +#include "xalloc.h" + +/* Returns allocated passwd and allocated pwdbuf to store passwd strings + * fields. In case of error returns NULL and set errno, for unknown user set + * errno to EINVAL + */ +struct passwd *xgetpwnam(const char *username, char **pwdbuf) +{ + struct passwd *pwd = NULL, *res = NULL; + int rc; + + if (!pwdbuf || !username) + return NULL; + + *pwdbuf = xmalloc(UL_GETPW_BUFSIZ); + pwd = xcalloc(1, sizeof(struct passwd)); + + errno = 0; + rc = getpwnam_r(username, 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; +} + + +#ifdef TEST_PROGRAM +int main(int argc, char *argv[]) +{ + char *pwdbuf = NULL; + struct passwd *pwd = NULL; + + if (argc != 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + pwd = xgetpwnam(argv[1], &pwdbuf); + if (!pwd) + err(EXIT_FAILURE, "failed to get %s pwd entry", argv[1]); + + printf("Username: %s\n", pwd->pw_name); + printf("UID: %d\n", pwd->pw_uid); + printf("HOME: %s\n", pwd->pw_dir); + printf("GECO: %s\n", pwd->pw_gecos); + + free(pwd); + free(pwdbuf); + return EXIT_SUCCESS; +} +#endif /* TEST_PROGRAM */ -- cgit v1.2.3-55-g7522