It turns out that DODMALLOC was broken when I reorganized busybox.h
[oweals/busybox.git] / mtab.c
1 /* vi: set sw=4 ts=4: */
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <mntent.h>
8 #include <fstab.h>
9 #include <sys/mount.h>
10 #include "busybox.h"
11
12 extern const char mtab_file[];  /* Defined in utility.c */
13
14
15 void erase_mtab(const char *name)
16 {
17         struct mntent entries[20];
18         int count = 0;
19         FILE *mountTable = setmntent(mtab_file, "r");
20         struct mntent *m;
21
22         /* Check if reading the mtab file failed */
23         if (mountTable == 0
24                         /* Bummer.  fall back on trying the /proc filesystem */
25                         && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
26                 perror_msg("%s", mtab_file);
27                 return;
28         }
29
30         while ((m = getmntent(mountTable)) != 0) {
31                 entries[count].mnt_fsname = strdup(m->mnt_fsname);
32                 entries[count].mnt_dir = strdup(m->mnt_dir);
33                 entries[count].mnt_type = strdup(m->mnt_type);
34                 entries[count].mnt_opts = strdup(m->mnt_opts);
35                 entries[count].mnt_freq = m->mnt_freq;
36                 entries[count].mnt_passno = m->mnt_passno;
37                 count++;
38         }
39         endmntent(mountTable);
40         if ((mountTable = setmntent(mtab_file, "w"))) {
41                 int i;
42
43                 for (i = 0; i < count; i++) {
44                         int result = (strcmp(entries[i].mnt_fsname, name) == 0
45                                                   || strcmp(entries[i].mnt_dir, name) == 0);
46
47                         if (result)
48                                 continue;
49                         else
50                                 addmntent(mountTable, &entries[i]);
51                 }
52                 endmntent(mountTable);
53         } else if (errno != EROFS)
54                 perror_msg("%s", mtab_file);
55 }
56
57 void write_mtab(char *blockDevice, char *directory,
58                                 char *filesystemType, long flags, char *string_flags)
59 {
60         FILE *mountTable = setmntent(mtab_file, "a+");
61         struct mntent m;
62
63         if (mountTable == 0) {
64                 perror_msg("%s", mtab_file);
65                 return;
66         }
67         if (mountTable) {
68                 int length = strlen(directory);
69
70                 if (length > 1 && directory[length - 1] == '/')
71                         directory[length - 1] = '\0';
72
73                 if (filesystemType == 0) {
74                         struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
75
76                         if (p && p->mnt_type)
77                                 filesystemType = p->mnt_type;
78                 }
79                 m.mnt_fsname = blockDevice;
80                 m.mnt_dir = directory;
81                 m.mnt_type = filesystemType ? filesystemType : "default";
82
83                 if (*string_flags) {
84                         m.mnt_opts = string_flags;
85                 } else {
86                         if ((flags | MS_RDONLY) == flags)
87                                 m.mnt_opts = "ro";
88                         else
89                                 m.mnt_opts = "rw";
90                 }
91
92                 m.mnt_freq = 0;
93                 m.mnt_passno = 0;
94                 addmntent(mountTable, &m);
95                 endmntent(mountTable);
96         }
97 }