UNIX环境高级编程-第6章- 系统数据文件和信息

6.2 口令文件

口令文件的结构

/* The passwd structure.  */  
struct passwd  
{  
  char *pw_name;        /* Username.  */  
  char *pw_passwd;      /* Password.  */  
  uid_t pw_uid;             /* User ID.  */  
  gid_t pw_gid;             /* Group ID.  */  
  char *pw_gecos;       /* Real name.  */  
  char *pw_dir;         /* Home directory.  */  
  char *pw_shell;       /* Shell program.  */  
};  
getpwuid和getpwnam函数
/**** 
 函数功能:获取口令文件信息 
 返回值:若成功返回指针,出错则返回NULL; 
 函数原型: 
*****/  
/* Search for an entry with a matching user ID. */  
struct passwd *getpwuid (uid_t uid);  
  
/* Search for an entry with a matching username. */  
struct passwd *getpwnam (const char *name);  
测试例子:
#include <pwd.h>  
#include <sys/types.h>  
#include <unistd.h>  
#include "apue.h"  
int main(void)  
{  
    struct passwd *ptr;  
    uid_t ui = getuid();  
    ptr = getpwuid(ui);  
    if(NULL == ptr)  
        perror("error.");  
    printf("pw_name:%s\n",ptr->pw_name);  
    printf("pw_uid:%d\n",ptr->pw_uid);  
    printf("pw_dir:%s\n",ptr->pw_dir);  
  
    exit(0);  
}  

查看口令函数

/**** 
 * 函数功能:查看整个口令文件信息; 
 * 返回值:若成功则返回指针,若出错或达到文件尾则返回NULL; 
 * 函数原型: 
 * */  
/* Read an entry from the password-file stream, opening it if necessary. */  
 struct passwd *getpwent (void);  
  
/* Rewind the password-file stream. */  
 void setpwent (void);  
  
/* Close the password-file stream. */  
 void endpwent (void);  

例子测试:

#include <pwd.h>  
#include <sys/types.h>  
#include <unistd.h>  
#include "apue.h"  
int main(void)  
{  
    struct passwd *ptr;  
    setpwent();  
    ptr = getpwent();  
    if(NULL == ptr)  
        perror("error.");  
    printf("pw_name:%s\n",ptr->pw_name);  
    printf("pw_uid:%d\n",ptr->pw_uid);  
    printf("pw_dir:%s\n",ptr->pw_dir);  
    endpwent();  
    exit(0);  
}  

6.4 组文件

在系统文件中,跟口令文件类似,存在这组文件,基本结构跟口令文件相同。

组文件结构信息

/* The group structure.  */  
struct group  
  {  
    char *gr_name;      /* Group name.  */  
    char *gr_passwd;        /* Password.    */  
    gid_t gr_gid;       /* Group ID.    */  
    char **gr_mem;      /* Member list. */  //指针数组,每个指针指向一个属于该组的用户名。
  };  

getgrgid和getgrnam函数

/****  
 函数功能:获取组文件信息  ,GID 和组名,需要提供相应的参数
 返回值:若成功返回结构体指针,出错则返回NULL;  
 函数原型:  
*****/    
/* Search for an entry with a matching group ID. */  
 struct group *getgrgid (gid_t gid);  
  
/* Search for an entry with a matching group name. */  
 struct group *getgrnam (const char *name);  
测试程序
#include <grp.h>  
#include <sys/types.h>  
#include <unistd.h>  
#include "apue.h"  
int main(void)  
{  
    struct group *ptr;  
    gid_t gi = getgid();  
    ptr = getgrgid(gi);  
    if(NULL == ptr)  
        perror("error.");  
    printf("gr_name:%s\n",ptr->gr_name);  
    printf("gr_gid:%d\n",ptr->gr_gid);  
    printf("gr_pwd:%s\n",ptr->gr_passwd);  
  
    exit(0);  
}  

查看组信息函数

/****  
 * 函数功能:查看整个组文件信息;  
 * 返回值:若成功则返回指针,若出错或达到文件尾则返回NULL;  
 * 函数原型:  
 * */   
/* Read an entry from the group-file stream, opening it if necessary. */  
  
 struct group *getgrent (void);  
  
/* Rewind the group-file stream. */  
  void setgrent (void);  
  
/* Close the group-file stream. */  
 void endgrent (void);  

测试程序

#include <grp.h>  
#include <sys/types.h>  
#include <unistd.h>  
#include "apue.h"  
int main(void)  
{  
    struct group *ptr;  
    setgrent();  
    ptr = getgrent();  
    if(NULL == ptr)  
        perror("error.");  
    printf("gr_name:%s\n",ptr->gr_name);  
    printf("gr_gid:%d\n",ptr->gr_gid);  
    printf("gr_passwd:%s\n",ptr->gr_passwd);  
    endgrent();  
    exit(0);  
}  

6.7 其他数据文件

 

6.10 时间和日期例程  

unix内核的基本服务时间是计算自国际标准时间公元1970年1月1日00:00:00以来经过的秒数,以时间类型time_t表示。以下根据时间的不同显示进行记录各种时间函数。

当前时间time

/* time函数. */  
/*** 
 * 函数功能:返回当前时间和日历。 
 * 返回值:若成功则返回时间值,若出错则返回-1。 
 * 函数原型: 
 * **/  
  #include <time.h>  
  time_t time(time_t *calptr);  
 /* 说明: 
 * 若参数calptr非空,则返回的时间值存储在calptr所指的单元内; 
 * ***/  

       上面time时间函数的返回值的日历时间,即是time_t类型的秒数,接下来是以普通显示的时间,就是我们平常接触的时间显示方式:年、月、日、时、分、秒。先看下这种时间显示结构信息:

/* tm structure.  */  
struct tm  
{  
  int tm_sec;           /* Seconds. [0-60] (1 leap second) */  
  int tm_min;           /* Minutes. [0-59] */  
  int tm_hour;          /* Hours.   [0-23] */  
  int tm_mday;          /* Day.     [1-31] */  
  int tm_mon;           /* Month.   [0-11] */  
  int tm_year;          /* Year - 1900.  */  
  int tm_wday;          /* Day of week. [0-6] */  
  int tm_yday;          /* Days in year.[0-365] */  
  int tm_isdst;         /* DST.     [-1/0/1]*/  
};  

       将日历时间time_t转换为可读时间的函数分别是localtime和gmtime函数,这两个函数的区别是:localtime将日历时间转换为当地时间;gmtime将日历时间转换为国际标准时间;注:这里的可读时间是指我们一般使用的年月日时分秒;

/* 将日历时间转换为可读日期 */    
/* localtime 和 gmtime 函数 */  
/** 
 * 函数功能:将时间秒转换为可读日期; 
 * 返回值:返回指向tm结构的指针; 
 * 函数原型: 
 * Return the `struct tm' representation of *TIMER 
   in Universal Coordinated Time (aka Greenwich Mean Time).*/   
 struct tm *gmtime (const time_t *timer);  
  
/* Return the `struct tm' representation 
   of *TIMER in the local timezone.  */  
 struct tm *localtime (const time_t *timer);  
 /** 
  * 区别:localtime返回的是当地时间;gmtime返回的是国际时间; 
  * ***/  

      mktime函数是将可读时间转换为日历时间time_t

/** 将可读时间转换为time_t时间 */  
time_t mktime(struct tm *tmptr);  
/* 返回值:若成功则返回日历时间,若出错则返回-1 */  
       以字符串显示时间的函数:asctime和ctime函数
 /* 以字符串显示时间的函数 */  
 /** 
  * 函数功能:以字符串显示的时间; 
  * 返回值:指向以NULL结尾的字符串指针; 
  * 函数原型: 
  * */  
/* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n" 
   that is the representation of TP in this format.  */  
 char *asctime (const struct tm *tp);  
  
/* Equivalent to `asctime (localtime (timer))'.  */  
 char *ctime (const time_t *timer);  
       以格式化显示时间的函数
/* Format TP into buf according to FORMAT. 
   Write no more than MAXSIZE characters and return the number 
   of characters written, or 0 if it would exceed MAXSIZE.  */  
 size_t strftime (char *restrict buf, size_t maxsize,  
            const char *restrict format,  
            const struct tm *restrict tp);  
/** 
 * 说明: 
 * 时间tp以格式format形式存放在大小为maxsize的缓冲数组buf中。 
 * 返回存在buf中字符数,不包括null字符; 
 * **/  

测试程序:
#include <time.h>  
#include <stdio.h>  
#include "apue.h"  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <utime.h>  
  
int main(void)  
{  
    time_t T;  
    char *ptr;  
    struct tm *time1, *time2;  
    T = time(NULL);  
    printf("time_t format:%ld\n",T);  
    time1 = localtime(&T);  
    ptr = asctime(time1);  
    printf("localtime format:%s\n",ptr);  
    time2 = gmtime(&T);  
    ptr = asctime(time2);  
    printf("gmtime format:%s\n",ptr);  
    ptr = ctime(&T);  
    printf("ctime format:%s\n",ptr);  
    T = mktime(time1);  
    printf("mktime format:%ld\n",T);  
    exit(0);  
}  

输出:

time_t format:1415101627  

localtime format:Tue Nov  4 19:47:07 2014  

  

gmtime format:Tue Nov  4 11:47:07 2014  

  

ctime format:Tue Nov  4 19:47:07 2014  

  

mktime format:1415101627  

 

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值