getmntent_r - Linux


Overview

getmntent_r retrieves the next entry from the mounted file system list. It can be used to enumerate all mounted file systems in the system in a loop.

Syntax

#include <mntent.h>

struct mntent *getmntent_r(FILE *file, struct mntent *mnt, char *buf, int len);

Options/Flags

None.

Examples

List mounted file systems

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

int main() {
    FILE *f;
    struct mntent *ent;
    char buf[1024];

    f = setmntent("/proc/mounts", "r");
    if (f == NULL) {
        perror("setmntent");
        return 1;
    }

    while ((ent = getmntent_r(f, ent, buf, sizeof(buf)))) {
        printf("%s on %s type %s\n",
               ent->mnt_fsname, ent->mnt_dir, ent->mnt_type);
    }

    endmntent(f);
    return 0;
}

Common Issues

  • Incorrect file path: Ensure you specify the correct path to the mounted file system list, typically /proc/mounts.
  • Invalid buffer: The length of the buffer (len) should be large enough to accommodate the maximum possible entry size.

Integration

getmntent_r can be used in conjunction with other commands to perform advanced tasks:

  • findmnt: List mounted file systems based on specific criteria, using getmntent_r to retrieve the entries.
  • df: Display disk usage information for mounted file systems, utilizing getmntent_r to enumerate the systems.

Related Commands

  • mount: Mount a file system.
  • umount: Unmount a file system.
  • swapon: Enable swap space on a file or device.