fstab.c revision 1573
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)
35static char sccsid[] = "@(#)fstab.c	8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37
38#include <errno.h>
39#include <fstab.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45static FILE *_fs_fp;
46static struct fstab _fs_fstab;
47
48static error __P((int));
49static fstabscan __P((void));
50
51static
52fstabscan()
53{
54	register char *cp;
55#define	MAXLINELENGTH	1024
56	static char line[MAXLINELENGTH];
57	char subline[MAXLINELENGTH];
58	int typexx;
59
60	for (;;) {
61		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
62			return(0);
63/* OLD_STYLE_FSTAB */
64		if (!strpbrk(cp, " \t")) {
65			_fs_fstab.fs_spec = strtok(cp, ":\n");
66			_fs_fstab.fs_file = strtok((char *)NULL, ":\n");
67			_fs_fstab.fs_type = strtok((char *)NULL, ":\n");
68			if (_fs_fstab.fs_type) {
69				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
70					continue;
71				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
72				_fs_fstab.fs_vfstype =
73				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
74				    "ufs" : "swap";
75				if (cp = strtok((char *)NULL, ":\n")) {
76					_fs_fstab.fs_freq = atoi(cp);
77					if (cp = strtok((char *)NULL, ":\n")) {
78						_fs_fstab.fs_passno = atoi(cp);
79						return(1);
80					}
81				}
82			}
83			goto bad;
84		}
85/* OLD_STYLE_FSTAB */
86		_fs_fstab.fs_spec = strtok(cp, " \t\n");
87		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
88			continue;
89		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
90		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
91		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
92		if (_fs_fstab.fs_mntops == NULL)
93			goto bad;
94		_fs_fstab.fs_freq = 0;
95		_fs_fstab.fs_passno = 0;
96		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
97			_fs_fstab.fs_freq = atoi(cp);
98			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
99				_fs_fstab.fs_passno = atoi(cp);
100		}
101		strcpy(subline, _fs_fstab.fs_mntops);
102		for (typexx = 0, cp = strtok(subline, ","); cp;
103		     cp = strtok((char *)NULL, ",")) {
104			if (strlen(cp) != 2)
105				continue;
106			if (!strcmp(cp, FSTAB_RW)) {
107				_fs_fstab.fs_type = FSTAB_RW;
108				break;
109			}
110			if (!strcmp(cp, FSTAB_RQ)) {
111				_fs_fstab.fs_type = FSTAB_RQ;
112				break;
113			}
114			if (!strcmp(cp, FSTAB_RO)) {
115				_fs_fstab.fs_type = FSTAB_RO;
116				break;
117			}
118			if (!strcmp(cp, FSTAB_SW)) {
119				_fs_fstab.fs_type = FSTAB_SW;
120				break;
121			}
122			if (!strcmp(cp, FSTAB_XX)) {
123				_fs_fstab.fs_type = FSTAB_XX;
124				typexx++;
125				break;
126			}
127		}
128		if (typexx)
129			continue;
130		if (cp != NULL)
131			return(1);
132
133bad:		/* no way to distinguish between EOF and syntax error */
134		error(EFTYPE);
135	}
136	/* NOTREACHED */
137}
138
139struct fstab *
140getfsent()
141{
142	if (!_fs_fp && !setfsent() || !fstabscan())
143		return((struct fstab *)NULL);
144	return(&_fs_fstab);
145}
146
147struct fstab *
148getfsspec(name)
149	register const char *name;
150{
151	if (setfsent())
152		while (fstabscan())
153			if (!strcmp(_fs_fstab.fs_spec, name))
154				return(&_fs_fstab);
155	return((struct fstab *)NULL);
156}
157
158struct fstab *
159getfsfile(name)
160	register const char *name;
161{
162	if (setfsent())
163		while (fstabscan())
164			if (!strcmp(_fs_fstab.fs_file, name))
165				return(&_fs_fstab);
166	return((struct fstab *)NULL);
167}
168
169setfsent()
170{
171	if (_fs_fp) {
172		rewind(_fs_fp);
173		return(1);
174	}
175	if (_fs_fp = fopen(_PATH_FSTAB, "r"))
176		return(1);
177	error(errno);
178	return(0);
179}
180
181void
182endfsent()
183{
184	if (_fs_fp) {
185		(void)fclose(_fs_fp);
186		_fs_fp = NULL;
187	}
188}
189
190static
191error(err)
192	int err;
193{
194	char *p;
195
196	(void)write(STDERR_FILENO, "fstab: ", 7);
197	(void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
198	(void)write(STDERR_FILENO, ": ", 1);
199	p = strerror(err);
200	(void)write(STDERR_FILENO, p, strlen(p));
201	(void)write(STDERR_FILENO, "\n", 1);
202}
203