您的位置:首页 > 其它

第六章 系统数据文件和信息

2014-10-10 09:45 288 查看
6.2 口令文件
和口令文件对应的字段包含在<pwd.h>的结构体passwd中。
#include<pwd.h>
struct passwd * getpwuid(uid_t uid);
struct passwd * getpwnam(const char * name); //两个函数的返回值:若成功则返回指针,若出错则返回NULL

如果要查看的只是登录名或用户ID,那么这两个POSIX.1函数能满足要求,但是也有些程序要查看整个口令文件。下列三个函数则可用于此种目的:
#include <pwd.h>
struct passwd* getpwent(void); 返回值:若成功则返回指针,若出错或达到文件结尾则返回NULL
void setpwent(void);
void endpwent(void);

#include <pwd.h>  #include <stddef.h>  #include <string.h>    struct passwd *  getpwnam(const char *name)  {      struct passwd  *ptr;        setpwent();      while ((ptr = getpwent()) != NULL)          if (strcmp(name, ptr->pw_name) == 0)              break;      /* found a match */      endpwent();      return(ptr);    /*a ptr is NULL if no match found */  }


6.3 阴影口令
#include <shadow.h>
struct spwd* getspnam(const char * name);
struct spwd* getspent(void);
两个函数返回值:若成功则返回指针,若出错则返回NULL
void setspent(void);
void endspent(void);

6.4组文件
UNIX组文件,这些字段包含在<grp.h>中所定义的group结构中。
#include<grp.h>
struct group * getgrgid(gid_t gid);
struct group * getgrnam(congst char * name);
两个函数返回值:若成功则返回指针 若出错则返回NULL
#include <grp.h>
struct group * getgrent(void); 返回值:若成功则返回指针,若出错或到达文件结尾则返回NULL
void setgrent(void);
void endgrent(void);

6.5 附加组ID
#include < unistd.h>
int getgroups(int gidsetsize,gid_t grouplist[]); 返回值:若成功则返回附加组ID数, 若出错则返回-1
#include <grp.h>
#include<unistd.h>
int setgroups(int ngroups, const gid_t grouplist[]);
#include <grp.h>
#include <unistd.h>
int initgroups(const char * username,gid_t basegid);
两个函数返回值:若成功则返回0 错出错则返回-1

6.8 登录帐号记录
大多数UNIX系统都提供下来连个数据文件:utmp文件,它记录当前登录进系统的各个用户;
wtmp文件,它跟踪各个登录和注销事件
struct utmp{
char ut_line[8];
char ut_name[8];
long ut_time;
}
登录时 login程序填写此类型结构,然后将其写入到utmp文件中,同时也将其添写到wtmp文件中。
注销时 init进程将utmp文件中相应的记录擦除,并将一个新纪录添写到wtmp文件中。
who(1)程序读utmp文件,并以可读格式打印其内容。
后来UNIX提供了last命令,它读wtmp文件并打印所选择的记录。

6.9 系统标识
#include < sys/utsname.h>
int uname(struct utsname * name); //返回值:若成功则返回非负值,若出错则返回-1
struct utsname{
char sysname[]; //name of the operating system
char nodename[]; //name of this node
char release[]; //current release of operating system
char version[]; // current versionof this release
char machine[];// name of hardware type
};
#incluce <unistd.h>
int gethostname(char * name , int namelen); //返回值:若成功则返回0 若出错则返回-1

6.10 时间和日期例程
time_t time(time_t * calptr); //返回值:若成功则返回时间值,若出错则返回-1
time_t自国际标准时间公元1970年1月1日00:00:00以来经过的秒数。这种秒数是以数据类型time_t表示的。我们称它为日历时间。丽日时间包括时间和日期。

gettimeofday提供了更高的分辨率
#include <sys/time.h>
int gettimeofday(struct timeval * restrict tp, void * restrict tzp);
struct timeval{
time_t tv_sec; /*seconds*/
long tv_usec; /*microsecones*/
}





#include<time.h>
struct tm * gmtime(const time_t * calptr);
struct tm * localtime(const time_t * calptr); //两个函数返回值:指向tm结构的指针
区别:localtime将日历时间转换成本地时间(考虑到本地时区和夏时制标志) 而gmtime则将日历转换成国际标准时间为年,月 日 时 分 秒 周日

#include<time.h>
time_t mktime(struct tm* tmptr); 返回值:若成功则返回日历事件, 若出错则返回-1

#include <time.h>
char * asctime(const struct tm* tmptr);
char * ctime(const time_t calptr); 连个函数的返回值:指向以NULL结尾的字符串的指针

#include<time.h>
size_t strftime(char * restrict buf, size_t maxsize,const char * restrict format, const sturct tm * restrict tmprt);
返回值:若有空间则返回存入数组的字符数,否则返回0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: