blob: dbf0b27cfd8c5d22944f9088b8c08e131b7cc3c4 [file] [log] [blame]
landley09ea7ac2006-10-30 01:38:00 -05001/* getmountlist.c - Get a linked list of mount points, with stat information.
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 */
landley4f344e32006-10-05 16:18:03 -04005
6#include "toys.h"
7
8#include <mntent.h>
9
Rob Landley078d31c2013-05-10 18:57:01 -050010// Get list of mounted filesystems, including stat and statvfs info.
11// Returns a reversed list, which is good for finding overmounts and such.
landley4f344e32006-10-05 16:18:03 -040012
Rob Landley078d31c2013-05-10 18:57:01 -050013struct mtab_list *xgetmountlist(void)
landley4f344e32006-10-05 16:18:03 -040014{
Rob Landley7aa651a2012-11-13 17:14:08 -060015 struct mtab_list *mtlist, *mt;
Rob Landley078d31c2013-05-10 18:57:01 -050016 struct mntent *me;
17 FILE *fp;
landley4f344e32006-10-05 16:18:03 -040018
Rob Landley078d31c2013-05-10 18:57:01 -050019 if (!(fp = setmntent("/proc/mounts", "r"))) perror_exit("bad /proc/mounts");
20
21 for (mtlist = 0; me = getmntent(fp); mtlist = mt) {
22 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
23 strlen(me->mnt_dir) + strlen(me->mnt_type) + 3);
24 mt->next = mtlist;
25
26 // Collect details about mounted filesystem (don't bother for /etc/fstab).
27 stat(me->mnt_dir, &(mt->stat));
28 statvfs(me->mnt_dir, &(mt->statvfs));
29
30 // Remember information from /proc/mounts
31 mt->dir = stpcpy(mt->type, me->mnt_type) + 1;
32 mt->device = stpcpy(mt->dir, me->mnt_dir) + 1;
33 strcpy(mt->device, me->mnt_fsname);
Rob Landley7aa651a2012-11-13 17:14:08 -060034 }
Rob Landley491eb802012-11-17 22:06:00 -060035 endmntent(fp);
Rob Landley078d31c2013-05-10 18:57:01 -050036
Rob Landley7aa651a2012-11-13 17:14:08 -060037 return mtlist;
landley4f344e32006-10-05 16:18:03 -040038}