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