擷取linux的檔案系統相關資訊的知識點

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.