更新时间:2023-01-30 09:29:51
您可以使用 getpwnam
以获得指向具有 pw_uid
成员的 struct passwd
结构的指针.示例程序:
You can use getpwnam
to get a pointer to a struct passwd
structure, which has pw_uid
member. Example program:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <pwd.h>
int main(int argc, char *argv[])
{
const char *name = "root";
struct passwd *p;
if (argc > 1) {
name = argv[1];
}
if ((p = getpwnam(name)) == NULL) {
perror(name);
return EXIT_FAILURE;
}
printf("%d\n", (int) p->pw_uid);
return EXIT_SUCCESS;
}
如果要重入函数,请查看 getpwnam_r
.
If you want a re-entrant function, look into getpwnam_r
.