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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/youngyangyang04/article/details/47842835

6.1 引言

UNIX系统的正常运行需要使用大量与系统有关的数据文件,例如, 口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件。用户每次登陆UNIX系统,以及每次执行ls -l命令是都要使用口令文件。
对于这些数据文件的可移植接口是本章的主题。本章包括了系统标示函数、时间、日期函数。

6.2 口令文件(Password File)

给出用户登录名或数值用户ID后,这两个用户就可以查看相关项
#Include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
getpwuid函数由ls(1)程序使用,在输入登录名是,getpwnam函数由login(1)程序使用

下列函数可以用来查看整个口令文件
#include<pwd.h>
struct passwdd *getpwent(void);
void setpwent(void);
void endpwent(void);
调用getpwent时,它返回口令文件中的下一个纪录项
函数setpwent反绕它所使用的文件,endpwent则关闭这些文件。
使用getpwent查看完口令文件后,一定要调用endpwent关闭这些文件

getpwnam函数的一个实现
#include <pwd.h>
#include <stddef.h>
#include <string.h>
struct passwd *getpwnam(const char *name)
{
      struct passwd *ptr;
      setpwent();
      whlie((ptr = getpwent())!=NULL)
            if(strcmp(name,ptr->pw_name)==0)
                     break;
      endpwent(); 
      return(ptr);
}

setpwent反绕有关文件使它们定位到文件开始处(自我保护措施)。getpwnam和getpwuid完成后不应使文件处于打开状态,所以调用endpwent关闭它们。

6.3 阴影口令(Shadow Passwords)

某些系统讲加密口令存放在另一个通常称为 阴影口令的文件中。该文件至少包含用户名和加密口令。与该口令相关的其他信息也可以存放在该文件中。

阴影口令文件不应是一般用户可以读取的。仅有少数几个程序需要访问加密口令,如login(1)和passwd(1)。有了阴影口令普通口令文件/etc/passwd可以让用户自由读取了

有另一组函数可用于访问阴影口令文件
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
void setpent(void);
void endspent(void);

6.4 组文件

用下列函数来查看组名或数值组ID
#include <grp.h>
struct group *getgrgid(gid_t gid);
struct group *gergrnam(const char *name);

如同对 口令文件进行操作的函数一样

如果需要搜索整个组文件,则必须使用另外几个函数
#include <grp.h>
struct group *getgrent(void);
void setgrent(void);
void endgrent(void);

6.5 附属组ID

6.6 实现区别

6.7 其他数据文件

至此讨论了两个系统数据文件-口令文件和组文件。UNIX系统还使用很多其他文件。例如,网络服务器所提供服务的数据文件(/etc/services),记录协议信息的数据文件(/etc/protocols), 这写数据文件的接口都与上述对口令文件的组文件的相似
分别是 get函数(读下一个记录), set函数(反绕该文件,希望在相应文件起始处开始处理), end函数(关闭相应数据文件)

6.8 登录账户记录

utmp文件记录当前登录到系统的各个用户;wtmp文件跟踪各个登陆和注销事件

6.9 系统标识

uname函数返回与主机和操作系统有关的信息
#include <sys/utname.h>
int uname (struct utsname *name);

utsname的结构

6.10 时间和日期例程(Time and Date Routines)

time函数返回当前时间和日期
#include <time.h>
time_t time(time_t *calptr);

clock_gettime函数可用于获取指定时钟的时间,把时间保存再timespec结构中
#include <sys/time.h>
int clock_gettime(clockid_t clock_id, struct timespec *tsp);
当时钟ID设置为CLOCK_REALTIME时,clock_gettinng函数提供了与time函数类似的功能,clock_getting可能比time函数提供更高的精度的时间

#include<sys/time.h>
int clock_getres(clockid_t clock_id, struct timespec *tsp);
clock_getres函数把参数tsp指向的timespec结构初始化为与clock_id参数相对应的时钟精度

对特定的时钟设置时间
#include<sys/time.h>
int clock_settime(clockid_t clock_id, const struct timespec *tsp);

#include <time.h>
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);
两个函数localtime 和gmtime将日历时间转化成分解的时间,并将其存放再一个tm结构中

localtime和gmtime之间的区别是:localtime将日历时间转换成本地时间( 考虑到本地时区和夏令时标志),而gmtime则将日历时间转化成协调统一时间的。

函数mktime以本地时间的年,月,日等作为菜蔬,将其变成time_t值
#include<time.h>
time_t mktime(struct tm *tmptr);

函数strftime是一个类似printf的时间值函数,可以通过可用的多个参数指定产生的字符串。
#include <time.h>
size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);
size_t strftime_l(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr, locale_t locale);
ISO C规定的转换说明(conversion specifiers)



strptime函数是strftime的反过来的版本,把字符串转化成分解的时间

6.11 小结

所有UNIX系统都使用 口令文件和组文件。介绍了 阴影口令,以及 它可以增强系统的安全性附属组ID提供了一个用户同事可以参加多个组的方法。还介绍了 大多数系统提供的访问其他与系统有关数据文件的类似函数。讨论一个POSIX.1的 系统标识函数,应用程序使用他们以标识它再何种系统上运行。最后说一下 与时间和日期有关的一些函数

猜你喜欢

转载自blog.csdn.net/youngyangyang04/article/details/47842835