目錄
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);