]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
4 | * Remy Card <card@Linux.EU.Org>, 1994, 1995, 1996 | |
5 | * | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Copyright (c) 1980, 1988, 1993 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. All advertising materials mentioning features or use of this software | |
21 | * must display the following acknowledgement: | |
22 | * This product includes software developed by the University of | |
23 | * California, Berkeley and its contributors. | |
24 | * 4. Neither the name of the University nor the names of its contributors | |
25 | * may be used to endorse or promote products derived from this software | |
26 | * without specific prior written permission. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
38 | * SUCH DAMAGE. | |
39 | */ | |
40 | ||
41 | #if defined(LIBC_SCCS) && !defined(lint) | |
42 | static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; | |
43 | #endif /* LIBC_SCCS and not lint */ | |
44 | ||
45 | #include <errno.h> | |
46 | #include <fstab.h> | |
47 | #include <mntent.h> | |
48 | #include <stdio.h> | |
49 | #include <stdlib.h> | |
50 | #include <string.h> | |
51 | #include <unistd.h> | |
52 | ||
53 | static FILE *_fs_fp; | |
54 | static struct fstab _fs_fstab; | |
55 | ||
56 | static void error __P((int)); | |
57 | static int fstabscan __P((void)); | |
58 | ||
59 | static | |
60 | int fstabscan() | |
61 | { | |
62 | struct mntent *mnt; | |
63 | register char *cp; | |
64 | int typexx; | |
65 | #define MAXLINELENGTH 1024 | |
66 | char subline[MAXLINELENGTH]; | |
67 | ||
68 | for (;;) { | |
69 | if (!(mnt = getmntent(_fs_fp))) | |
70 | return 0; | |
71 | ||
72 | _fs_fstab.fs_spec = mnt->mnt_fsname; | |
73 | _fs_fstab.fs_file = mnt->mnt_dir; | |
74 | _fs_fstab.fs_vfstype = mnt->mnt_type; | |
75 | _fs_fstab.fs_mntops = mnt->mnt_opts; | |
76 | _fs_fstab.fs_type = FSTAB_RW; /* rw by default under Linux */ | |
77 | _fs_fstab.fs_freq = mnt->mnt_freq; | |
78 | _fs_fstab.fs_passno = mnt->mnt_passno; | |
79 | ||
80 | strcpy(subline, _fs_fstab.fs_mntops); | |
81 | for (typexx = 0, cp = strtok(subline, ","); cp; | |
82 | cp = strtok((char *)NULL, ",")) { | |
83 | if (!strcmp(cp, FSTAB_RW)) { | |
84 | _fs_fstab.fs_type = FSTAB_RW; | |
85 | break; | |
86 | } | |
87 | if (!strcmp(cp, FSTAB_RQ)) { | |
88 | _fs_fstab.fs_type = FSTAB_RQ; | |
89 | break; | |
90 | } | |
91 | if (!strcmp(cp, FSTAB_RO)) { | |
92 | _fs_fstab.fs_type = FSTAB_RO; | |
93 | break; | |
94 | } | |
95 | if (!strcmp(cp, FSTAB_SW)) { | |
96 | _fs_fstab.fs_type = FSTAB_SW; | |
97 | break; | |
98 | } | |
99 | if (!strcmp(cp, FSTAB_XX)) { | |
100 | _fs_fstab.fs_type = FSTAB_XX; | |
101 | typexx++; | |
102 | break; | |
103 | } | |
104 | } | |
105 | if (typexx) | |
106 | continue; | |
107 | ||
108 | return 1; | |
109 | } | |
110 | } | |
111 | ||
112 | struct fstab * | |
113 | getfsent() | |
114 | { | |
115 | if (!_fs_fp && !setfsent() || !fstabscan()) | |
116 | return((struct fstab *)NULL); | |
117 | return(&_fs_fstab); | |
118 | } | |
119 | ||
120 | struct fstab * | |
121 | getfsspec(name) | |
122 | register const char *name; | |
123 | { | |
124 | if (setfsent()) | |
125 | while (fstabscan()) | |
126 | if (!strcmp(_fs_fstab.fs_spec, name)) | |
127 | return(&_fs_fstab); | |
128 | return((struct fstab *)NULL); | |
129 | } | |
130 | ||
131 | struct fstab * | |
132 | getfsfile(name) | |
133 | register const char *name; | |
134 | { | |
135 | if (setfsent()) | |
136 | while (fstabscan()) | |
137 | if (!strcmp(_fs_fstab.fs_file, name)) | |
138 | return(&_fs_fstab); | |
139 | return((struct fstab *)NULL); | |
140 | } | |
141 | ||
142 | int setfsent() | |
143 | { | |
144 | if (_fs_fp) { | |
145 | rewind(_fs_fp); | |
146 | return(1); | |
147 | } | |
148 | if ((_fs_fp = setmntent(_PATH_FSTAB, "r"))) | |
149 | return(1); | |
150 | error(errno); | |
151 | return(0); | |
152 | } | |
153 | ||
154 | void | |
155 | endfsent() | |
156 | { | |
157 | if (_fs_fp) { | |
158 | (void)endmntent(_fs_fp); | |
159 | _fs_fp = NULL; | |
160 | } | |
161 | } | |
162 | ||
163 | static | |
164 | void error(err) | |
165 | int err; | |
166 | { | |
167 | char *p; | |
168 | ||
169 | (void)write(STDERR_FILENO, "fstab: ", 7); | |
170 | (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); | |
171 | (void)write(STDERR_FILENO, ": ", 1); | |
172 | p = strerror(err); | |
173 | (void)write(STDERR_FILENO, p, strlen(p)); | |
174 | (void)write(STDERR_FILENO, "\n", 1); | |
175 | } |