UNIX环境高级编程读书笔记(五)—系统文件和信息 (2)
来源:互联网 发布:无间道歌词含义 知乎 编辑:程序博客网 时间:2024/05/17 04:05
二、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 用户组id
返回值:
若成功则返回指针,若出错则返回NULL
用户组的信息包含在/ect/group文件中,该文件包括如下信息。
组名 char *gr_name
加密口令 char *gr_passwd
数值组id int gr_gid
指向各用户名的指针数组 char **grmen
/*5_4.c*/
#include <sys/types.h>
#include <grp.h>
#include <unistd.h>
main()
{
struct group *grp;
if((grp=getgrgid(getgid()))<0)
perror(“error”);
else
{
printf(“gname:%s/n”,ptr->gr_name);
printf(“gid:%d/n”,ptr->gr_gid);
}
}
5.
名称::
getgrent/sergrent/endgrent
功能:
get group file entry
头文件:
#include <grp.h>
函数原形:
void group *getgrent();
void setgrent();
void endgrent();
参数:
无
返回值:
若成功则返回指针,若出错则返回NULL
上面两组函数类似于针对口令的几个函数。大家可以仿照程序5_2.c编写打印系统所有组的组名,和组id的程序。/*5_5.c */
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[] 用于存放附加组id
gidsetsize 最大存放id的个数
ngroups 数组中元素的个数
username 用户名
返回值:
getgroups 若成功则返回附加组id数,若出错则返回-1
setgroups/ initgrops 若成功则返回0,若出错则返回-1
一个用户不仅属于口令文件记录项中组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++)
printf(“%d/n”,grouplist[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{
char sysname[9];
char nodename[9];
char release[9];
char version[9];
char machine[9];
}
/*5_7.c*/
#include <stdio.h>
#include <sys/utsname.h>
#include <error.h>
main()
{
struct utsname buf[10];
if((uname(buf))<0)
perror(“error”);
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 指定name缓冲区长度
返回值:
若成功则返回0,若出错则返回-1
Gethostname函数的功能是返回系统的主机名。该名字通常是TCP/IP网络上的主机的名字。
/*5_8.c*/
#include <stdio.h>
#include <unistd.h>
main()
{
char name[10];
int namelen;
if((gethostname(name,namelen)<0)
perror(“error”);
else
printf(“%s/n”,name);
}
除了这些系统文件,linux还有其它系统文件记录着系统信息。如utmp(/var/run/utmp)记录着当前登录进系统的各个用户。wtmp(/var/log/wtmp)跟踪各个登录和注销事件。
四、linux中的系统文件总结:
1.文件位置:/ect/passwd 文件功能:口令文件 头文件:<pwd.h> 结构passwd包括信息:用户名(char *pw_name),加密口令(char * pw_passwd),数值用户id(uid_t pw_uid),数值组id(uid_t pw_gid),注释字段(char *pw_gecos),初始工作目录(char *pw_dir),初始Shell(char *pw_shell),用户防问类(char *pw_class),下次更改口令时间(time_t pw_change),帐户到期时间(time_t pw_expire)相关函数: getpwuid/getpwnam(读当前用户的信息) getpwent/setpwent/endpwent(读所有用户的信息).
2.文件位置:/ect/shadow 文件功能:阴影口令文件 头文件:<shadow.h> 结构:spwd包括信息:用户登录名(char *sp_namp),加密口令(char *sp_pwdp),上次更改口令以来经过的时间(int sp_lstchg),经过多少天后容许更改(nt sp_min),要求更改声音剩余天数(int sp_max),到期警告天数 (int sp_warn),帐户不活动以前尚余天数(int sp_inact),帐户到期天数(int sp_expir),保留(unsigned int sp_flag)。相关函数:getspnam/getspent/setspent/endspent。
3.文件位置:/ect/group 文件名:组文件 头文件:<grp.h> 结构:group 包括信息:组名(char *gr_name),加密口令 (char *gr_passwd),数值组id(int gr_gid),指向各用户名的指针数组(char **grmen)。相关函数:getgrgid/getgrnam (读取当前用户的组信息)getgrent/sergrent/endgrent(读取所有组的信息)。
4.文件位置: /ect/host 文件功能:记录主机信息 头文件:<notdb.h> 结构:hostent 相关函数:gethostbyname/gethostbyaddr
5. 文件位置:/ect/networks 文件功能:记录网络信息 头文件:<netdb.h> 结构:netent 相关函数: getnetbyname/getnetbyaddr
5.文件位置:/ect/protocols 文件功能:记录协议信息 头文件:<netdb.h> 结构:protoent 相关函数:getprotobyname/getprotobynumber
6.文件位置:/ect/services 文件功能:记录服务信息 头文件:<netdb.h> 结构:servent 相关函数:getservbyname,getservbyport
7.文件位置:/var/run/utmp 文件功能:记录当前登录进系统的各个用户。头文件:<unmp.h> 结构:utmp
8.文件位置:/var/log/wtmp 文件功能:记录登录和注销事件。
- UNIX环境高级编程读书笔记(五)—系统文件和信息 (2)
- UNIX环境高级编程读书笔记(五)—系统文件和信息 (2)
- UNIX环境高级编程读书笔记(五)—系统文件和信息 (1)
- UNIX环境高级编程读书笔记(五)—系统文件和信息 (1)
- UNIX环境高级编程-读书笔记-系统文件信息
- 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)
- linux系统数据文件和信息--unix环境高级编程读书笔记
- 《UNIX环境高级编程》六系统数据文件和信息读书笔记
- 文件和目录(二)--unix环境高级编程读书笔记
- 《UNIX环境高级编程》读书笔记之文件IO(2)
- 读书笔记——《UNIX环境高级编程》第六章_系统数据文件和信息
- 《unix环境高级编程》 读书笔记 (2)
- UNIX环境高级编程读书笔记(2)
- 《unix高级环境编程》系统数据文件和信息——口令文件
- 《unix高级环境编程》系统数据文件和信息——组文件
- 《unix高级环境编程》系统数据文件和信息——口令文件
- 《unix高级环境编程》系统数据文件和信息——组文件
- UNIX环境高级编程读书笔记(一)—文件IO (2)
- CASyncSocket类和CSocket类编程
- UNIX环境高级编程读书笔记(四)—出错处理
- Statement与PreparedStatement的区别
- struts1流程图
- UNIX环境高级编程读书笔记(五)—系统文件和信息 (1)
- UNIX环境高级编程读书笔记(五)—系统文件和信息 (2)
- sales force的个人开发笔记
- UNIX环境高级编程读书笔记(六)—时间和日期 (1)
- 如何理解 #define SREG (*(volatile unsigned char *)0x5F
- UNIX环境高级编程读书笔记(六)—时间和日期 (2)
- AMR文件格式分析与转换
- Struts,Spring,Hibernate的面试题
- 草稿
- HashMap、HashSet、HashTable的区别