Linux下获取用户信息和主机信息程序
程序代码:
#include
#include
#include
#include
#include
#include
void main()
{
printf("当前用户 %s\n", getlogin());
printf("uid=%d, gid=%d\n", getuid(), getgid());
struct passwd *pw;
//当前用户信息
pw = getpwuid(getuid());
printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
pw -> pw_name, pw -> pw_uid, pw -> pw_gid, pw -> pw_dir, pw -> pw_shell);
//tsybius用户信息
pw = getpwnam("tsybius");
printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
pw -> pw_name, pw -> pw_uid, pw -> pw_gid, pw -> pw_dir, pw -> pw_shell);
//root用户信息
pw = getpwnam("root");
printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
pw -> pw_name, pw -> pw_uid, pw -> pw_gid, pw -> pw_dir, pw -> pw_shell);
printf("==============\n");
//获取主机信息
char computer[256];
struct utsname uts;
if (gethostname(computer, 255) != 0 || uname(&uts)
{
fprintf(stderr, "无法获取主机信息\n");
exit(EXIT_FAILURE);
}
printf("HOST NAME:[%s]\n", computer);
printf("操作系统: [%s]\n", uts.sysname);
printf("硬件类型: [%s]\n", uts.machine);
printf("主机名: [%s]\n", uts.nodename);
printf("VERSION: [%s]\n", uts.version);
printf("RELEASE: [%s]\n", uts.release);
exit(EXIT_SUCCESS);
}
运行结果:
END