Fixed ln, df, and removed redundant stuff from mtab.
[oweals/busybox.git] / mtab.c
1 #include "internal.h"
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
10 extern const char mtab_file[]; /* Defined in utility.c */
11
12 static char *
13 stralloc(const char * string)
14 {
15         int     length = strlen(string) + 1;
16         char *  n = malloc(length);
17         memcpy(n, string, length);
18         return n;
19 }
20
21 extern void
22 erase_mtab(const char * name)
23 {
24         struct mntent   entries[20];
25         int     count = 0;
26         FILE *mountTable = setmntent(mtab_file, "r");
27         struct mntent * m;
28
29         if ( mountTable == 0
30          && (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
31                 perror(mtab_file);
32                 return;
33         }
34
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;
42                 count++;
43         }
44         endmntent(mountTable);
45         if ( (mountTable = setmntent(mtab_file, "w")) ) {
46                 int     i;
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 );
50
51                         if ( result )
52                                 continue;
53                         else
54                                 addmntent(mountTable, &entries[i]);
55                 }
56                 endmntent(mountTable);
57         }
58         else if ( errno != EROFS )
59                 perror(mtab_file);
60 }
61
62 extern void 
63 write_mtab(char* blockDevice, char* directory, 
64         char* filesystemType, long flags, char* string_flags)
65 {
66         FILE *mountTable = setmntent(mtab_file, "a+");
67         struct mntent m;
68
69         if ( mountTable == 0 ) {
70                 perror(mtab_file);
71                 return;
72         }
73         if (mountTable) {
74             int length = strlen(directory);
75
76             if ( length > 1 && directory[length - 1] == '/' )
77                     directory[length - 1] = '\0';
78
79             if ( filesystemType == 0 ) {
80                     struct mntent *     p
81                      = findMountPoint(blockDevice, "/proc/mounts");
82
83                     if ( p && p->mnt_type )
84                             filesystemType = p->mnt_type;
85             }
86             m.mnt_fsname = blockDevice;
87             m.mnt_dir = directory;
88             m.mnt_type = filesystemType ? filesystemType : "default";
89             
90             if (*string_flags) {
91                     m.mnt_opts = string_flags;
92             } else {
93                     if ( (flags | MS_RDONLY) == flags )
94                             m.mnt_opts = "ro";
95                     else        
96                             m.mnt_opts = "rw";
97             }
98             
99             m.mnt_freq = 0;
100             m.mnt_passno = 0;
101             addmntent(mountTable, &m);
102             endmntent(mountTable);
103     }
104 }
105
106