UNIX環境高階程式設計第二版 讀書筆記6章

2021-06-17 19:06:04 字數 2910 閱讀 8951

6.1  由於歷史原因,大量系統有關的資料檔案都是ascii文字檔案。

6.2  口令檔案中shell如果為/dev/null表示阻止任何人以該使用者名義登陸。

finger -p username可以顯示使用者相關資訊。

有些系統提供了vipw命令,允許管理員編輯口令檔案。

獲取口令檔案項的函式。

struct passwd *getpwuid(uid_t uid);

struct passwd *getpwnam(const char *name);

檢視整個口令檔案

struct passwd *getpwent(void);

void setpwent(void); void endpwent(void);

在程式開始處呼叫setpwent是自我保護性措施,以便呼叫者在此之前已經呼叫getpwent開啟有關檔案的情況下,反繞有關檔案使他們定位到檔案開始處。使用完之後應該呼叫endpwent關閉檔案。

6.3  加密口令是單向演算法處理過的使用者口令副本。為使有所企圖的人難以獲得加密口令,系統將加密口令存放在陰影口令中(shadow password)的檔案中。/etc/shadow檔案

下列函式可以訪問陰影口令檔案。

struct spwd *getspnam(const char *name);

struct spwd *getspent(void);

void setspent(void);

void endspent(void);

6.4  檢視組名或組id。

struct group *getgrgid(gid_t gid);

struct group *getgrnam(const char *name);

搜尋整個組檔案。

struct *getgrent(void);

void setgrent(void);

void endgrent(void);

6.5  早期使用者的組經常改動,後引入附加組,可以有多個。

獲取和設定附加組。

int getgroups(int gidsetsize, gid_t grouplist);返回組。

int setgroups(int ngroups, const gid_t grouplist);由超級使用者呼叫為程序設定附加組id表。

int initgroups(const char *username, gid_t basegid);讀取整個組檔案,然後對username確定其組的成員關係,並呼叫setgroups為該使用者初始化附加組id表。

6.6  檢視不同系統的實現的區別

6.7  /etc/services記錄各網路伺服器所提供的服務的資料檔案。

/etc/protocols記錄協議資訊的資料檔案。

/etc/networks記錄網路資訊的資料檔案。

對於每個資料檔案,一般都有至少三個函式。

2)set函式:開啟資料檔案,然後反繞該檔案。如果希望在檔案起始處開始處理,使用此函式。

3)end函式:關閉相應資料檔案。

6.8  登陸賬戶記錄

大多數unix提供下列資料檔案:

utmp檔案:記錄當前登陸進系統的各個使用者;/var/run/utmp

wtmp檔案:跟蹤各個登入和登出事件;/var/log/wtmp

6.9  獲取當前主機和作業系統有關資訊。

int uname(struct utsname *name);作業系統名字,節點名字,當前release,當前版本,硬體型別。

int gethostname(char *name, int namelen);只返回主機名,通常是tcp/ip網路上的主機的名字。對應hostname命令。

6.10 unix核心提供的時間是自utc公元2023年1月1日00:00:00以來的秒數。

time_t time(time_t *calptr);返回當前時間和日期。

int gettimeofday(struct timeval *restrict tp, void *restrict tzp);提供最高位微妙級的時間解析度。

struct tm *gmtime(const time_t *calptr);將日曆時間轉換成國際標準時間。

struct tm *localtime(const time_t *calptr); 將日曆時間轉換為本地時間。

time_t mktime(struct tm *tmptr);以本地時間年月日做引數,將其轉換成time_t值。

char *asctime(const struct tm *tmptr);

char *ctime(const time_t *calptr);產生大家熟悉的26位元組的字串,與date命令輸出型別。

最後乙個函式,格式化輸出時間。

UNIX環境高階程式設計第二版 讀書筆記5章

5.1 標準i o由dennis ritchie在1975年左右編寫。5.2 第三章的i o都是針對檔案描述符。而標準i o庫是圍繞stream,開啟或建立檔案時,我們已經使乙個stream與檔案相關聯。stream的定向決定了讀寫的字元是單字元還是多位元組。在未定向的流上使用i o函式可以改變定向...

修改《Unix環境高階程式設計第二版》程式清單4 7

修改了 unix環境高階程式設計第二版 程式清單4 7,使用chdir變換當前工作目錄,使用檔案名字而不是檔案路徑進行處理。通過測試,執行效率提高了。測試環境為centos 6.5 include apue.h include stdio.h include fcntl.h include unis...

UNIX環境高階程式設計 第二章

這一節介紹了三個重要的標準 由於c語言在unix系統中的地位不言而喻,c語言的標準化也是unix標準化的重要組成部分。iso c標準當中包括了 時間線 1989 發布 1999 更新 2001 2004 2007 勘誤 2011 更新 posix portable operating system ...