二、ect/group文件
4.
名称:: | getgrgid/getgrnam |
功能: | get group file entry |
头文件: | #include <grp.h> #include <sys/types.h> |
函数原形: | struct group *getgrnam(const char *name); struct group *getgrgid(gid_t gid); |
参数: | name gid |
返回值: | 若成功则返回指针,若出错则返回NULL |
用户组的信息包含在/ect/group文件中,该文件包括如下信息。
组名
加密口令
数值组id
指向各用户名的指针数组
#include <sys/types.h> #include <grp.h> #include <unistd.h> main() { struct group *grp; if((grp=getgrgid(getgid()))<0) else { } } |
5.
名称:: | getgrent/sergrent/endgrent |
功能: | get group file entry |
头文件: | #include <grp.h> |
函数原形: | void group *getgrent(); void setgrent(); void endgrent(); |
参数: | 无 |
返回值: | 若成功则返回指针,若出错则返回NULL |
上面两组函数类似于针对口令的几个函数。大家可以仿照程序5_2.c编写打印系统所有组的组名,和组id的程序。
6.
名称:: | getgroups/setgroups/initgroups |
功能: | get group file entry |
头文件: | #include <grp.h> #include <unistd.h> |
函数原形: | int getgroups(int gidsetsize,gid_t grouplist[]); int setgroups(int ngroups,const gid_t grouplist[]); int initgrops(const char *username,gid_t basegid); |
参数: | grouplist[] gidsetsize ngroups username |
返回值: | getgroups setgroups/ initgrops |
一个用户不仅属于口令文件记录项中组id所对应的组。也可以属于多达16个另外的组。在这里另外的组id叫附加组id。这样我们就可以实现一个人属于多个组了。下面的例子功能是打印当前用户的附加主组id。
/5_6.c #include <stdio.h> #include <unistd.h> #include <grp.h> #include <sys/type.h> iint main() { int num; int i; int gidsetsize=16; gid_t grouplist[16]; num=getgroups(gidsetsize,grouplist); for(i=0;i<num;i++) } |
Getgroups将进程所属用户的各添加组ID填写到grouplist中,填写入该数组的添加组ID最多为gidsetsize个。实际填写到数组中的添加组ID数由函数返回。
Setgroups可由超级用户调用以便为调用进程设置添加组ID表。Grouplist是组ID数组,而ngroups说明了数组中的元素数。
通常只有initgroups函数调用setgroups,initgroups读整个组文件,然后对username确定其组的成员关系。然后,它调用setgroups,以便为该用户初始化添加组ID表。
三、获得主机名
7.
名称:: | uname |
功能: | get name and information about current kernel |
头文件: | #include <sys/utsname.h> |
函数原形: | int uname(struct utsname *buf); |
参数: | name |
返回值: | 若成功则返回非负值,若出错则返回-1 |
Uname函数返回与当前主机和操作系统有关信息。通过该函数的参数向其传递一个utsname结构的地址,然后该函数填写该结构。
struct utsname{
}
#include <stdio.h> #include <sys/utsname.h> #include <error.h> main() { struct utsname buf[10]; if((uname(buf))<0) else { printf(“%s “,buf->sysname); printf(“%s”,buf->nodename); printf(“%s”,buf->release); printf(“%s”,buf->version); printf(“%s”,buf->machine); } |
8.
名称:: | gethostname |
功能: | get group file entry |
头文件: | #include <unistd.h> |
函数原形: | int gethostname(char *name,int namelen) |
参数: | name namelen |
返回值: | 若成功则返回0,若出错则返回-1 |
Gethostname函数的功能是返回系统的主机名。该名字通常是TCP/IP网络上的主机的名字。
#include <stdio.h> #include <unistd.h> main() { char name[10]; int namelen; if((gethostname(name,namelen)<0) else printf(“%s\n”,name); } |
四、linux中的系统文件总结:
1.文件位置:/ect/passwd
2.文件位置:/ect/shadow
7.文件位置:/var/run/utmp 文件功能:记录当前登录进系统的各个用户。头文件:<unmp.h> 结构:utmp