在 struct mntent 中的成員與 /etc/fstab 檔案中的條目是直接對應的。它的內容如下:
struct mntent { char *mnt_fsname; /* 掛載的檔案系統的名字 */ char *mnt_dir; /* 掛載點 */ char *mnt_type; /* 檔案系統類型:ufs、nfs 等 */ char *mnt_opts; /* 選項,以逗號為分隔字元 */ int mnt_freq; /* Dump 的頻率(以天為單位) */ int mnt_passno; /* fsck檢查的次序 */ }; FILE *setmntent(const char *filename, const char *type); struct mntent *getmntent(FILE *filep); int addmntent(FILE *filep, const struct mntent *mnt); int endmntent(FILE *filep); char *hasmntopt(const struct mntent *mnt, const char *opt); |
setmntent() 是開啟包含掛載點項目的檔案, 其中的 filename 參數是要開啟的檔案名稱, type 參數就像 fopen() 的第二個參數, 代表唯讀、唯寫, 或讀寫皆可的存模數式 。返回FILE*。
getmntent() 則是循序讀取整個檔案,傳回指向 static struct mntent 結構的指標,結構中會填入適當的值。
addmntent() 可以在已開啟檔案的末端加上資訊,它原本是給 mount 使用的。
endmntent() 的功用是關閉開啟的檔案。這不能只是呼叫 fclose() 而已,因為可能還有其它與FILE * 有關的內部資料結構需要清理。
hasmntopt() 是個比較特殊的函式。它會掃描第一個參數所傳入的struct mntent,找出它的掛載選項是否符合第二個引數。假如找到選項就傳回符合的子字串的位址;否則傳回NULL。
/etc/fstab、/etc/mtab 和 /proc/mounts 其中任何一個, 都可以在程式中使用 getmntent() 這組函數來讀取
eg:
#include <mntent.h>
struct mntent* mnt;
FILE* fp;
fp = setmntent("/dev/mmc/mmcblk0", "r");
if ( !fp )
{
return FALSE;
}
if(mnt=getmntent(fp) )
{
#if 1
printf("woosoori[%s:%d] mnt->mnt_fsname=%s/n",__FUNCTION__,__LINE__, mnt->mnt_fsname);
printf("woosoori[%s:%d] mnt->mnt_dir=%s/n",__FUNCTION__,__LINE__, mnt->mnt_dir);
printf("woosoori[%s:%d]mnt->mnt_type=%s/n",__FUNCTION__,__LINE__,mnt->mnt_type);
printf("woosoori[%s:%d] mnt->mnt_opts=%s/n",__FUNCTION__,__LINE__, mnt->mnt_opts);
printf("woosoori[%s:%d]mnt->mnt_freq=%d/n",__FUNCTION__,__LINE__,mnt->mnt_freq);
printf("woosoori[%s:%d]mnt->mnt_passno=%d/n",__FUNCTION__,__LINE__,mnt->mnt_passno);
#endif
endmntent(fp);
return TRUE;
}