fstab.c revision 1.13 1 /* $NetBSD: fstab.c,v 1.13 1998/07/26 13:47:20 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: fstab.c,v 1.13 1998/07/26 13:47:20 mycroft Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include <sys/types.h>
46 #include <sys/uio.h>
47 #include <errno.h>
48 #include <fstab.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #ifdef __weak_alias
55 __weak_alias(endfsent,_endfsent);
56 __weak_alias(getfsent,_getfsent);
57 __weak_alias(getfsfile,_getfsfile);
58 __weak_alias(getfsspec,_getfsspec);
59 __weak_alias(setfsent,_setfsent);
60 #endif
61
62 static FILE *_fs_fp;
63 static struct fstab _fs_fstab;
64
65 static void error __P((int));
66 static int fstabscan __P((void));
67
68 static int
69 fstabscan()
70 {
71 char *cp;
72 #define MAXLINELENGTH 1024
73 static char line[MAXLINELENGTH];
74 char subline[MAXLINELENGTH];
75 int typexx;
76 static const char sep[] = ":\n";
77 static const char ws[] = " \t\n";
78
79 for (;;) {
80 if (!(cp = fgets(line, sizeof(line), _fs_fp)))
81 return(0);
82 /* OLD_STYLE_FSTAB */
83 if (!strpbrk(cp, " \t")) {
84 _fs_fstab.fs_spec = strtok(cp, sep);
85 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
86 continue;
87 _fs_fstab.fs_file = strtok(NULL, sep);
88 _fs_fstab.fs_type = strtok(NULL, sep);
89 if (_fs_fstab.fs_type) {
90 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
91 continue;
92 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
93 _fs_fstab.fs_vfstype =
94 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
95 "ufs" : "swap";
96 if ((cp = strtok(NULL, sep)) != NULL) {
97 _fs_fstab.fs_freq = atoi(cp);
98 if ((cp = strtok(NULL, sep)) != NULL) {
99 _fs_fstab.fs_passno = atoi(cp);
100 return(1);
101 }
102 }
103 }
104 goto bad;
105 }
106 /* OLD_STYLE_FSTAB */
107 _fs_fstab.fs_spec = strtok(cp, ws);
108 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
109 continue;
110 _fs_fstab.fs_file = strtok(NULL, ws);
111 _fs_fstab.fs_vfstype = strtok(NULL, ws);
112 _fs_fstab.fs_mntops = strtok(NULL, ws);
113 if (_fs_fstab.fs_mntops == NULL)
114 goto bad;
115 _fs_fstab.fs_freq = 0;
116 _fs_fstab.fs_passno = 0;
117 if ((cp = strtok(NULL, ws)) != NULL) {
118 _fs_fstab.fs_freq = atoi(cp);
119 if ((cp = strtok(NULL, ws)) != NULL)
120 _fs_fstab.fs_passno = atoi(cp);
121 }
122 (void)strncpy(subline, _fs_fstab.fs_mntops, sizeof(subline)-1);
123 for (typexx = 0, cp = strtok(subline, ","); cp;
124 cp = strtok((char *)NULL, ",")) {
125 if (strlen(cp) != 2)
126 continue;
127 if (!strcmp(cp, FSTAB_RW)) {
128 _fs_fstab.fs_type = FSTAB_RW;
129 break;
130 }
131 if (!strcmp(cp, FSTAB_RQ)) {
132 _fs_fstab.fs_type = FSTAB_RQ;
133 break;
134 }
135 if (!strcmp(cp, FSTAB_RO)) {
136 _fs_fstab.fs_type = FSTAB_RO;
137 break;
138 }
139 if (!strcmp(cp, FSTAB_SW)) {
140 _fs_fstab.fs_type = FSTAB_SW;
141 break;
142 }
143 if (!strcmp(cp, FSTAB_XX)) {
144 _fs_fstab.fs_type = FSTAB_XX;
145 typexx++;
146 break;
147 }
148 }
149 if (typexx)
150 continue;
151 if (cp != NULL)
152 return(1);
153
154 bad: /* no way to distinguish between EOF and syntax error */
155 error(EFTYPE);
156 }
157 /* NOTREACHED */
158 }
159
160 struct fstab *
161 getfsent()
162 {
163 if ((!_fs_fp && !setfsent()) || !fstabscan())
164 return((struct fstab *)NULL);
165 return(&_fs_fstab);
166 }
167
168 struct fstab *
169 getfsspec(name)
170 const char *name;
171 {
172 if (setfsent())
173 while (fstabscan())
174 if (!strcmp(_fs_fstab.fs_spec, name))
175 return(&_fs_fstab);
176 return((struct fstab *)NULL);
177 }
178
179 struct fstab *
180 getfsfile(name)
181 const char *name;
182 {
183 if (setfsent())
184 while (fstabscan())
185 if (!strcmp(_fs_fstab.fs_file, name))
186 return(&_fs_fstab);
187 return((struct fstab *)NULL);
188 }
189
190 int
191 setfsent()
192 {
193 if (_fs_fp) {
194 rewind(_fs_fp);
195 return(1);
196 }
197 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL)
198 return(1);
199 error(errno);
200 return(0);
201 }
202
203 void
204 endfsent()
205 {
206 if (_fs_fp) {
207 (void)fclose(_fs_fp);
208 _fs_fp = NULL;
209 }
210 }
211
212 static void
213 error(err)
214 int err;
215 {
216 struct iovec iov[5];
217
218 iov[0].iov_base = "fstab: ";
219 iov[0].iov_len = 7;
220 iov[1].iov_base = _PATH_FSTAB;
221 iov[1].iov_len = sizeof(_PATH_FSTAB) - 1;
222 iov[2].iov_base = ": ";
223 iov[2].iov_len = 2;
224 iov[3].iov_base = (char *)strerror(err);
225 iov[3].iov_len = strlen(iov[3].iov_base);
226 iov[4].iov_base = "\n";
227 iov[4].iov_len = 1;
228 (void)writev(STDERR_FILENO, iov, 5);
229 }
230