diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-04-01 16:01:11 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-04-01 16:01:11 +0000 |
commit | c4cef5ab063d7abb604d48610424566202dcade0 (patch) | |
tree | c661196acbb1adfa81257a7f23b08bd15d653be8 /libbb | |
parent | f77d88641d934fbd9ed0da8c8e31c84dc5856532 (diff) | |
download | busybox-w32-c4cef5ab063d7abb604d48610424566202dcade0.tar.gz busybox-w32-c4cef5ab063d7abb604d48610424566202dcade0.tar.bz2 busybox-w32-c4cef5ab063d7abb604d48610424566202dcade0.zip |
Move the mtab support stuff into libbb
-Erik
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/mtab.c | 95 | ||||
-rw-r--r-- | libbb/mtab_file.c | 52 |
2 files changed, 147 insertions, 0 deletions
diff --git a/libbb/mtab.c b/libbb/mtab.c new file mode 100644 index 000000000..28c9978ef --- /dev/null +++ b/libbb/mtab.c | |||
@@ -0,0 +1,95 @@ | |||
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 "libbb.h" | ||
9 | |||
10 | extern const char mtab_file[]; /* Defined in utility.c */ | ||
11 | static const int MS_RDONLY = 1; /* Mount read-only. */ | ||
12 | |||
13 | void erase_mtab(const char *name) | ||
14 | { | ||
15 | struct mntent entries[20]; | ||
16 | int count = 0; | ||
17 | FILE *mountTable = setmntent(mtab_file, "r"); | ||
18 | struct mntent *m; | ||
19 | |||
20 | /* Check if reading the mtab file failed */ | ||
21 | if (mountTable == 0 | ||
22 | /* Bummer. fall back on trying the /proc filesystem */ | ||
23 | && (mountTable = setmntent("/proc/mounts", "r")) == 0) { | ||
24 | perror_msg("%s", mtab_file); | ||
25 | return; | ||
26 | } | ||
27 | |||
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; | ||
35 | count++; | ||
36 | } | ||
37 | endmntent(mountTable); | ||
38 | if ((mountTable = setmntent(mtab_file, "w"))) { | ||
39 | int i; | ||
40 | |||
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); | ||
44 | |||
45 | if (result) | ||
46 | continue; | ||
47 | else | ||
48 | addmntent(mountTable, &entries[i]); | ||
49 | } | ||
50 | endmntent(mountTable); | ||
51 | } else if (errno != EROFS) | ||
52 | perror_msg("%s", mtab_file); | ||
53 | } | ||
54 | |||
55 | void write_mtab(char *blockDevice, char *directory, | ||
56 | char *filesystemType, long flags, char *string_flags) | ||
57 | { | ||
58 | FILE *mountTable = setmntent(mtab_file, "a+"); | ||
59 | struct mntent m; | ||
60 | |||
61 | if (mountTable == 0) { | ||
62 | perror_msg("%s", mtab_file); | ||
63 | return; | ||
64 | } | ||
65 | if (mountTable) { | ||
66 | int length = strlen(directory); | ||
67 | |||
68 | if (length > 1 && directory[length - 1] == '/') | ||
69 | directory[length - 1] = '\0'; | ||
70 | |||
71 | if (filesystemType == 0) { | ||
72 | struct mntent *p = find_mount_point(blockDevice, "/proc/mounts"); | ||
73 | |||
74 | if (p && p->mnt_type) | ||
75 | filesystemType = p->mnt_type; | ||
76 | } | ||
77 | m.mnt_fsname = blockDevice; | ||
78 | m.mnt_dir = directory; | ||
79 | m.mnt_type = filesystemType ? filesystemType : "default"; | ||
80 | |||
81 | if (*string_flags) { | ||
82 | m.mnt_opts = string_flags; | ||
83 | } else { | ||
84 | if ((flags | MS_RDONLY) == flags) | ||
85 | m.mnt_opts = "ro"; | ||
86 | else | ||
87 | m.mnt_opts = "rw"; | ||
88 | } | ||
89 | |||
90 | m.mnt_freq = 0; | ||
91 | m.mnt_passno = 0; | ||
92 | addmntent(mountTable, &m); | ||
93 | endmntent(mountTable); | ||
94 | } | ||
95 | } | ||
diff --git a/libbb/mtab_file.c b/libbb/mtab_file.c new file mode 100644 index 000000000..d9c3de3c3 --- /dev/null +++ b/libbb/mtab_file.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | /* Busybox mount uses either /proc/mounts or /dev/mtab to | ||
33 | * get the list of currently mounted filesystems */ | ||
34 | #if defined BB_MTAB | ||
35 | const char mtab_file[] = "/etc/mtab"; | ||
36 | #else | ||
37 | # if defined BB_FEATURE_USE_DEVPS_PATCH | ||
38 | const char mtab_file[] = "/dev/mtab"; | ||
39 | # else | ||
40 | const char mtab_file[] = "/proc/mounts"; | ||
41 | # endif | ||
42 | #endif | ||
43 | |||
44 | |||
45 | /* END CODE */ | ||
46 | /* | ||
47 | Local Variables: | ||
48 | c-file-style: "linux" | ||
49 | c-basic-offset: 4 | ||
50 | tab-width: 4 | ||
51 | End: | ||
52 | */ | ||