C語言pututline()函數:將utmp記錄寫入文件
頭文件:
#include <utmp.h>
定義函數:
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文件中查找特定的)
頭文件:
#include <utmp.h>
定義函數:
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); }}
執行:
7 root pts/1
C語言getutid()函數:從utmp文件中查找特定的記錄
頭文件:
#include <utmp.h>
定義函數:
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); }}
執行:
1 runlevel -
新聞熱點
疑難解答
圖片精選