]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/pwdutils.c
7 /* Returns allocated passwd and allocated pwdbuf to store passwd strings
8 * fields. In case of error returns NULL and set errno, for unknown user set
11 struct passwd
*xgetpwnam(const char *username
, char **pwdbuf
)
13 struct passwd
*pwd
= NULL
, *res
= NULL
;
16 if (!pwdbuf
|| !username
)
19 *pwdbuf
= xmalloc(UL_GETPW_BUFSIZ
);
20 pwd
= xcalloc(1, sizeof(struct passwd
));
23 rc
= getpwnam_r(username
, pwd
, *pwdbuf
, UL_GETPW_BUFSIZ
, &res
);
41 struct passwd
*pw
= NULL
;
49 /* GNU Hurd implementation has an extension where a process can exist in a
50 * non-conforming environment, and thus be outside the realms of POSIX
51 * process identifiers; on this platform, getuid() fails with a status of
52 * (uid_t)(-1) and sets errno if a program is run from a non-conforming
55 * http://austingroupbugs.net/view.php?id=511
62 if (pw
&& pw
->pw_name
&& *pw
->pw_name
)
63 return xstrdup(pw
->pw_name
);
69 int main(int argc
, char *argv
[])
72 struct passwd
*pwd
= NULL
;
75 fprintf(stderr
, "usage: %s <username>\n", argv
[0]);
79 pwd
= xgetpwnam(argv
[1], &buf
);
81 err(EXIT_FAILURE
, "failed to get %s pwd entry", argv
[1]);
83 printf("Username: %s\n", pwd
->pw_name
);
84 printf("UID: %d\n", pwd
->pw_uid
);
85 printf("HOME: %s\n", pwd
->pw_dir
);
86 printf("GECO: %s\n", pwd
->pw_gecos
);
91 printf("Current: %s\n", (buf
= xgetlogin()));
96 #endif /* TEST_PROGRAM */