C語言getgrent()函數(shù):從組文件中取得賬號的數(shù)據(jù)
頭文件:
#include <grp.h> #include <sys/types.h>
定義函數(shù):
struct group * getgrent(void);
函數(shù)說明:getgrent()用來從組文件(/etc/group)中讀取一項(xiàng)組數(shù)據(jù), 該數(shù)據(jù)以group 結(jié)構(gòu)返回. 第一次調(diào)用時(shí)會(huì)取得第一項(xiàng)組數(shù)據(jù), 之后每調(diào)用一次就會(huì)返回下一項(xiàng)數(shù)據(jù), 直到已無任何數(shù)據(jù)時(shí)返回NULL.
struct group{ char *gr_name; //組名稱 char *gr_passwd; //組密碼 gid_t gr_gid; //組識(shí)別碼 char **gr_mem; //組成員賬號}
返回值:返回 group 結(jié)構(gòu)數(shù)據(jù), 如果返回NULL 則表示已無數(shù)據(jù), 或有錯(cuò)誤發(fā)生.
附加說明:getgrent()在第一次調(diào)用時(shí)會(huì)打開組文件, 讀取數(shù)據(jù)完畢后可使用endgrent()來關(guān)閉該組文件.
錯(cuò)誤代碼:
ENOMEM:內(nèi)存不足, 無法配置group 結(jié)構(gòu)。
范例
#include <grp.h>#include <sys/types.h>main(){ struct group *data; int i; while((data = getgrent()) != 0) { i = 0; printf("%s:%s:%d:", data->gr_name, data->gr_passwd, data->gr_gid); while(data->gr_mem[i]) printf("%s, ", data->gr_mem[i++]); printf("/n"); } endgrent();}
執(zhí)行:
root:x:0:root,bin:x:1:root, bin, daemon,daemon:x:2:root, bin, daemon,sys:x:3:root, bin, adm,adm:x:4:root, adm, daemontty:x:5disk:x:6:rootlp:x:7:daemon, lpmem:x:8kmem:x:9:wheel:x:10:rootmail:x:12:mailnews:x:13:newsuucp:x:14:uucpman:x:15:games:x:20gopher:x:30dip:x:40ftp:x:50nobody:x:99
C語言setgrent()函數(shù):從頭讀取組文件中的組數(shù)據(jù)
頭文件:
#include <grp.h> #include <sys/types.h>
定義函數(shù):
void setgrent(void);
函數(shù)說明:setgrent()用來將getgrent()的讀寫地址指回組文件開頭。
用法參考 setpwent().
C語言endgrent()函數(shù):關(guān)閉文件(關(guān)閉組文件)
相關(guān)函數(shù):
getgrent, setgrent
頭文件:
#include <grp.h> #include <sys/types.h>
定義函數(shù):
void endgrent(void);
函數(shù)說明:endgrent()用來關(guān)閉由getgrent()所打開的密碼文件。
范例請參考setgrent().
新聞熱點(diǎn)
疑難解答
圖片精選