Sample code from jammit rework in linux:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <mntent.h> // These routines are used to access the file system description file /etc/fstab and the mounted file system description file /etc/mtab
struct mntent *mountpoint(const char *filename, struct mntent *mnt, char *buf, size_t buflen)
{
struct stat s;
FILE *      fp;
dev_t       dev;
if (stat(filename, &s) != 0) {
return NULL;
}
dev = s.st_dev;
if ((fp = setmntent(“/proc/mounts”, “r”)) == NULL) {
return NULL;
}
while (getmntent_r(fp, mnt, buf, buflen)) {
if (stat(mnt->mnt_dir, &s) != 0) {
continue;
}
if (s.st_dev == dev) {
endmntent(fp);
return mnt;
}
}
endmntent(fp);
// Should never reach here.
errno = EINVAL;
return NULL;
}

#include <sys/types.h>#include <sys/stat.h>#include <unistd.h> #include <mntent.h> // These routines are used to access the file system description file /etc/fstab and the mounted file system description file /etc/mtab
struct mntent *mountpoint(const char *filename, struct mntent *mnt, char *buf, size_t buflen){    struct stat s;    FILE *      fp;    dev_t       dev;
if (stat(filename, &s) != 0) {        return NULL;    }
dev = s.st_dev;
if ((fp = setmntent(“/proc/mounts”, “r”)) == NULL) {        return NULL;    }
while (getmntent_r(fp, mnt, buf, buflen)) {        if (stat(mnt->mnt_dir, &s) != 0) {            continue;        }
if (s.st_dev == dev) {            endmntent(fp);            return mnt;        }    }
endmntent(fp);
// Should never reach here.    errno = EINVAL;    return NULL;}

Advertisements