博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

小猪娃


One World One Dream
xuanfei.cublog.cn


linux c 获取系统、用户信息
源码:
xuanfei#cat getuserinfor.c

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <stdio.h>
int main(int argc, char **argv)
{
    pid_t my_pid, parent_pid;//*_t 均为int类型
    uid_t my_uid, my_euid;
    gid_t my_gid, my_egid;
    struct passwd *my_info;
    my_pid = getpid();
    parent_pid = getppid();
    my_uid = getuid();
    my_euid = geteuid();
    my_gid = getgid();
    my_egid = getegid();
    my_info = getpwuid(my_uid);//可以指定用户id
    printf("Process ID:%d\n", my_pid);
    printf("Parent ID:%d\n", parent_pid);
    printf("User ID:%d\n", my_uid);
    printf("Effective User ID:%d\n", my_euid);
    printf("Group ID:%d\n", my_gid);
    printf("Effective Group ID:%d\n", my_egid);
    if (my_info) {
        printf("My Login Name:%s\n", my_info->pw_name);
        printf("My Password :%s\n", my_info->pw_passwd);
        printf("My User ID :%d\n", my_info->pw_uid);
        printf("My Group ID :%d\n", my_info->pw_gid);
        printf("My Real Name:%s\n", my_info->pw_gecos);
        printf("My Home Dir :%s\n", my_info->pw_dir);
        printf("My Work Shell:%s\n", my_info->pw_shell);
    }
    return 0;
}


编译运行结果:

xuanfei#gcc -Wall getuserinfor.c

xuanfei#./a.out
Process ID:31679
Parent ID:16759
User ID:0
Effective User ID:0
Group ID:0
Effective Group ID:0
My Login Name:root
My Password :x
My User ID :0
My Group ID :0
My Real Name:root
My Home Dir :/root
My Work Shell:/bin/bash

 TAG pid_t
发表于: 2007-12-23 ,修改于: 2007-12-23 18:05,已浏览576次,有评论0条 推荐 投诉


网友评论

发表评论