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