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