fstab.c revision 55028
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)
35#if 0
36static char sccsid[] = "@(#)fstab.c	8.1 (Berkeley) 6/4/93";
37#else
38static char rcsid[] =
39 "$FreeBSD: head/lib/libc/gen/fstab.c 55028 1999-12-23 14:44:36Z peter $";
40#endif
41#endif /* LIBC_SCCS and not lint */
42
43#include <sys/param.h>
44#include <sys/mount.h>
45#include <sys/stat.h>
46
47#include <errno.h>
48#include <fstab.h>
49#include <paths.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54
55static FILE *_fs_fp;
56static struct fstab _fs_fstab;
57static int LineNo = 0;
58
59static void error __P((int));
60static void fixfsfile __P((void));
61static int fstabscan __P((void));
62
63static void
64fixfsfile()
65{
66	static char buf[sizeof(_PATH_DEV) + MNAMELEN];
67	struct stat sb;
68	struct statfs sf;
69
70	if (strcmp(_fs_fstab.fs_file, "/") != 0)
71		return;
72	if (statfs("/", &sf) != 0)
73		return;
74	if (sf.f_mntfromname[0] == '/')
75		buf[0] = '\0';
76	else
77		strcpy(buf, _PATH_DEV);
78	strcat(buf, sf.f_mntfromname);
79	if (stat(buf, &sb) != 0 ||
80	    (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
81		return;
82	_fs_fstab.fs_spec = buf;
83}
84
85static int
86fstabscan()
87{
88	char *cp, *p;
89#define	MAXLINELENGTH	1024
90	static char line[MAXLINELENGTH];
91	char subline[MAXLINELENGTH];
92	int typexx;
93
94	for (;;) {
95
96		if (!(p = fgets(line, sizeof(line), _fs_fp)))
97			return(0);
98/* OLD_STYLE_FSTAB */
99		++LineNo;
100		if (*line == '#' || *line == '\n')
101			continue;
102		if (!strpbrk(p, " \t")) {
103			_fs_fstab.fs_spec = strsep(&p, ":\n");
104			_fs_fstab.fs_file = strsep(&p, ":\n");
105			fixfsfile();
106			_fs_fstab.fs_type = strsep(&p, ":\n");
107			if (_fs_fstab.fs_type) {
108				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
109					continue;
110				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
111				_fs_fstab.fs_vfstype =
112				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
113				    "ufs" : "swap";
114				if ((cp = strsep(&p, ":\n")) != NULL) {
115					_fs_fstab.fs_freq = atoi(cp);
116					if ((cp = strsep(&p, ":\n")) != NULL) {
117						_fs_fstab.fs_passno = atoi(cp);
118						return(1);
119					}
120				}
121			}
122			goto bad;
123		}
124/* OLD_STYLE_FSTAB */
125		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
126			;
127		_fs_fstab.fs_spec = cp;
128		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
129			continue;
130		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
131			;
132		_fs_fstab.fs_file = cp;
133		fixfsfile();
134		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
135			;
136		_fs_fstab.fs_vfstype = cp;
137		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
138			;
139		_fs_fstab.fs_mntops = cp;
140		if (_fs_fstab.fs_mntops == NULL)
141			goto bad;
142		_fs_fstab.fs_freq = 0;
143		_fs_fstab.fs_passno = 0;
144		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
145			;
146		if (cp != NULL) {
147			_fs_fstab.fs_freq = atoi(cp);
148			while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
149				;
150			if (cp != NULL)
151				_fs_fstab.fs_passno = atoi(cp);
152		}
153		strcpy(subline, _fs_fstab.fs_mntops);
154		p = subline;
155		for (typexx = 0, cp = strsep(&p, ","); cp;
156		     cp = strsep(&p, ",")) {
157			if (strlen(cp) != 2)
158				continue;
159			if (!strcmp(cp, FSTAB_RW)) {
160				_fs_fstab.fs_type = FSTAB_RW;
161				break;
162			}
163			if (!strcmp(cp, FSTAB_RQ)) {
164				_fs_fstab.fs_type = FSTAB_RQ;
165				break;
166			}
167			if (!strcmp(cp, FSTAB_RO)) {
168				_fs_fstab.fs_type = FSTAB_RO;
169				break;
170			}
171			if (!strcmp(cp, FSTAB_SW)) {
172				_fs_fstab.fs_type = FSTAB_SW;
173				break;
174			}
175			if (!strcmp(cp, FSTAB_XX)) {
176				_fs_fstab.fs_type = FSTAB_XX;
177				typexx++;
178				break;
179			}
180		}
181		if (typexx)
182			continue;
183		if (cp != NULL)
184			return(1);
185
186bad:		/* no way to distinguish between EOF and syntax error */
187		error(EFTYPE);
188	}
189	/* NOTREACHED */
190}
191
192struct fstab *
193getfsent()
194{
195	if ((!_fs_fp && !setfsent()) || !fstabscan())
196		return((struct fstab *)NULL);
197	return(&_fs_fstab);
198}
199
200struct fstab *
201getfsspec(name)
202	register const char *name;
203{
204	if (setfsent())
205		while (fstabscan())
206			if (!strcmp(_fs_fstab.fs_spec, name))
207				return(&_fs_fstab);
208	return((struct fstab *)NULL);
209}
210
211struct fstab *
212getfsfile(name)
213	register const char *name;
214{
215	if (setfsent())
216		while (fstabscan())
217			if (!strcmp(_fs_fstab.fs_file, name))
218				return(&_fs_fstab);
219	return((struct fstab *)NULL);
220}
221
222int
223setfsent()
224{
225	if (_fs_fp) {
226		rewind(_fs_fp);
227		LineNo = 0;
228		return(1);
229	}
230	if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) {
231		LineNo = 0;
232		return(1);
233	}
234	error(errno);
235	return(0);
236}
237
238void
239endfsent()
240{
241	if (_fs_fp) {
242		(void)fclose(_fs_fp);
243		_fs_fp = NULL;
244	}
245}
246
247static void
248error(err)
249	int err;
250{
251	char *p;
252	char num[30];
253
254	(void)write(STDERR_FILENO, "fstab: ", 7);
255	(void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
256	(void)write(STDERR_FILENO, ":", 1);
257	sprintf(num, "%d: ", LineNo);
258	(void)write(STDERR_FILENO, num, strlen(num));
259	p = strerror(err);
260	(void)write(STDERR_FILENO, p, strlen(p));
261	(void)write(STDERR_FILENO, "\n", 1);
262}
263