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