#include
#include
#include
#include
#include
int main(void)
{
char *login;
struct passwd *pentry;
/* Get the login name */
if((login = getlogin()) == NULL) { /* oops */
perror("getlogin");
exit(EXIT_FAILURE);
}
/* Get the password entry for login */
if((pentry = getpwnam(login)) == NULL) {
perror("getpwnam");
exit(EXIT_FAILURE);
}
/* Display the password entry */
printf("user name: %s\n", pentry- >pw_name);
printf("UID : %d\n", pentry- >pw_uid);
printf("GID : %d\n", pentry- >pw_gid);
printf("gecos : %s\n", pentry- >pw_gecos);
printf("home dir : %s\n", pentry- >pw_dir);
printf("shell : %s\n", pentry- >pw_shell);
exit(EXIT_SUCCESS);
}
该程序是显示哪个用户在运行进程,输出用户名、UID等信息。可是运行的时候出现问题,调用getlogin()函数后返回却是NULL,得不到用户名(我以普通用户身份登陆linux系统)
$./getname
getlogin:no such file or directory
请问是怎么回事?