当前位置:首页 > 通信资讯 > 正文

C语言pututline()函数:将utmp记录写入文件
头文件:

?
1 #include <utmp.h>

定义函数:

?
1 void pututline(struct utmp *ut);

函数说明:pututline()用来将参数ut 的utmp 结构记录到utmp 文件中. 此函数会先用getutid()来取得正确的写入位置, 如果没有找到相符的记录则会加入到utmp 文件尾.
附加说明:需要有写入/var/run/utmp 的权限

范例

?
1 2 3 4 5 6 7 8 9 10 11 #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); }

执行:

?
1 2 3 4 //执行范例后用指令who -l 观察 root pts/0 dec9 19:20 kids pts/1 dec12 10:31(www.gnu.org) root pts/2 dec12 13:33

C语言getutline()函数:文件查找函数(从utmp文件中查找特定的)
头文件:

?
1 #include <utmp.h>

定义函数:

?
1 struct utmp * getutline(struct utmp *ut);

函数说明:getutline()用来从目前utmp 文件的读写位置逐一往后搜索ut_type 为USER_PROCESS 或LOGIN_PROCESS 的记录, 而且ut_line 和ut->ut_line 相符. 找到相符的记录便将该数据以utmp 结构返回。

返回值:返回 utmp 结构数据, 如果返回NULL 则表示已无数据, 或有错误发生.

范例

?
1 2 3 4 5 6 7 8 9 10 #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); } }

执行:

?
1 7 root pts/1

C语言getutid()函数:从utmp文件中查找特定的记录
头文件:

?
1 #include <utmp.h>

定义函数:

?
1 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 则表示已无数据, 或有错误发生.

范例

?
1 2 3 4 5 6 7 8 9 10 #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 1 runlevel -
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。