1 /* vi: set sw=4 ts=4: */
10 extern const char mtab_file[]; /* Defined in utility.c */
11 static const int MS_RDONLY = 1; /* Mount read-only. */
13 void erase_mtab(const char *name)
15 struct mntent entries[20];
17 FILE *mountTable = setmntent(mtab_file, "r");
20 /* Check if reading the mtab file failed */
22 /* Bummer. fall back on trying the /proc filesystem */
23 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
24 perror_msg("%s", mtab_file);
28 while ((m = getmntent(mountTable)) != 0) {
29 entries[count].mnt_fsname = strdup(m->mnt_fsname);
30 entries[count].mnt_dir = strdup(m->mnt_dir);
31 entries[count].mnt_type = strdup(m->mnt_type);
32 entries[count].mnt_opts = strdup(m->mnt_opts);
33 entries[count].mnt_freq = m->mnt_freq;
34 entries[count].mnt_passno = m->mnt_passno;
37 endmntent(mountTable);
38 if ((mountTable = setmntent(mtab_file, "w"))) {
41 for (i = 0; i < count; i++) {
42 int result = (strcmp(entries[i].mnt_fsname, name) == 0
43 || strcmp(entries[i].mnt_dir, name) == 0);
48 addmntent(mountTable, &entries[i]);
50 endmntent(mountTable);
51 } else if (errno != EROFS)
52 perror_msg("%s", mtab_file);
55 void write_mtab(char *blockDevice, char *directory,
56 char *filesystemType, long flags, char *string_flags)
58 FILE *mountTable = setmntent(mtab_file, "a+");
61 if (mountTable == 0) {
62 perror_msg("%s", mtab_file);
66 int length = strlen(directory);
68 if (length > 1 && directory[length - 1] == '/')
69 directory[length - 1] = '\0';
71 if (filesystemType == 0) {
72 struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
75 filesystemType = p->mnt_type;
77 m.mnt_fsname = blockDevice;
78 m.mnt_dir = directory;
79 m.mnt_type = filesystemType ? filesystemType : "default";
82 m.mnt_opts = string_flags;
84 if ((flags | MS_RDONLY) == flags)
92 addmntent(mountTable, &m);
93 endmntent(mountTable);