landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4 : */ |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame^] | 2 | /* getmountlist.c - Get a linked list of mount points, with stat information. |
| 3 | * |
| 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | */ |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 6 | |
| 7 | #include "toys.h" |
| 8 | |
| 9 | #include <mntent.h> |
| 10 | |
| 11 | char *path_mounts = "/proc/mounts"; |
| 12 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame^] | 13 | // Get a list of mount points from /etc/mtab or /proc/mounts, including |
| 14 | // statvfs() information. This returns a reversed list, which is good for |
| 15 | // finding overmounts and such. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 16 | |
| 17 | struct mtab_list *getmountlist(int die) |
| 18 | { |
| 19 | FILE *fp; |
| 20 | struct mtab_list *mtlist, *mt; |
| 21 | struct mntent me; |
| 22 | char evilbuf[2*PATH_MAX]; |
| 23 | |
| 24 | mtlist = 0; |
| 25 | if (!(fp = setmntent(path_mounts, "r"))) { |
| 26 | if (die) error_exit("cannot open %s", path_mounts); |
| 27 | } else { |
| 28 | while (getmntent_r(fp, &me, evilbuf, sizeof(evilbuf))) { |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame^] | 29 | mt = xzalloc(sizeof(struct mtab_list) + strlen(me.mnt_fsname) + |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 30 | strlen(me.mnt_dir) + strlen(me.mnt_type) + 3); |
| 31 | mt->next = mtlist; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame^] | 32 | // Get information about this filesystem. Yes, we need both. |
| 33 | stat(me.mnt_dir, &(mt->stat)); |
| 34 | statvfs(me.mnt_dir, &(mt->statvfs)); |
| 35 | // Remember information from /proc/mounts |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 36 | strcpy(mt->type, me.mnt_type); |
| 37 | mt->dir = mt->type + strlen(mt->type) + 1; |
| 38 | strcpy(mt->dir, me.mnt_dir); |
| 39 | mt->device = mt->dir + strlen(mt->dir) + 1; |
landley | 8ce06f2 | 2006-10-26 12:04:17 -0400 | [diff] [blame] | 40 | strcpy(mt->device, me.mnt_fsname); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 41 | mtlist = mt; |
| 42 | } |
| 43 | } |
| 44 | return mtlist; |
| 45 | } |