Unix环境高级编程学习笔记(4)

第六章 系统数据文件和信息
1、口令文件:一般包含用户名,加密口令,数值用户id,数值组id,注释字段,初始工作目录,初始shell,用户访问类,下次更改口令时间,帐号到期时间   注:这些字段包括在<pwd.h>中定义的passwd结构中。
口令文件一般存储在/etc/passwd中:如:root:x:0:0:root:/root:/bin/bash     cao:x:1000:1000:Cao,,,:/home/cao:/bin/bash
  当shell字段的值为/dev/null时表示阻止任何人以该用户的名义登录系统。
  使用finger命令能够查看用户有关信息:cao@cao-Rev-1-0:~/Studying/Unix编程/Chapter6$ finger -p cao
    Login: cao               Name: Cao
    Directory: /home/cao                 Shell: /bin/bash
    On since Fri Jul 29 16:25 (CST) on tty7 from :0
        16 minutes 24 seconds idle
    On since Fri Jul 29 16:26 (CST) on pts/0 from :0
    No mail.
 两个获取口令文件项的函数。#include<pwd.h>       struct passwd *getpwuid(uid_t uid);(由ls程序使用)       struct passwd *getpwnam(const char *name);(由login程序使用)   //成功返回指针,出错返回null
 #include<pwd.h>      struct passwd *getpwent(void);(返回口令文件的下一项)
         void setpwent(void);(反绕它所使用的文件)   void endpwent(void);(关闭文件)

2、阴影口令:/etc/shadow文件中的字段:
        说明    struct spwd成员
 用户登录名   char *sp_namp
 加密口令   char *sp_pwdp
 上次更改口令以来经过的时间 int sp_lstchg
 经过多少天后允许更改  int sp_min
 要求更改尚剩余的天数  int sp_max
 到期警告天数   int sp_warn
 账户不活动之前尚余天数  int sp_inact
 账户到期天数   int sp_expire
 保留    unsigned int sp_flag
  访问阴影口令的函数:#include<shadow.h>     struct spwd *getspnam(const char *name);        struct spwd *getspent(void);     
          void setspent(void);       void endspent(void);
  注:阴影口令仅能被用户id是root的程序读取。

3、组文件:  <grp.h>     group
      说明        struct group的成员
      组名   char *gr_name
      加密口令   char *gr_passwd
      数值组id   int gr_gid
      指向各用户名的
        指针数组  char **gr_mem
 用于查看组名或数值组id的函数:#include<grp.h>
       struct group *getgrpid(gid_t gid);
      struct group *getgrnam(const char *name);
 搜索整个组文件的函数:#include<grp.h>
          struct group *getgrent();
         void setgrent();
         void endgrent();

4、附加组ID:#include<unistd.h>
      int getgroups(int gidsetsize, gid_t grouplist[]);        将附加组id填写到grouplist中,最多gidsetsize个。
      #include<grp.h>
      #include<unistd.h>/*非linux*/
      int setgroups(int ngroups, const gid_t grouplist[]); 为进程设置附加组id表
      #include<grp.h>   /*on linux and Solaris*/
      #include<unistd.h>/*非linux 和 solaris*/
      int initgroups(const char* username, gid_t basegid);

5、实现的区别:略

6、其他数据文件:这些数据文件的获取类似与口令文件和组文件的接口。一般来说都会提供三个函数:get函数,set函数,end函数,有时会附加关键字搜索接口:
    说明 数据文件  头文件  结构    附加的关键字查找函数
    口令 /etc/passwd  <pwd.h>  passwd  getpwnam,getpwuid
    组  /etc/group  <grp.h>  group  getgrnam,getgrgid
    阴影 /etc/shadow  <shadow.h> spwd  getspnam
    主机 /etc/hosts  <netdb.h> hostent  gethostbyname,gethostbyaddr
    网络 /etc/network  <netdb.h> netend  getnetbyname,getnetbyaddr
    协议 /etc/protocols  <netdb.h> protoent getprotobyname,getprotobynumber
    服务 /etc/services  <netdb.h> servent  getservbyname,getservbyport

7、登录帐号记录
 utmp:记录当前登录进系统的各个用户;wtmp文件,跟踪各个登录和注销事件。

8、系统标识
返回与当前主机和操作系统有关信息:#include<sys/utsname.h>  int uname(struct utsname *name);
struct utsname{
 char sysname[];
 char nodename[];
 char release[];
 char version[];
 char machine[];
};
#include<unistd.h>        int geyhostname(char *name, int namelen);          返回主机的完全限定域名。

9、时间和日期程例程:
 9.1、返回当前时间和日期   #include<time.h>       time_t time(time_t *calptr);
 9.2、返回当前时间和日期(精度更高)   #include<time.h>    int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
  timeval结构中存储秒和微妙:struct timeval{
      time_t tv_sec;
    long tv_usec;
          };
 9.3、localtime和gmtime将日历时间转化成以年、月、日、时、分、秒、周、日表示时间,并将这些存放在tm结构中:
  struct tm{
   int tm_sec;
 int tm_min;
 int tm_hour;
 int tm_mday;
 int tm_mon;
 int tm_year;
 int tm_wday;
 int tm_yday;
 int tm_isdst;
  };
  除了月日字段,其他字段的值都以0开始。
  #include<time.h>
  struct tm *gmtime(const time_t *calptr);  转化为国际标准时间
  struct tm *localtime(const time_t *calptr);     转化为本地时间
 9.4、以本地时间的年、月、日等作为参数,将其转化成time_t值
  #include<time.h>  time_t mktime(struct tm *tmptr);
 9.5、#include<time.h>   char *asctime(const struct tm *tmptr);          char *ctime(const time_t *calptr);
   返回26字节的字符串,例如:Thu Feb 10 18:27:38 2004\n\0
 9.6、#include<time.h>     size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);
   十分复杂,类似于printf的时间值函数

转载于:https://www.cnblogs.com/Just-do-it110/archive/2011/08/03/2126618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值