linux:通过uid和gid找到用户名字和用户组名字
一,函数getpwuid简介:
// 终端输入命令 man getpwuid
/*
#include<sys/types.h>
#include<pwd.h>
struct passwd *getpwuid(uid_t uid);
// 结构体struct passwd如下
struct passwd {
char *pw_name; // username
char *pw_passwd; // user password
uid_t pw_uid; // user ID
gid_t pw_gid; // group ID
char *pw_gecos; // user information
char *pw_dir; // home directory
char *pw_shell; // shell program
};
*/
二,函数getpwuid使用:
#include<stdio.h>
#include<sys/types.h>
#include<pwd.h>
int main()
{
struct passwd *_passwd;
uid_t uid = 1000;//我当前用户uid是1000
_passwd = getpwuid(uid);
if(_passwd != NULL)
printf("%s\n", _passwd->pw_name);
return 0;
}
三,函数getgrgid简介:
// 终端输入命令 man getgrgid
/*
#include <sys/types.h>
#include <grp.h>
struct group *getgrgid(gid_t gid);
// 结构体struct group如下
struct group {
char *gr_name; // group name
char *gr_passwd; // group password
gid_t gr_gid; // group ID
char **gr_mem; // NULL-terminated array of pointers
to names of group members
};
*/
四,函数getgrgid使用:
#include<stdio.h>
#include<sys/types.h>
#include<grp.h>
int main()
{
struct group *_group;
gid_t gid = 1000;//我目前用户gid也是1000
_group = getgrgid(gid);
if(_group != NULL)
printf("%s\n", _group->gr_name);
return 0;
}