Deleted Added
sdiff udiff text old ( 90045 ) new ( 113219 )
full compact
1/*
2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/gen/fstab.c 113219 2003-04-07 12:55:00Z mdodd $");
39
40#include "namespace.h"
41#include <sys/param.h>
42#include <sys/mount.h>
43#include <sys/stat.h>
44
45#include <errno.h>
46#include <fstab.h>
47#include <paths.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52#include "un-namespace.h"
53
54static FILE *_fs_fp;
55static struct fstab _fs_fstab;
56static int LineNo = 0;
57static char *path_fstab;
58static char fstab_path[PATH_MAX];
59static int fsp_set = 0;
60
61static void error(int);
62static void fixfsfile(void);
63static int fstabscan(void);
64
65void
66setfstab(const char *file)
67{
68
69 if (file == NULL) {
70 path_fstab = _PATH_FSTAB;
71 } else {
72 strncpy(fstab_path, file, PATH_MAX);
73 fstab_path[PATH_MAX - 1] = '\0';
74 path_fstab = fstab_path;
75 }
76 fsp_set = 1;
77
78 return;
79}
80
81const char *
82getfstab (void)
83{
84
85 if (fsp_set)
86 return (path_fstab);
87 else
88 return (_PATH_FSTAB);
89}
90
91static void
92fixfsfile()
93{
94 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
95 struct stat sb;
96 struct statfs sf;
97
98 if (strcmp(_fs_fstab.fs_file, "/") != 0)
99 return;
100 if (statfs("/", &sf) != 0)
101 return;
102 if (sf.f_mntfromname[0] == '/')
103 buf[0] = '\0';
104 else
105 strcpy(buf, _PATH_DEV);
106 strcat(buf, sf.f_mntfromname);
107 if (stat(buf, &sb) != 0 ||
108 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
109 return;
110 _fs_fstab.fs_spec = buf;
111}
112
113static int
114fstabscan()
115{
116 char *cp, *p;
117#define MAXLINELENGTH 1024
118 static char line[MAXLINELENGTH];
119 char subline[MAXLINELENGTH];
120 int typexx;
121
122 for (;;) {
123
124 if (!(p = fgets(line, sizeof(line), _fs_fp)))
125 return(0);
126/* OLD_STYLE_FSTAB */
127 ++LineNo;
128 if (*line == '#' || *line == '\n')
129 continue;
130 if (!strpbrk(p, " \t")) {
131 _fs_fstab.fs_spec = strsep(&p, ":\n");
132 _fs_fstab.fs_file = strsep(&p, ":\n");
133 fixfsfile();
134 _fs_fstab.fs_type = strsep(&p, ":\n");
135 if (_fs_fstab.fs_type) {
136 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
137 continue;
138 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
139 _fs_fstab.fs_vfstype =
140 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
141 "ufs" : "swap";
142 if ((cp = strsep(&p, ":\n")) != NULL) {
143 _fs_fstab.fs_freq = atoi(cp);
144 if ((cp = strsep(&p, ":\n")) != NULL) {
145 _fs_fstab.fs_passno = atoi(cp);
146 return(1);
147 }
148 }
149 }
150 goto bad;
151 }
152/* OLD_STYLE_FSTAB */
153 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
154 ;
155 _fs_fstab.fs_spec = cp;
156 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
157 continue;
158 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
159 ;
160 _fs_fstab.fs_file = cp;
161 fixfsfile();
162 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
163 ;
164 _fs_fstab.fs_vfstype = cp;
165 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
166 ;
167 _fs_fstab.fs_mntops = cp;
168 if (_fs_fstab.fs_mntops == NULL)
169 goto bad;
170 _fs_fstab.fs_freq = 0;
171 _fs_fstab.fs_passno = 0;
172 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
173 ;
174 if (cp != NULL) {
175 _fs_fstab.fs_freq = atoi(cp);
176 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
177 ;
178 if (cp != NULL)
179 _fs_fstab.fs_passno = atoi(cp);
180 }
181 strcpy(subline, _fs_fstab.fs_mntops);
182 p = subline;
183 for (typexx = 0, cp = strsep(&p, ","); cp;
184 cp = strsep(&p, ",")) {
185 if (strlen(cp) != 2)
186 continue;
187 if (!strcmp(cp, FSTAB_RW)) {
188 _fs_fstab.fs_type = FSTAB_RW;
189 break;
190 }
191 if (!strcmp(cp, FSTAB_RQ)) {
192 _fs_fstab.fs_type = FSTAB_RQ;
193 break;
194 }
195 if (!strcmp(cp, FSTAB_RO)) {
196 _fs_fstab.fs_type = FSTAB_RO;
197 break;
198 }
199 if (!strcmp(cp, FSTAB_SW)) {
200 _fs_fstab.fs_type = FSTAB_SW;
201 break;
202 }
203 if (!strcmp(cp, FSTAB_XX)) {
204 _fs_fstab.fs_type = FSTAB_XX;
205 typexx++;
206 break;
207 }
208 }
209 if (typexx)
210 continue;
211 if (cp != NULL)
212 return(1);
213
214bad: /* no way to distinguish between EOF and syntax error */
215 error(EFTYPE);
216 }
217 /* NOTREACHED */
218}
219
220struct fstab *
221getfsent()
222{
223 if ((!_fs_fp && !setfsent()) || !fstabscan())
224 return((struct fstab *)NULL);
225 return(&_fs_fstab);
226}
227
228struct fstab *
229getfsspec(name)
230 const char *name;
231{
232 if (setfsent())
233 while (fstabscan())
234 if (!strcmp(_fs_fstab.fs_spec, name))
235 return(&_fs_fstab);
236 return((struct fstab *)NULL);
237}
238
239struct fstab *
240getfsfile(name)
241 const char *name;
242{
243 if (setfsent())
244 while (fstabscan())
245 if (!strcmp(_fs_fstab.fs_file, name))
246 return(&_fs_fstab);
247 return((struct fstab *)NULL);
248}
249
250int
251setfsent()
252{
253 if (_fs_fp) {
254 rewind(_fs_fp);
255 LineNo = 0;
256 return(1);
257 }
258 if (fsp_set == 0) {
259 if (issetugid())
260 setfstab(NULL);
261 else
262 setfstab(getenv("PATH_FSTAB"));
263 }
264 if ((_fs_fp = fopen(path_fstab, "r")) != NULL) {
265 LineNo = 0;
266 return(1);
267 }
268 error(errno);
269 return(0);
270}
271
272void
273endfsent()
274{
275 if (_fs_fp) {
276 (void)fclose(_fs_fp);
277 _fs_fp = NULL;
278 }
279
280 fsp_set = 0;
281}
282
283static void
284error(err)
285 int err;
286{
287 char *p;
288 char num[30];
289
290 (void)_write(STDERR_FILENO, "fstab: ", 7);
291 (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
292 (void)_write(STDERR_FILENO, ":", 1);
293 sprintf(num, "%d: ", LineNo);
294 (void)_write(STDERR_FILENO, num, strlen(num));
295 p = strerror(err);
296 (void)_write(STDERR_FILENO, p, strlen(p));
297 (void)_write(STDERR_FILENO, "\n", 1);
298}