2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static 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$");
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/mount.h>
52 #include "un-namespace.h"
55 static struct fstab _fs_fstab;
56 static int LineNo = 0;
57 static char *path_fstab;
58 static char fstab_path[PATH_MAX];
59 static int fsp_set = 0;
61 static void error(int);
62 static void fixfsfile(void);
63 static int fstabscan(void);
66 setfstab(const char *file)
70 path_fstab = _PATH_FSTAB;
72 strncpy(fstab_path, file, PATH_MAX);
73 fstab_path[PATH_MAX - 1] = '\0';
74 path_fstab = fstab_path;
94 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
98 if (strcmp(_fs_fstab.fs_file, "/") != 0)
100 if (statfs("/", &sf) != 0)
102 if (sf.f_mntfromname[0] == '/')
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)))
110 _fs_fstab.fs_spec = buf;
117 #define MAXLINELENGTH 1024
118 static char line[MAXLINELENGTH];
119 char subline[MAXLINELENGTH];
124 if (!(p = fgets(line, sizeof(line), _fs_fp)))
126 /* OLD_STYLE_FSTAB */
128 if (*line == '#' || *line == '\n')
130 if (!strpbrk(p, " \t")) {
131 _fs_fstab.fs_spec = strsep(&p, ":\n");
132 _fs_fstab.fs_file = strsep(&p, ":\n");
134 _fs_fstab.fs_type = strsep(&p, ":\n");
135 if (_fs_fstab.fs_type) {
136 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
138 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
139 _fs_fstab.fs_vfstype =
140 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
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);
152 /* OLD_STYLE_FSTAB */
153 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
155 _fs_fstab.fs_spec = cp;
156 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
158 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
160 _fs_fstab.fs_file = cp;
162 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
164 _fs_fstab.fs_vfstype = cp;
165 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
167 _fs_fstab.fs_mntops = cp;
168 if (_fs_fstab.fs_mntops == NULL)
170 _fs_fstab.fs_freq = 0;
171 _fs_fstab.fs_passno = 0;
172 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
175 _fs_fstab.fs_freq = atoi(cp);
176 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
179 _fs_fstab.fs_passno = atoi(cp);
181 strcpy(subline, _fs_fstab.fs_mntops);
183 for (typexx = 0, cp = strsep(&p, ","); cp;
184 cp = strsep(&p, ",")) {
187 if (!strcmp(cp, FSTAB_RW)) {
188 _fs_fstab.fs_type = FSTAB_RW;
191 if (!strcmp(cp, FSTAB_RQ)) {
192 _fs_fstab.fs_type = FSTAB_RQ;
195 if (!strcmp(cp, FSTAB_RO)) {
196 _fs_fstab.fs_type = FSTAB_RO;
199 if (!strcmp(cp, FSTAB_SW)) {
200 _fs_fstab.fs_type = FSTAB_SW;
203 if (!strcmp(cp, FSTAB_XX)) {
204 _fs_fstab.fs_type = FSTAB_XX;
214 bad: /* no way to distinguish between EOF and syntax error */
223 if ((!_fs_fp && !setfsent()) || !fstabscan())
224 return((struct fstab *)NULL);
234 if (!strcmp(_fs_fstab.fs_spec, name))
236 return((struct fstab *)NULL);
245 if (!strcmp(_fs_fstab.fs_file, name))
247 return((struct fstab *)NULL);
262 setfstab(getenv("PATH_FSTAB"));
264 if ((_fs_fp = fopen(path_fstab, "r")) != NULL) {
276 (void)fclose(_fs_fp);
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));
296 (void)_write(STDERR_FILENO, p, strlen(p));
297 (void)_write(STDERR_FILENO, "\n", 1);