struct mntent sd/tf卡

在 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章