Initial public busybox upstream commit
[busybox4maemo] / libbb / mtab.c
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
14 void erase_mtab(const char *name)
15 {
16         struct mntent *entries = NULL;
17         int i, count = 0;
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         while ((m = getmntent(mountTable)) != 0) {
30                 i = count++;
31                 entries = xrealloc(entries, count * sizeof(entries[0]));
32                 entries[i].mnt_fsname = xstrdup(m->mnt_fsname);
33                 entries[i].mnt_dir = xstrdup(m->mnt_dir);
34                 entries[i].mnt_type = xstrdup(m->mnt_type);
35                 entries[i].mnt_opts = xstrdup(m->mnt_opts);
36                 entries[i].mnt_freq = m->mnt_freq;
37                 entries[i].mnt_passno = m->mnt_passno;
38         }
39         endmntent(mountTable);
40
41         mountTable = setmntent(bb_path_mtab_file, "w");
42         if (mountTable) {
43                 for (i = 0; i < count; i++) {
44                         if (strcmp(entries[i].mnt_fsname, name) != 0
45                          && strcmp(entries[i].mnt_dir, name) != 0)
46                                 addmntent(mountTable, &entries[i]);
47                 }
48                 endmntent(mountTable);
49         } else if (errno != EROFS)
50                 bb_perror_msg(bb_path_mtab_file);
51 }
52 #endif