您的位置:首页 > 运维架构 > Linux

Gnu/Linux系统C编程之 -- 用户和组

2016-03-09 11:38 495 查看
用户和组

getpwnam函数的简单使用,
[root@python users_groups]# cat my_getpwnam.c
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>

int main(int argc, char *argv[])
{
struct passwd *pwd;

if (argc < 2) {
printf("Usage: %s <username>\n", argv[0]);
exit(1);
}

pwd = getpwnam(argv[1]);
if (pwd == NULL) {
printf("could not get %s record\n", argv[1]);
exit(1);
} else {
printf("find [ %s ] record, the following is the info:\n", argv[1]);
printf("Username: %s\n", pwd->pw_name);
printf("Uid     : %ld\n", (long)pwd->pw_uid);
printf("Shell   : %s\n", pwd->pw_shell);
}

return 0;
}


编译并运行,
[root@python users_groups]# gcc -g -o my_getpwnam my_getpwnam.c
[root@python users_groups]# ./my_getpwnam
Usage: ./my_getpwnam <username>
[root@python users_groups]# ./my_getpwnam root
find [ root ] record, the following is the info:
Username: root
Uid     : 0
Shell   : /bin/bash
[root@python users_groups]# ./my_getpwnam www
could not get www record
[root@python users_groups]# ./my_getpwnam lavenliu
find [ lavenliu ] record, the following is the info:
Username: lavenliu
Uid     : 500
Shell   : /bin/bash
[root@python users_groups]# ./my_getpwnam lavenliu
[root@python users_groups]# ./my_getpwnam taoqi
find [ taoqi ] record, the following is the info:
Username: taoqi
Uid     : 517
Shell   : /bin/bash


本文出自 “固态U盘” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1749062
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: