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