blob: c863de5df0c435edc8bdc8dc79f72f650f5d609b [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 Landley42adb7a2013-08-30 17:34:24 -050013struct mtab_list *xgetmountlist(char *path)
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 Landley42adb7a2013-08-30 17:34:24 -050019 if (!path) path = "/proc/mounts";
20 if (!(fp = setmntent(path, "r"))) perror_exit("bad %s", path);
Rob Landley078d31c2013-05-10 18:57:01 -050021
Rob Landley00474ef2013-05-14 20:22:23 -050022 // The "test" part of the loop is done before the first time through and
23 // again after each "increment", so putting the actual load there avoids
24 // duplicating it. If the load was NULL, the loop stops.
25
26 for (mtlist = 0; (me = getmntent(fp)); mtlist = mt) {
Rob Landley078d31c2013-05-10 18:57:01 -050027 mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
28 strlen(me->mnt_dir) + strlen(me->mnt_type) + 3);
29 mt->next = mtlist;
30
31 // Collect details about mounted filesystem (don't bother for /etc/fstab).
32 stat(me->mnt_dir, &(mt->stat));
33 statvfs(me->mnt_dir, &(mt->statvfs));
34
35 // Remember information from /proc/mounts
36 mt->dir = stpcpy(mt->type, me->mnt_type) + 1;
37 mt->device = stpcpy(mt->dir, me->mnt_dir) + 1;
38 strcpy(mt->device, me->mnt_fsname);
Rob Landley7aa651a2012-11-13 17:14:08 -060039 }
Rob Landley491eb802012-11-17 22:06:00 -060040 endmntent(fp);
Rob Landley078d31c2013-05-10 18:57:01 -050041
Rob Landley7aa651a2012-11-13 17:14:08 -060042 return mtlist;
landley4f344e32006-10-05 16:18:03 -040043}