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 | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame^] | 13 | struct mtab_list *xgetmountlist(void) |
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 | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame^] | 19 | 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 Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 34 | } |
Rob Landley | 491eb80 | 2012-11-17 22:06:00 -0600 | [diff] [blame] | 35 | endmntent(fp); |
Rob Landley | 078d31c | 2013-05-10 18:57:01 -0500 | [diff] [blame^] | 36 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 37 | return mtlist; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 38 | } |