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
33 * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.8 2000/01/27 23:06:15 jasone Exp $
34 * $DragonFly: src/lib/libc/gen/fstab.c,v 1.6 2005/11/13 00:07:42 swildner Exp $
36 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
37 * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.8 2000/01/27 23:06:15 jasone Exp $
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;
58 static void error (int);
59 static void fixfsfile (void);
60 static int fstabscan (void);
65 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
69 if (strcmp(_fs_fstab.fs_file, "/") != 0)
71 if (statfs("/", &sf) != 0)
73 if (sf.f_mntfromname[0] == '/')
76 strcpy(buf, _PATH_DEV);
77 strcat(buf, sf.f_mntfromname);
78 if (stat(buf, &sb) != 0 ||
79 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
81 _fs_fstab.fs_spec = buf;
88 #define MAXLINELENGTH 1024
89 static char line[MAXLINELENGTH];
90 char subline[MAXLINELENGTH];
95 if (!(p = fgets(line, sizeof(line), _fs_fp)))
99 if (*line == '#' || *line == '\n')
101 if (!strpbrk(p, " \t")) {
102 _fs_fstab.fs_spec = strsep(&p, ":\n");
103 _fs_fstab.fs_file = strsep(&p, ":\n");
105 _fs_fstab.fs_type = strsep(&p, ":\n");
106 if (_fs_fstab.fs_type) {
107 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
109 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
110 _fs_fstab.fs_vfstype =
111 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
113 if ((cp = strsep(&p, ":\n")) != NULL) {
114 _fs_fstab.fs_freq = atoi(cp);
115 if ((cp = strsep(&p, ":\n")) != NULL) {
116 _fs_fstab.fs_passno = atoi(cp);
123 /* OLD_STYLE_FSTAB */
124 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
126 _fs_fstab.fs_spec = cp;
127 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
129 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
131 _fs_fstab.fs_file = cp;
133 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
135 _fs_fstab.fs_vfstype = cp;
136 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
138 _fs_fstab.fs_mntops = cp;
139 if (_fs_fstab.fs_mntops == NULL)
141 _fs_fstab.fs_freq = 0;
142 _fs_fstab.fs_passno = 0;
143 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
146 _fs_fstab.fs_freq = atoi(cp);
147 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
150 _fs_fstab.fs_passno = atoi(cp);
152 strcpy(subline, _fs_fstab.fs_mntops);
154 for (typexx = 0, cp = strsep(&p, ","); cp;
155 cp = strsep(&p, ",")) {
158 if (!strcmp(cp, FSTAB_RW)) {
159 _fs_fstab.fs_type = FSTAB_RW;
162 if (!strcmp(cp, FSTAB_RQ)) {
163 _fs_fstab.fs_type = FSTAB_RQ;
166 if (!strcmp(cp, FSTAB_RO)) {
167 _fs_fstab.fs_type = FSTAB_RO;
170 if (!strcmp(cp, FSTAB_SW)) {
171 _fs_fstab.fs_type = FSTAB_SW;
174 if (!strcmp(cp, FSTAB_XX)) {
175 _fs_fstab.fs_type = FSTAB_XX;
185 bad: /* no way to distinguish between EOF and syntax error */
194 if ((!_fs_fp && !setfsent()) || !fstabscan())
195 return((struct fstab *)NULL);
200 getfsspec(const char *name)
204 if (!strcmp(_fs_fstab.fs_spec, name))
206 return((struct fstab *)NULL);
210 getfsfile(const char *name)
214 if (!strcmp(_fs_fstab.fs_file, name))
216 return((struct fstab *)NULL);
227 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) {
250 _write(STDERR_FILENO, "fstab: ", 7);
251 _write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
252 _write(STDERR_FILENO, ":", 1);
253 sprintf(num, "%d: ", LineNo);
254 _write(STDERR_FILENO, num, strlen(num));
256 _write(STDERR_FILENO, p, strlen(p));
257 _write(STDERR_FILENO, "\n", 1);