目录
2.3 gmtime、localtime、mktime、ctime、asctime、strftime
第一部分:获取系统文件的数据
1. 口令文件:/etc/passwd
1.0 什么是口令文件?
存放用户账户信息的文件,就是口令文件。
1.1 文件内容
1.1.1 账户所包含的信息
以 gsx:x:1000:1000:gsx,,,:/home/gsx:/bin/bash 为例
分为7个字段,字段间使用:分隔
用户名:密码:用户ID:用户所在组的组ID:注释:用户主目录的路径:shell程序的路径
1.1.2 getpwuid、getpwnam
这两个函数的作用是,获取passwd文件中的账户数据,其实,我们也可以调用open、read
getpwuid、getpwnam这个两个函数是c库函数,这两个函数也是靠封装open、read等函数来实现的。
#include <sys/types.h> #include <pwd.h> struct passwd *getpwuid(uid_t uid); struct passwd *getpwnam(const char *name);
1.2 阴影文件:/etc/shadow
1.2.1 里面放的是什么
放的是加密后的密码。
1.2.2 为什么密码要单独存放,而且还要加密
(1)密码不能明文存放
- 注册用户时,用户密码会被加密,而且使用的是不可逆加密算法
- 所谓不可逆算法,就是不能通过密文,反过来推算出原文。
(2)密码单独放在一个文件中(/etc/shadow),而且普通用户无法查看
(3)文件中的内容
(4)Linux也提供了的相应的API,用于获取/etc/shadow中密码信息
1.3 组文件:/etc/group
1.3.1 放的是什么
/etc/group里面放的就是各种用户组相关的信息。
这个文件,普通用户也只能读,不能写。
1.3.2 文件内容
1.3.3 getgrgid、getgrnam函数
这两个函数同样是库函数,工作原理和getpwduid、getpwnam完全一样。
#include <sys/types.h> #include <grp.h> struct group *getgrnam(const char *name); struct group *getgrgid(gid_t gid);
1.4 其它系统文件
比如:
/etc/services:记录了各种网络服务器提供的服务。
/etc/protocols:记录了各种的协议。
/etc/networks:记录网络信息
第二部分:获取系统时间
什么是系统时间:就是年月日 时分秒,有OS时,这个时间就是系统提供的,因此成为系统时间。
2.1 Linux的计时方式
Linux系统记录的时间,是从公元1970年1月1日00:00:00开始,到现在的总秒数,每过1秒,总秒数就会+1。
这个总秒数 + 1970年1月1日00:00:00这个起点时间,即可计算得到当的前时间。
2.2 time
#include <time.h> time_t time(time_t *t);
2.3 gmtime、localtime、mktime、ctime、asctime、strftime
2.3.1 调用关系
2.3.2 ctime
#include <time.h> char *ctime(const time_t *timep);
2.3.3 gmtime、localtime、mktime
(1)gmtime
#include <time.h> struct tm *gmtime(const time_t *timep);
(2)localtime
#include <time.h> struct tm *localtime(const time_t *timep);
功能与gmtime完全一样,只不过是转为本地时间的年月日时分秒,我们的本地时间是北京时间。
(3)mktime
#include <time.h> time_t mktime(struct tm *tm);
2.3.4 asctime、strftime
(1)asctime
#include <time.h> char *asctime(const struct tm *tm);
(2)strftime
#include <time.h> size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);