2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)fstab.c 8.1 (Berkeley) 6/4/93");
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mount.h>
49 #include "un-namespace.h"
52 static struct fstab _fs_fstab;
53 static int LineNo = 0;
54 static char *path_fstab;
55 static char fstab_path[PATH_MAX];
56 static int fsp_set = 0;
58 static void error(int);
59 static void fixfsfile(void);
60 static int fstabscan(void);
63 setfstab(const char *file)
67 path_fstab = _PATH_FSTAB;
69 strncpy(fstab_path, file, PATH_MAX);
70 fstab_path[PATH_MAX - 1] = '\0';
71 path_fstab = fstab_path;
91 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
95 if (_fs_fstab.fs_file != NULL && strcmp(_fs_fstab.fs_file, "/") != 0)
97 if (statfs("/", &sf) != 0)
99 if (sf.f_mntfromname[0] == '/')
102 strcpy(buf, _PATH_DEV);
103 strcat(buf, sf.f_mntfromname);
104 if (stat(buf, &sb) != 0 ||
105 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
107 _fs_fstab.fs_spec = buf;
114 #define MAXLINELENGTH 1024
115 static char line[MAXLINELENGTH];
116 char subline[MAXLINELENGTH];
121 if (!(p = fgets(line, sizeof(line), _fs_fp)))
123 /* OLD_STYLE_FSTAB */
125 if (*line == '#' || *line == '\n')
127 if (!strpbrk(p, " \t")) {
128 _fs_fstab.fs_spec = strsep(&p, ":\n");
129 _fs_fstab.fs_file = strsep(&p, ":\n");
131 _fs_fstab.fs_type = strsep(&p, ":\n");
132 if (_fs_fstab.fs_type) {
133 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
135 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
136 _fs_fstab.fs_vfstype =
137 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
139 if ((cp = strsep(&p, ":\n")) != NULL) {
140 _fs_fstab.fs_freq = atoi(cp);
141 if ((cp = strsep(&p, ":\n")) != NULL) {
142 _fs_fstab.fs_passno = atoi(cp);
149 /* OLD_STYLE_FSTAB */
150 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
152 _fs_fstab.fs_spec = cp;
153 if (_fs_fstab.fs_spec == NULL || *_fs_fstab.fs_spec == '#')
155 if (strunvis(_fs_fstab.fs_spec, _fs_fstab.fs_spec) < 0)
157 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
159 _fs_fstab.fs_file = cp;
160 if (_fs_fstab.fs_file == NULL)
162 if (strunvis(_fs_fstab.fs_file, _fs_fstab.fs_file) < 0)
165 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
167 _fs_fstab.fs_vfstype = cp;
168 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
170 _fs_fstab.fs_mntops = cp;
171 if (_fs_fstab.fs_mntops == NULL)
173 _fs_fstab.fs_freq = 0;
174 _fs_fstab.fs_passno = 0;
175 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
178 _fs_fstab.fs_freq = atoi(cp);
179 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
182 _fs_fstab.fs_passno = atoi(cp);
184 (void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline));
186 for (typexx = 0, cp = strsep(&p, ","); cp;
187 cp = strsep(&p, ",")) {
190 if (!strcmp(cp, FSTAB_RW)) {
191 _fs_fstab.fs_type = FSTAB_RW;
194 if (!strcmp(cp, FSTAB_RQ)) {
195 _fs_fstab.fs_type = FSTAB_RQ;
198 if (!strcmp(cp, FSTAB_RO)) {
199 _fs_fstab.fs_type = FSTAB_RO;
202 if (!strcmp(cp, FSTAB_SW)) {
203 _fs_fstab.fs_type = FSTAB_SW;
206 if (!strcmp(cp, FSTAB_XX)) {
207 _fs_fstab.fs_type = FSTAB_XX;
217 bad: /* no way to distinguish between EOF and syntax error */
227 if ((!_fs_fp && !setfsent()) || !fstabscan())
233 getfsspec(const char *name)
238 if (!strcmp(_fs_fstab.fs_spec, name))
244 getfsfile(const char *name)
249 if (!strcmp(_fs_fstab.fs_file, name))
266 setfstab(getenv("PATH_FSTAB"));
268 if ((_fs_fp = fopen(path_fstab, "re")) != NULL) {
281 (void)fclose(_fs_fp);
294 (void)_write(STDERR_FILENO, "fstab: ", 7);
295 (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
296 (void)_write(STDERR_FILENO, ":", 1);
297 sprintf(num, "%d: ", LineNo);
298 (void)_write(STDERR_FILENO, num, strlen(num));
300 (void)_write(STDERR_FILENO, p, strlen(p));
301 (void)_write(STDERR_FILENO, "\n", 1);