C語言pututline()函數:將utmp記錄寫入檔案
標頭檔:
定義函數:
void pututline(struct utmp *ut);
函數說明:pututline()用來將參數ut 的utmp 結構記錄到utmp 檔案中. 此函數會先用getutid()來取得正確的寫入位置, 如果沒有找到相符的記錄則會加入到utmp 檔案尾.
附加說明:需要有寫入/var/run/utmp 的許可權
範例
#include <utmp.h>main(){ struct utmp ut; ut.ut_type = USER_PROCESS; ut.ut_pid = getpid(); strcpy(ut.ut_user, "kids"); strcpy(ut.ut_line, "pts/1"); strcpy(ut.ut_host, "www.gnu.org"); pututline(&ut);}
執行:
//執行範例後用指令who -l 觀察root pts/0 dec9 19:20kids pts/1 dec12 10:31(www.gnu.org)root pts/2 dec12 13:33
C語言getutline()函數:檔案尋找函數(從utmp檔案中尋找特定的)
標頭檔:
定義函數:
struct utmp * getutline(struct utmp *ut);
函數說明:getutline()用來從目前utmp 檔案的讀寫位置逐一往後搜尋ut_type 為USER_PROCESS 或LOGIN_PROCESS 的記錄, 而且ut_line 和ut->ut_line 相符. 找到相符的記錄便將該資料以utmp 結構返回。
傳回值:返回 utmp 結構資料, 如果返回NULL 則表示已無資料, 或有錯誤發生.
範例
#include <utmp.h>main(){ struct utmp ut, *u; strcpy(ut.ut_line, "pts/1"); while((u = getutline(&ut))) { printf("%d %s %s %s \n", u->ut_type, u->ut_user, u->ut_line, u->ut_host); }}
執行:
C語言getutid()函數:從utmp檔案中尋找特定的記錄
標頭檔:
定義函數:
strcut utmp *getutid(strcut utmp *ut);
函數說明:
getutid()用來從目前utmp 檔案的讀寫位置逐一往後搜尋參數ut 指定的記錄。
1、如果ut->ut_type 為RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME 其中之一則尋找與ut->ut_type 相符的記錄;
2、若ut->ut_type為INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS 或DEAD_PROCESS 其中之一, 則尋找與ut->ut_id相符的記錄. 找到相符的記錄便將該資料以utmp 結構返回.
傳回值:返回 utmp 結構資料, 如果返回NULL 則表示已無資料, 或有錯誤發生.
範例
#include <utmp.h>main(){ struct utmp ut, *u; ut.ut_type=RUN_LVL; while((u = getutid(&ut))) { printf("%d %s %s %s\n", u->ut_type, u->ut_user, u->ut_line, u->ut_host); }}
執行: