getmntent使用例

Man page of GETMNTENT


/etc/mtabに列挙される情報を読み出すためのインタフェースの
ようである。mountコマンドを引数なしで実行した場合の一覧表示に
利用されているようです。

コード

参考のために、読み出されてセットされる構造体も載せておきます。

#include <stdio.h>
#include <mntent.h>

#if 0
struct mntent {
    char *mnt_fsname;   /* name of mounted file system */
    char *mnt_dir;      /* file system path prefix */
    char *mnt_type;     /* mount type (see mntent.h) */
    char *mnt_opts;     /* mount options (see mntent.h) */
    int   mnt_freq;     /* dump frequency in days */
    int   mnt_passno;   /* pass number on parallel fsck */
};
#endif

int main()
{
    FILE* fstab = setmntent("/etc/mtab", "r");
    struct mntent *e;
    const char *devname = NULL;

    while ((e = getmntent(fstab)) != NULL) {
        printf("@@ fsname=%s, dir=%s\n", e->mnt_fsname, e->mnt_dir);
    }

    endmntent(fstab);
    return 0;
}

結果

% gcc getmntent.c
% ./a.out
@@ fsname=/dev/sda1, dir=/
@@ fsname=proc, dir=/proc
@@ fsname=sysfs, dir=/sys
@@ fsname=none, dir=/sys/fs/fuse/connections
@@ fsname=none, dir=/sys/kernel/debug
@@ fsname=none, dir=/sys/kernel/security
@@ fsname=udev, dir=/dev
@@ fsname=devpts, dir=/dev/pts
@@ fsname=tmpfs, dir=/run
@@ fsname=none, dir=/run/lock
@@ fsname=none, dir=/run/shm
@@ fsname=none, dir=/run/user
@@ fsname=/run/shm, dir=/mnt/ramdisk