getpwuid()函数

linux getpwuid 得到指定用户信息
 
    
 
    
和系统数据相关的passwd 结构 
 
    
和系统数据相关的一个结构passwd定义如下 
 
    
/* The passwd structure.      */ 
struct passwd 
{ 
      char *pw_name;       /* 用户名*/ 
      char *pw_passwd;     /* 密码.*/ 
      __uid_t pw_uid;      /* 用户ID.*/ 
      __gid_t pw_gid;      /*组ID.*/ 
      char *pw_gecos;      /*真实名*/ 
      char *pw_dir;        /* 主目录.*/ 
      char *pw_shell;      /*使用的shell*/ 
}; 
该结构描述了/etc/passwd的文件记录行 包括用户名 密码 用户ID 用户组ID 真实姓名 用户主目录和默默认SHELL 
相关的API有 
struct passwd *getpwuid(uid uid) 
struct paswd *getpwnam(const char *name) 
getpwuid 根据传入的用户ID返回指向passwd的结构体 该结构体初始化了里面的所有成员 
getpwnam 和getpwuid一样 只是传入的参数是用户名 
小试牛刀:这个程序只是作演示并没有作错误处理 
 
    
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <pwd.h> 
int main (void) 
{ 
       struct passwd *passwd; 
       passwd=getpwuid(getuid()); 
       /** passwd=getcwnam("phpos"); **/ 
       printf("Username:%s/n",passwd->pw_name); 
       printf("Password:%s/n",passwd->pw_passwd); 
       printf("uid:%d/n",passwd->pw_uid); 
       printf("gid:%d/n",passwd->pw_gid); 
       printf("shell:%s/n",passwd->pw_shell); 
       printf("dir:%s/n",passwd->pw_dir); 
       return 0; 
} 
posted on 2015-03-22 22:50 guangliang_98 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/guangliang/p/4358316.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值