struct passwd

本文介绍了在POSIX系统中用于管理用户信息的结构体struct passwd及其相关函数,包括如何通过UID或用户名获取用户详细信息,以及如何操作用户数据文件。
 struct passwd 

    char * pw_name; /* Username, POSIX.1 */ 
    char * pw_passwd; /* Password */ 
    __uid_t pw_uid; /* User ID, POSIX.1 */ 
    __gid_t pw_gid; /* Group ID, POSIX.1 */ 
    char * pw_gecos; /* Real Name or Comment field */ 
    char * pw_dir; /* Home directory, POSIX.1 */ 
    char * pw_shell; /* Shell Program, POSIX.1 */ 
}; 


--------------------------------------------------------------------------------

当您需要取得有关某个使用者的资讯时,大致上有以下几个函数可以使用: 

--------------------------------------------------------------------------------

struct passwd * getpwuid(uid_t uid); 
当您知道使用者的uid(user id)时,可以透过getpwuid来得知所有关於该使用者的相关资讯。 



--------------------------------------------------------------------------------

struct passwd * getpwnam(char * name); 
当您知道使用者名称时,可以透过getpwnam来得知所有关於该使用者的相关资讯。 



--------------------------------------------------------------------------------

int getpw(uid_t uid, char *buf); 
当您仅需要取得使用者的密码进行比对时,可以使用getpw。 



--------------------------------------------------------------------------------

另外,有存取一系列使用者资讯的方法。 


--------------------------------------------------------------------------------

FILE * pwdopen(void); 
开启password档案。 



--------------------------------------------------------------------------------

struct passwd * pwdread(FILE * stream,struct passwd *p); 
读取一个使用者资讯进来,填到p中,返回p为成功,NULL为失败。 



--------------------------------------------------------------------------------

void setpwent(void); 
将读取资料流重设到起点。 



--------------------------------------------------------------------------------

void endpwent(void); 
关闭password档案资料流。 



--------------------------------------------------------------------------------

struct passwd * getpwent(void); 
读取一个使用者资讯进来,有必要的话,则将进行开档动作。 



--------------------------------------------------------------------------------

struct passwd * fgetpwent(FILE * stream); 
从档案中读取一个使用者资讯进来。 



--------------------------------------------------------------------------------

int putpwent(struct passwd *p,FILE *f); 
将一个使用者资讯写入档案中。 



--------------------------------------------------------------------------------

struct passwd * pwdalloc(void); 
配置一个记忆体区块给passwd用。 



--------------------------------------------------------------------------------
int main(int argc, char *argv[]) { struct stat file_stat; stat(argv[1],&file_stat); DIR * dp = opendir(argv[1]); if(NULL == dp) { perror("opendir"); return -1; } struct dirent * p = NULL; while(1) { p = readdir(dp); if(p == NULL) { break; } if(p->d_name[0] == '.') //不显示隐藏文件 continue; if(S_ISREG(file_stat.st_mode)) { printf("-"); } else if(S_ISBLK(file_stat.st_mode)) { printf("b"); } else if(S_ISCHR(file_stat.st_mode)) { printf("c"); } else if(S_ISDIR(file_stat.st_mode)) { printf("d"); } else if(S_ISLNK(file_stat.st_mode)) { printf("l"); } else if(S_ISFIFO(file_stat.st_mode)) { printf("p"); } else if(S_ISSOCK(file_stat.st_mode)) { printf("s"); } if(file_stat.st_mode & S_IRUSR) printf("r"); else printf("-"); if(file_stat.st_mode & S_IWUSR) printf("w"); else printf("-"); if(file_stat.st_mode & S_IXUSR) printf("x"); else printf("-"); if(file_stat.st_mode & S_IRGRP) printf("r"); else printf("-"); if(file_stat.st_mode & S_IWGRP) printf("w"); else printf("-"); if(file_stat.st_mode & S_IXGRP) printf("x"); else printf("-"); if(file_stat.st_mode & S_IROTH) printf("r"); else printf("-"); if(file_stat.st_mode & S_IWOTH) printf("w"); else printf("-"); if(file_stat.st_mode & S_IXOTH) printf("x"); else printf("-"); printf(" "); printf("%ld",file_stat.st_nlink); printf(" "); struct passwd * file_passwd; file_passwd=getpwuid(file_stat.st_uid); printf("%s",file_passwd->pw_name); printf(" "); struct group * file_group; file_group=getgrgid(file_stat.st_gid); printf("%s",file_group->gr_name); printf(" "); printf("%ld",file_stat.st_size); printf(" "); printf("%s\n",p->d_name); printf("\n"); } return 0; } 为什么只有文件名在变
08-07
int main(int argc, char *argv[]) { struct stat file_stat; stat(argv[1],&file_stat); if(S_ISREG(file_stat.st_mode)) { printf("-"); } else if(S_ISBLK(file_stat.st_mode)) { printf("b"); } else if(S_ISCHR(file_stat.st_mode)) { printf("c"); } else if(S_ISDIR(file_stat.st_mode)) { printf("d"); } else if(S_ISLNK(file_stat.st_mode)) { printf("l"); } else if(S_ISFIFO(file_stat.st_mode)) { printf("p"); } else if(S_ISSOCK(file_stat.st_mode)) { printf("s"); } if(file_stat.st_mode & S_IRUSR) printf("r"); else printf("-"); if(file_stat.st_mode & S_IWUSR) printf("w"); else printf("-"); if(file_stat.st_mode & S_IXUSR) printf("x"); else printf("-"); if(file_stat.st_mode & S_IRGRP) printf("r"); else printf("-"); if(file_stat.st_mode & S_IWGRP) printf("w"); else printf("-"); if(file_stat.st_mode & S_IXGRP) printf("x"); else printf("-"); if(file_stat.st_mode & S_IROTH) printf("r"); else printf("-"); if(file_stat.st_mode & S_IWOTH) printf("w"); else printf("-"); if(file_stat.st_mode & S_IXOTH) printf("x"); else printf("-"); printf(" "); printf("%ld",file_stat.st_nlink); printf(" "); struct passwd * file_passwd; file_passwd=getpwuid(file_stat.st_uid); printf("%s",file_passwd->pw_name); printf(" "); struct group * file_group; file_group=getgrgid(file_stat.st_gid); printf("%s",file_group->gr_name); printf(" "); printf("%ld",file_stat.st_size); printf("\n"); return 0; int main(int argc, char *argv[]) { DIR * dp = opendir(argv[1]); if(NULL == dp) { perror(“opendir”); return -1; } struct dirent * p = NULL; while(1) { p = readdir(dp); if(p == NULL) { break; } if(p->d_name[0] == ‘.’) //不显示隐藏文件 continue; printf(“%s\n”,p->d_name); } return 0; } 基于这些代码如何实现ls -l的效果
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值