blob: 8410a923c273e7f5f7926e472c9405e72ff945f4 [file] [log] [blame]
landleycd9dfc32006-10-18 18:38:16 -04001/* vi: set sw=4 ts=4 : */
landley09ea7ac2006-10-30 01:38:00 -05002/* getmountlist.c - Get a linked list of mount points, with stat information.
3 *
4 * Copyright 2006 Rob Landley <rob@landley.net>
5 */
landley4f344e32006-10-05 16:18:03 -04006
7#include "toys.h"
8
9#include <mntent.h>
10
11char *path_mounts = "/proc/mounts";
12
landley09ea7ac2006-10-30 01:38:00 -050013// 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.
landley4f344e32006-10-05 16:18:03 -040016
17struct 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))) {
landley09ea7ac2006-10-30 01:38:00 -050029 mt = xzalloc(sizeof(struct mtab_list) + strlen(me.mnt_fsname) +
landley4f344e32006-10-05 16:18:03 -040030 strlen(me.mnt_dir) + strlen(me.mnt_type) + 3);
31 mt->next = mtlist;
landley09ea7ac2006-10-30 01:38:00 -050032 // 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
landley4f344e32006-10-05 16:18:03 -040036 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;
landley8ce06f22006-10-26 12:04:17 -040040 strcpy(mt->device, me.mnt_fsname);
landley4f344e32006-10-05 16:18:03 -040041 mtlist = mt;
42 }
43 }
44 return mtlist;
45}