I need to parse table of mounted filesystems without using /proc/mounts since I'm parsing it to determine where proc file system is mounted. How can I do it?
I've googled it, but all answers was to use /proc
And why people are so sure that procfs is mounted to default place?
I'm asking out of curiosity. I understand that /proc is a standard de-facto, many tools use it and so /proc is anchored pretty good.
Linux API is consistent towards filesystem paths, all really needed information is passed via environmental variables, making possible to alter library and executable files, configuration files and so on.
I'm curios is it possible to detec PROC_PATH
with no prior knowledge about it? I may do ftw with statvfs used as callback, but this is so ungraceful. There definetly should be more straight way.
How about df -k
?
google leads me to this too.
on mac you have diskutil list
.
if you want to figure out what syscalls you need to perform in your c
code use strace
to find it out.
Another approach assuming you have access to /
root directory used Petesh code and llolydm code:
#include <stdlib.h>
#include <mntent.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
void listdir(const char *name, int level)
{
DIR *dir;
struct dirent *entry;
struct mntent *ent;
FILE *aFile;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%*s[%s]\n", level*2, "", entry->d_name);
listdir(path, level + 1);
}
else {
printf("%*s- %s\n", level*2, "", entry->d_name);
aFile = setmntent(entry);
while (NULL != (ent = getmntent(aFile))) {
printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
}
endmntent(aFile);
}
} while (entry = readdir(dir));
closedir(dir);
}
int main(void)
{
listdir("/", 0);
return 0;
}