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