]>
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
);
39 struct passwd
*xgetpwuid(uid_t uid
, char **pwdbuf
)
41 struct passwd
*pwd
= NULL
, *res
= NULL
;
47 *pwdbuf
= xmalloc(UL_GETPW_BUFSIZ
);
48 pwd
= xcalloc(1, sizeof(struct passwd
));
51 rc
= getpwuid_r(uid
, pwd
, *pwdbuf
, UL_GETPW_BUFSIZ
, &res
);
69 struct passwd
*pw
= NULL
;
77 /* GNU Hurd implementation has an extension where a process can exist in a
78 * non-conforming environment, and thus be outside the realms of POSIX
79 * process identifiers; on this platform, getuid() fails with a status of
80 * (uid_t)(-1) and sets errno if a program is run from a non-conforming
83 * http://austingroupbugs.net/view.php?id=511
90 if (pw
&& pw
->pw_name
&& *pw
->pw_name
)
91 return xstrdup(pw
->pw_name
);
97 int main(int argc
, char *argv
[])
100 struct passwd
*pwd
= NULL
;
103 fprintf(stderr
, "usage: %s <username>\n", argv
[0]);
107 pwd
= xgetpwnam(argv
[1], &buf
);
109 err(EXIT_FAILURE
, "failed to get %s pwd entry", argv
[1]);
111 printf("Username: %s\n", pwd
->pw_name
);
112 printf("UID: %d\n", pwd
->pw_uid
);
113 printf("HOME: %s\n", pwd
->pw_dir
);
114 printf("GECO: %s\n", pwd
->pw_gecos
);
119 printf("Current: %s\n", (buf
= xgetlogin()));
124 #endif /* TEST_PROGRAM */