10 extern const char mtab_file[]; /* Defined in utility.c */
13 stralloc(const char * string)
15 int length = strlen(string) + 1;
16 char * n = malloc(length);
17 memcpy(n, string, length);
22 erase_mtab(const char * name)
24 struct mntent entries[20];
26 FILE *mountTable = setmntent(mtab_file, "r");
30 && (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
35 while ( (m = getmntent(mountTable)) != 0 ) {
36 entries[count].mnt_fsname = stralloc(m->mnt_fsname);
37 entries[count].mnt_dir = stralloc(m->mnt_dir);
38 entries[count].mnt_type = stralloc(m->mnt_type);
39 entries[count].mnt_opts = stralloc(m->mnt_opts);
40 entries[count].mnt_freq = m->mnt_freq;
41 entries[count].mnt_passno = m->mnt_passno;
44 endmntent(mountTable);
45 if ( (mountTable = setmntent(mtab_file, "w")) ) {
47 for ( i = 0; i < count; i++ ) {
48 int result = ( strcmp(entries[i].mnt_fsname, name) == 0
49 || strcmp(entries[i].mnt_dir, name) == 0 );
54 addmntent(mountTable, &entries[i]);
56 endmntent(mountTable);
58 else if ( errno != EROFS )
63 * Given a block device, find the mount table entry if that block device
66 * Given any other file (or directory), find the mount table entry for its
69 static struct mntent *
70 findMountPoint(const char * name, const char * table)
75 struct mntent* mountEntry;
77 if ( stat(name, &s) != 0 )
80 if ( (s.st_mode & S_IFMT) == S_IFBLK )
81 mountDevice = s.st_rdev;
83 mountDevice = s.st_dev;
86 if ( (mountTable = setmntent(table, "r")) == 0 )
89 while ( (mountEntry = getmntent(mountTable)) != 0 ) {
90 if ( strcmp(name, mountEntry->mnt_dir) == 0 ||
91 strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */
93 if ( stat(mountEntry->mnt_fsname, &s) == 0 &&
94 s.st_rdev == mountDevice ) /* Match the device. */
96 if ( stat(mountEntry->mnt_dir, &s) == 0 &&
97 s.st_dev == mountDevice ) /* Match the directory's mount point. */
100 endmntent(mountTable);
105 write_mtab(char* blockDevice, char* directory,
106 char* filesystemType, long flags, char* string_flags)
108 FILE *mountTable = setmntent(mtab_file, "a+");
111 if ( mountTable == 0 ) {
116 int length = strlen(directory);
118 if ( length > 1 && directory[length - 1] == '/' )
119 directory[length - 1] = '\0';
121 if ( filesystemType == 0 ) {
123 = findMountPoint(blockDevice, "/proc/mounts");
125 if ( p && p->mnt_type )
126 filesystemType = p->mnt_type;
128 m.mnt_fsname = blockDevice;
129 m.mnt_dir = directory;
130 m.mnt_type = filesystemType ? filesystemType : "default";
133 m.mnt_opts = string_flags;
135 if ( (flags | MS_RDONLY) == flags )
143 addmntent(mountTable, &m);
144 endmntent(mountTable);