【发布时间】:2012-09-30 19:43:02
【问题描述】:
我编写了一个简单的程序,它在用户名上调用getpwnam(),将该用户名传递给一个函数,然后再次调用getpwnam()。出于某种原因,我总是在函数中获取 root 的密码信息。
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
void printuser(char *username) {
printf("USERNAME2: %s\n", username);
struct passwd *user_info = getpwnam(username);
printf("USERNAME3: %s\n", username);
printf("USERNAME4: %s\n", user_info->pw_name);
}
int main(int argc, char *argv[]) {
struct passwd *user_info = getpwnam(argv[1]);
printf("USERNAME1: %s\n", user_info->pw_name);
printuser(user_info->pw_name);
printf("USERNAME5: %s\n", user_info->pw_name);
}
程序总是产生以下输出:
$ ./test_getpwnam chenxiaolong
USERNAME1: chenxiaolong
USERNAME2: chenxiaolong
USERNAME3: root
USERNAME4: root
USERNAME5: root
我在手册页中找不到与此相关的任何内容。是不是我做错了什么?
提前致谢!
【问题讨论】: