landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 1 | /* getmountlist.c - Get a linked list of mount points, with stat information. |
| 2 | * |
| 3 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 4 | */ |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 5 | |
| 6 | #include "toys.h" |
| 7 | |
| 8 | #include <mntent.h> |
| 9 | |
Rob Landley | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame] | 10 | // Get list of mounted filesystems, including stat and statvfs info. |
| 11 | // Returns a reversed list, which is good for finding overmounts and such. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 12 | |
Rob Landley | 42adb7a | 2013-08-30 17:34:24 -0500 | [diff] [blame] | 13 | struct mtab_list *xgetmountlist(char *path) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 14 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 15 | struct mtab_list *mtlist, *mt; |
Rob Landley | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame] | 16 | struct mntent *me; |
| 17 | FILE *fp; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 18 | |
Rob Landley | 42adb7a | 2013-08-30 17:34:24 -0500 | [diff] [blame] | 19 | if (!path) path = "/proc/mounts"; |
| 20 | if (!(fp = setmntent(path, "r"))) perror_exit("bad %s", path); |
Rob Landley | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame] | 21 | |
Rob Landley | 00474ef | 2013-05-14 20:22:23 -0500 | [diff] [blame] | 22 | // 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 Landley | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame] | 27 | 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 Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 39 | } |
Rob Landley | 491eb80 | 2012-11-17 22:06:00 -0600 | [diff] [blame] | 40 | endmntent(fp); |
Rob Landley | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame] | 41 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 42 | return mtlist; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 43 | } |