1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include <mntent.h>
11#include "libbb.h"
12
13#if ENABLE_FEATURE_MTAB_SUPPORT
14void FAST_FUNC erase_mtab(const char *name)
15{
16	struct mntent *entries;
17	int i, count;
18	FILE *mountTable;
19	struct mntent *m;
20
21	mountTable = setmntent(bb_path_mtab_file, "r");
22	/* Bummer. Fall back on trying the /proc filesystem */
23	if (!mountTable) mountTable = setmntent("/proc/mounts", "r");
24	if (!mountTable) {
25		bb_perror_msg(bb_path_mtab_file);
26		return;
27	}
28
29	entries = NULL;
30	count = 0;
31	while ((m = getmntent(mountTable)) != 0) {
32		entries = xrealloc_vector(entries, 3, count);
33		entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
34		entries[count].mnt_dir = xstrdup(m->mnt_dir);
35		entries[count].mnt_type = xstrdup(m->mnt_type);
36		entries[count].mnt_opts = xstrdup(m->mnt_opts);
37		entries[count].mnt_freq = m->mnt_freq;
38		entries[count].mnt_passno = m->mnt_passno;
39		count++;
40	}
41	endmntent(mountTable);
42
43//TODO: make update atomic
44	mountTable = setmntent(bb_path_mtab_file, "w");
45	if (mountTable) {
46		for (i = 0; i < count; i++) {
47			if (strcmp(entries[i].mnt_fsname, name) != 0
48			 && strcmp(entries[i].mnt_dir, name) != 0)
49				addmntent(mountTable, &entries[i]);
50		}
51		endmntent(mountTable);
52	} else if (errno != EROFS)
53		bb_perror_msg(bb_path_mtab_file);
54}
55#endif
56