显示已挂载的文件系统

显示已挂载的文件系统 

/etc/fstab、/etc/mtab 和 /proc/mounts 其中任何一个, 都可以在程序中使用 getmntent() 这组函数来读取:

#include 
#include

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 結構的指针,结构中会填入适当的值。每次调用這個 static 儲存空間都會被覆蓋。等到項目都讀完了, 則傳回 NULL(這部份類似用來讀取密碼和群組檔案的常式, 參見《Linux 系統程式實例設計 》6.3 小節)。

addmntent() 可以在已開啟檔案的末端加上資訊, 它原本是給 mount 使用的。

endmntent() 的功用是关闭打开的文件。這不能只是呼叫 fclose() 而已, 因為可能還有其他與 FILE * 變數有關的內部資料結構需要清理。

hasmntopt() 是個比較特殊的函式。它會掃描第一個參數所傳入的 struct mntent, 找出它的掛載選項是否符合第二個引數。假如找到選項就傳回符合的子字串的位址;否則傳回 NULL。

在 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检查的次序 */
};

在处理挂载的文件系统时, 一般是以一个外部循环来读取 /etc/mtab, 一次处理一个 struct mntent。底下的 ch08-mounted.c 正是这样做:

01 /* ch08-mounted.c --- 打印挂载文件系统的清单 */
02
03 /* 注意:这是 GNU/Linux 特有的! */
04
05 #include 
06 #include 
07 #include /* getmntent() 的定义 */
08 #include /* getopt() 的定义 */
09
10 void process(const char *filename);
11 void print_mount(const struct mntent *fs);
12
13 char *myname;
14
15 /* main --- 处理参数 */
16
17 int main(int argc, char **argv)
18 {
19 int c;
20 char *file = "/etc/mtab"; /*要读取的文件 */
21
22 myname = argv[0];
23 while ((c = getopt(argc, argv, "f:")) != -1) {
24 switch (c) {
25 case 'f':
26 file = optarg;
27 break;
28 default:
29 fprintf(stderr, "usage: %s [-f fstab-file]\n", argv[0]);
30 exit(1);
31 }
32 }
33
34 process(file);
35 return 0;
36 }
37
38 /* process --- 从文件读取 struct mntent 结构 */
39
40 void process(const char *filename)
41 {
42 FILE *fp;
43 struct mntent *fs;
44
45 fp = setmntent(filename, "r"); /* 读取 */
46 if (fp == NULL) {
47 fprintf(stderr, "%s: %s: could not open: %s\n",
48 myname, filename, strerror(errno));
49 exit(1);
50 }
51
52 while ((fs = getmntent(fp)) != NULL)
53 print_mount(fs);
54
55 endmntent(fp);
56 }
57
58 /* print_mount --- 列印一个挂载条目 */
59
60 void print_mount(const struct mntent *fs)
61 {
62 printf("%s %s %s %s %d %d\n",
63 fs->mnt_fsname,
64 fs->mnt_dir,
65 fs->mnt_type,
66 fs->mnt_opts,
67 fs->mnt_freq,
68 fs->mnt_passno);
69 }

和之前我们看过的大部份程序不一样, 这个程序是专门针对Linux写的。许多Unix系统也有类似的程序,但是并不保证完全相同。

在預設情況下, ch08-mounted 程式是讀取 /etc/mtab, 列印出每個掛載檔案系統的相關資訊。-f 選項是用來指定要讀取的檔案, 如 /proc/mounts 或甚至 /etc/fstab。

main() 函式負責處理命令列 (第 23–32 行) 和呼叫 process() 處理指定的檔案 (這個程式遵照了我們的標準程式模版)。

process() 這次的工作是開啟檔案 (第 45 行), 以迴圈處理每個傳回的檔案系統 (第 52–53 行)。完成後關閉檔案 (第 55 行)。

print_mount() 函式則是印出在 struct mntent 裡的資訊。其輸出結果與 'cat /etc/mtab' 很類似:

$ ch08-mounted <-----執行程式
/dev/hda2 / ext3 rw 0 0
none /proc proc rw 0 0
usbdevfs /proc/bus/usb usbdevfs rw 0 0
/dev/hda5 /d ext3 rw 0 0
none /dev/pts devpts rw,gid=5,mode=620 0 0
none /dev/shm tmpfs rw 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
/dev/hda1 /win vfat rw,noexec,nosuid,nodev,uid=2076,gid=10,user=arnold 0 0

【作者: hedes

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=3810884

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值