Date:2009-05-13 09:57:19
Content:系統資料檔案和資訊
一、存取系統資料檔案的類似常式
說明 |
資料檔案 |
標頭檔 |
結構 |
附加的關鍵字尋找函數 |
passwords |
/etc/passwd |
<pwd.h> |
passwd |
getpwnam, getpwuid |
groups |
/etc/group |
<grp.h> |
group |
getgrnam, getgrgid |
shadow |
/etc/shadow |
<shadow.h> |
spwd |
getspnam |
hosts |
/etc/hosts |
<netdb.h> |
hostent |
gethostbyname, gethostbyaddr |
networks |
/etc/networks |
<netdb.h> |
netent |
getnetbyname, getnetbyaddr |
protocols |
/etc/protocols |
<netdb.h> |
protoent |
getprotobyname, getprotobynumber |
services |
/etc/services |
<netdb.h> |
servent |
getservbyname, getservbyport |
二、函數介紹
1、擷取口令檔案項目的函數
#include <pwd.h><br />//成功則返回指標,否則返回NULL<br />struct passwd *getpwuid(uid_t uid);<br />struct passwd *getpwnam(const char *name);
2、查看整個口令檔案
#include <pwd.h></p><p>struct passwd *getpwent(void);<br />//Returns: pointer if OK, NULL on error or end of file </p><p>void setpwent(void);</p><p>void endpwent(void);</p><p>
3、訪問陰影口令檔案
#include <shadow.h><br />//Both return: pointer if OK, NULL on error<br />struct spwd *getspnam(const char *name);</p><p>struct spwd *getspent(void);<br />void setspent(void);<br />void endspent(void);</p><p>
4、查看組名或數值組ID
#include <grp.h><br />//Both return: pointer if OK, NULL on error<br />struct group *getgrgid(gid_t gid);</p><p>struct group *getgrnam(const char *name);</p><p>
5、如果需要搜尋整個組檔案,則需要使用另外幾個函數
#include <grp.h><br />//Returns: pointer if OK, NULL on error or end of file<br />struct group *getgrent(void);</p><p>void setgrent(void);<br />void endgrent(void);</p><p>
6、為了擷取和設定附加組ID
#include <unistd.h><br />//Returns: number of supplementary group IDs if OK, 1 on error<br />int getgroups(int gidsetsize, gid_t grouplist[]);</p><p>//Both return: 0 if OK, 1 on error<br />#include <grp.h> /* on Linux */<br />#include <unistd.h> /* on FreeBSD, Mac OS X, and Solaris */</p><p>int setgroups(int ngroups, const gid_t grouplist[]);</p><p>#include <grp.h> /* on Linux and Solaris */<br />#include <unistd.h> /* on FreeBSD and Mac OS X */</p><p>int initgroups(const char *username, gid_t basegid);<br />
7、返回與當前主機和作業系統有關的資訊
#include <sys/utsname.h><br />//Returns: non-negative value if OK, 1 on error<br />int uname(struct utsname *name);</p><p>
8、返回主機名稱
#include <unistd.h><br />//Returns: 0 if OK, 1 on error<br />int gethostname(char *name, int namelen);</p><p>
9、返回當前日期和時間
#include <time.h></p><p>time_t time(time_t *calptr);<br />
#include <sys/time.h></p><p>int gettimeofday(struct timeval *restrict tp, void *restrict tzp);<br />
#include <time.h><br />//Both return: pointer to broken-down time</p><p>struct tm *gmtime(const time_t *calptr);</p><p>struct tm *localtime(const time_t *calptr);</p><p>
#include <time.h><br />//Returns: calendar time if OK, 1 on error</p><p>time_t mktime(struct tm *tmptr);</p><p>
#include <time.h><br />//Both return: pointer to null-terminated string<br />//Tue Feb 10 18:27:38 2004/n/0<br />char *asctime(const struct tm *tmptr);</p><p>char *ctime(const time_t *calptr);</p><p>
#include <time.h><br />//Returns: number of characters stored in array if room, 0 otherwise<br />size_t strftime(char *restrict buf, size_t maxsize,<br /> const char *restrict format,<br /> const struct tm *restrict tmptr);</p><p>