fstab.c revision 7327
155714Skris/*
255714Skris * Copyright (c) 1980, 1988, 1993
355714Skris *	The Regents of the University of California.  All rights reserved.
455714Skris *
555714Skris * Redistribution and use in source and binary forms, with or without
655714Skris * modification, are permitted provided that the following conditions
755714Skris * are met:
8280304Sjkim * 1. Redistributions of source code must retain the above copyright
955714Skris *    notice, this list of conditions and the following disclaimer.
1055714Skris * 2. Redistributions in binary form must reproduce the above copyright
1155714Skris *    notice, this list of conditions and the following disclaimer in the
1255714Skris *    documentation and/or other materials provided with the distribution.
1355714Skris * 3. All advertising materials mentioning features or use of this software
1455714Skris *    must display the following acknowledgement:
15280304Sjkim *	This product includes software developed by the University of
1655714Skris *	California, Berkeley and its contributors.
1755714Skris * 4. Neither the name of the University nor the names of its contributors
1855714Skris *    may be used to endorse or promote products derived from this software
1955714Skris *    without specific prior written permission.
2055714Skris *
2155714Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22280304Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155714Skris * SUCH DAMAGE.
3255714Skris */
3355714Skris
3455714Skris#if defined(LIBC_SCCS) && !defined(lint)
3555714Skrisstatic char sccsid[] = "@(#)fstab.c	8.1 (Berkeley) 6/4/93";
3655714Skris#endif /* LIBC_SCCS and not lint */
37280304Sjkim
3855714Skris#include <errno.h>
3955714Skris#include <fstab.h>
40280304Sjkim#include <stdio.h>
4155714Skris#include <stdlib.h>
4255714Skris#include <string.h>
4355714Skris#include <unistd.h>
4455714Skris
4555714Skrisstatic FILE *_fs_fp;
4655714Skrisstatic struct fstab _fs_fstab;
4755714Skrisstatic int LineNo = 0;
4855714Skris
4955714Skrisstatic error __P((int));
5055714Skrisstatic fstabscan __P((void));
5155714Skris
52280304Sjkimstatic
5355714Skrisfstabscan()
5455714Skris{
5555714Skris	char *cp, *p;
5655714Skris#define	MAXLINELENGTH	1024
5755714Skris	static char line[MAXLINELENGTH];
5855714Skris	char subline[MAXLINELENGTH];
5955714Skris	int typexx;
6055714Skris
6155714Skris	for (;;) {
62280304Sjkim
63280304Sjkim		if (!(p = fgets(line, sizeof(line), _fs_fp)))
64280304Sjkim			return(0);
65280304Sjkim/* OLD_STYLE_FSTAB */
6655714Skris		++LineNo;
6768651Skris		if (*line == '#' || *line == '\n')
68280304Sjkim			continue;
69280304Sjkim		if (!strpbrk(p, " \t")) {
70280304Sjkim			_fs_fstab.fs_spec = strsep(&p, ":\n");
71280304Sjkim			_fs_fstab.fs_file = strsep(&p, ":\n");
72280304Sjkim			_fs_fstab.fs_type = strsep(&p, ":\n");
73280304Sjkim			if (_fs_fstab.fs_type) {
74280304Sjkim				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
75280304Sjkim					continue;
76280304Sjkim				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
77280304Sjkim				_fs_fstab.fs_vfstype =
78280304Sjkim				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
7955714Skris				    "ufs" : "swap";
80280304Sjkim				if (cp = strsep(&p, ":\n")) {
81280304Sjkim					_fs_fstab.fs_freq = atoi(cp);
82280304Sjkim					if (cp = strsep(&p, ":\n")) {
83280304Sjkim						_fs_fstab.fs_passno = atoi(cp);
84280304Sjkim						return(1);
85280304Sjkim					}
86280304Sjkim				}
87280304Sjkim			}
88280304Sjkim			goto bad;
89280304Sjkim		}
90280304Sjkim/* OLD_STYLE_FSTAB */
91280304Sjkim		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
92280304Sjkim			;
93280304Sjkim		_fs_fstab.fs_spec = cp;
94280304Sjkim		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
95280304Sjkim			continue;
96280304Sjkim		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
97280304Sjkim			;
98280304Sjkim		_fs_fstab.fs_file = cp;
99280304Sjkim		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
100280304Sjkim			;
101280304Sjkim		_fs_fstab.fs_vfstype = cp;
102280304Sjkim		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
103280304Sjkim			;
104280304Sjkim		_fs_fstab.fs_mntops = cp;
105280304Sjkim		if (_fs_fstab.fs_mntops == NULL)
106280304Sjkim			goto bad;
107280304Sjkim		_fs_fstab.fs_freq = 0;
108280304Sjkim		_fs_fstab.fs_passno = 0;
109280304Sjkim		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
110280304Sjkim			;
111		if (cp != NULL) {
112			_fs_fstab.fs_freq = atoi(cp);
113			while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
114				;
115			if (cp != NULL)
116				_fs_fstab.fs_passno = atoi(cp);
117		}
118		strcpy(subline, _fs_fstab.fs_mntops);
119		p = subline;
120		for (typexx = 0, cp = strsep(&p, ","); cp;
121		     cp = strsep(&p, ",")) {
122			if (strlen(cp) != 2)
123				continue;
124			if (!strcmp(cp, FSTAB_RW)) {
125				_fs_fstab.fs_type = FSTAB_RW;
126				break;
127			}
128			if (!strcmp(cp, FSTAB_RQ)) {
129				_fs_fstab.fs_type = FSTAB_RQ;
130				break;
131			}
132			if (!strcmp(cp, FSTAB_RO)) {
133				_fs_fstab.fs_type = FSTAB_RO;
134				break;
135			}
136			if (!strcmp(cp, FSTAB_SW)) {
137				_fs_fstab.fs_type = FSTAB_SW;
138				break;
139			}
140			if (!strcmp(cp, FSTAB_XX)) {
141				_fs_fstab.fs_type = FSTAB_XX;
142				typexx++;
143				break;
144			}
145		}
146		if (typexx)
147			continue;
148		if (cp != NULL)
149			return(1);
150
151bad:		/* no way to distinguish between EOF and syntax error */
152		error(EFTYPE);
153	}
154	/* NOTREACHED */
155}
156
157struct fstab *
158getfsent()
159{
160	if (!_fs_fp && !setfsent() || !fstabscan())
161		return((struct fstab *)NULL);
162	return(&_fs_fstab);
163}
164
165struct fstab *
166getfsspec(name)
167	register const char *name;
168{
169	if (setfsent())
170		while (fstabscan())
171			if (!strcmp(_fs_fstab.fs_spec, name))
172				return(&_fs_fstab);
173	return((struct fstab *)NULL);
174}
175
176struct fstab *
177getfsfile(name)
178	register const char *name;
179{
180	if (setfsent())
181		while (fstabscan())
182			if (!strcmp(_fs_fstab.fs_file, name))
183				return(&_fs_fstab);
184	return((struct fstab *)NULL);
185}
186
187setfsent()
188{
189	if (_fs_fp) {
190		rewind(_fs_fp);
191		LineNo = 0;
192		return(1);
193	}
194	if (_fs_fp = fopen(_PATH_FSTAB, "r")) {
195		LineNo = 0;
196		return(1);
197	}
198	error(errno);
199	return(0);
200}
201
202void
203endfsent()
204{
205	if (_fs_fp) {
206		(void)fclose(_fs_fp);
207		_fs_fp = NULL;
208	}
209}
210
211static
212error(err)
213	int err;
214{
215	char *p;
216	char num[30];
217
218	(void)write(STDERR_FILENO, "fstab: ", 7);
219	(void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
220	(void)write(STDERR_FILENO, ":", 1);
221	sprintf(num, "%d: ", LineNo);
222	(void)write(STDERR_FILENO, num, strlen(num));
223	p = strerror(err);
224	(void)write(STDERR_FILENO, p, strlen(p));
225	(void)write(STDERR_FILENO, "\n", 1);
226}
227