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/libcr/gen/Attic/fstab.c,v 1.4 2004/07/05 17:30:59 eirikn 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 <sys/param.h>
41 #include <sys/mount.h>
53 static struct fstab _fs_fstab;
54 static int LineNo = 0;
56 static void error (int);
57 static void fixfsfile (void);
58 static int fstabscan (void);
63 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
67 if (strcmp(_fs_fstab.fs_file, "/") != 0)
69 if (statfs("/", &sf) != 0)
71 if (sf.f_mntfromname[0] == '/')
74 strcpy(buf, _PATH_DEV);
75 strcat(buf, sf.f_mntfromname);
76 if (stat(buf, &sb) != 0 ||
77 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
79 _fs_fstab.fs_spec = buf;
86 #define MAXLINELENGTH 1024
87 static char line[MAXLINELENGTH];
88 char subline[MAXLINELENGTH];
93 if (!(p = fgets(line, sizeof(line), _fs_fp)))
97 if (*line == '#' || *line == '\n')
99 if (!strpbrk(p, " \t")) {
100 _fs_fstab.fs_spec = strsep(&p, ":\n");
101 _fs_fstab.fs_file = strsep(&p, ":\n");
103 _fs_fstab.fs_type = strsep(&p, ":\n");
104 if (_fs_fstab.fs_type) {
105 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
107 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
108 _fs_fstab.fs_vfstype =
109 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
111 if ((cp = strsep(&p, ":\n")) != NULL) {
112 _fs_fstab.fs_freq = atoi(cp);
113 if ((cp = strsep(&p, ":\n")) != NULL) {
114 _fs_fstab.fs_passno = atoi(cp);
121 /* OLD_STYLE_FSTAB */
122 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
124 _fs_fstab.fs_spec = cp;
125 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
127 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
129 _fs_fstab.fs_file = cp;
131 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
133 _fs_fstab.fs_vfstype = cp;
134 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
136 _fs_fstab.fs_mntops = cp;
137 if (_fs_fstab.fs_mntops == NULL)
139 _fs_fstab.fs_freq = 0;
140 _fs_fstab.fs_passno = 0;
141 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
144 _fs_fstab.fs_freq = atoi(cp);
145 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
148 _fs_fstab.fs_passno = atoi(cp);
150 strcpy(subline, _fs_fstab.fs_mntops);
152 for (typexx = 0, cp = strsep(&p, ","); cp;
153 cp = strsep(&p, ",")) {
156 if (!strcmp(cp, FSTAB_RW)) {
157 _fs_fstab.fs_type = FSTAB_RW;
160 if (!strcmp(cp, FSTAB_RQ)) {
161 _fs_fstab.fs_type = FSTAB_RQ;
164 if (!strcmp(cp, FSTAB_RO)) {
165 _fs_fstab.fs_type = FSTAB_RO;
168 if (!strcmp(cp, FSTAB_SW)) {
169 _fs_fstab.fs_type = FSTAB_SW;
172 if (!strcmp(cp, FSTAB_XX)) {
173 _fs_fstab.fs_type = FSTAB_XX;
183 bad: /* no way to distinguish between EOF and syntax error */
192 if ((!_fs_fp && !setfsent()) || !fstabscan())
193 return((struct fstab *)NULL);
203 if (!strcmp(_fs_fstab.fs_spec, name))
205 return((struct fstab *)NULL);
214 if (!strcmp(_fs_fstab.fs_file, name))
216 return((struct fstab *)NULL);
227 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) {
239 (void)fclose(_fs_fp);
251 (void)_write(STDERR_FILENO, "fstab: ", 7);
252 (void)_write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
253 (void)_write(STDERR_FILENO, ":", 1);
254 sprintf(num, "%d: ", LineNo);
255 (void)_write(STDERR_FILENO, num, strlen(num));
257 (void)_write(STDERR_FILENO, p, strlen(p));
258 (void)_write(STDERR_FILENO, "\n", 1);