getmountlist.c revision 4f344e356d2c36c4b1df46917eaef25f82ca79a9
1ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/* vi: set ts=4 : */
2ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
3ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann#include "toys.h"
4ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
5ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann#include <mntent.h>
6ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
7ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmannchar *path_mounts = "/proc/mounts";
8ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
9ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann// Get a list of mount points from /etc/mtab or /proc/mounts.  This returns
10ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann// a reversed list, which is good for finding overmounts and such.
11ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
12ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmannstruct mtab_list *getmountlist(int die)
13ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann{
14ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	FILE *fp;
15ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	struct mtab_list *mtlist, *mt;
16ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	struct mntent me;
17ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	char evilbuf[2*PATH_MAX];
18ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
19ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	mtlist = 0;
20ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	if (!(fp = setmntent(path_mounts, "r"))) {
21ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann		if (die) error_exit("cannot open %s", path_mounts);
22ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	} else {
23ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann		while (getmntent_r(fp, &me, evilbuf, sizeof(evilbuf))) {
24ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann			char *str;
25ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
26ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann			mt = xmalloc(sizeof(struct mtab_list) + strlen(me.mnt_fsname) +
27ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann				strlen(me.mnt_dir) + strlen(me.mnt_type) + 3);
28			mt->next = mtlist;
29			strcpy(mt->type, me.mnt_type);
30			mt->dir = mt->type + strlen(mt->type) + 1;
31			strcpy(mt->dir, me.mnt_dir);
32			mt->device = mt->dir + strlen(mt->dir) + 1;
33			mt->device = ++str;
34			strcpy(str, me.mnt_fsname);
35			mtlist = mt;
36		}
37	}
38	return mtlist;
39}
40