在C語言中對utmp檔案進行尋找和寫入操作的函數小結_C 語言

來源:互聯網
上載者:User

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 -

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.