Deleted Added
full compact
fstab.c (84768) fstab.c (90045)
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)
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#if 0
36static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
35static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
37#else
38static char rcsid[] =
39 "$FreeBSD: head/lib/libc/gen/fstab.c 84768 2001-10-10 17:48:44Z bde $";
40#endif
41#endif /* LIBC_SCCS and not lint */
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/gen/fstab.c 90045 2002-02-01 01:32:19Z obrien $");
42
43#include "namespace.h"
44#include <sys/param.h>
45#include <sys/mount.h>
46#include <sys/stat.h>
47
48#include <errno.h>
49#include <fstab.h>
50#include <paths.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55#include "un-namespace.h"
56
57static FILE *_fs_fp;
58static struct fstab _fs_fstab;
59static int LineNo = 0;
60
39
40#include "namespace.h"
41#include <sys/param.h>
42#include <sys/mount.h>
43#include <sys/stat.h>
44
45#include <errno.h>
46#include <fstab.h>
47#include <paths.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52#include "un-namespace.h"
53
54static FILE *_fs_fp;
55static struct fstab _fs_fstab;
56static int LineNo = 0;
57
61static void error __P((int));
62static void fixfsfile __P((void));
63static int fstabscan __P((void));
58static void error(int);
59static void fixfsfile(void);
60static int fstabscan(void);
64
65static void
66fixfsfile()
67{
68 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
69 struct stat sb;
70 struct statfs sf;
71
72 if (strcmp(_fs_fstab.fs_file, "/") != 0)
73 return;
74 if (statfs("/", &sf) != 0)
75 return;
76 if (sf.f_mntfromname[0] == '/')
77 buf[0] = '\0';
78 else
79 strcpy(buf, _PATH_DEV);
80 strcat(buf, sf.f_mntfromname);
81 if (stat(buf, &sb) != 0 ||
82 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
83 return;
84 _fs_fstab.fs_spec = buf;
85}
86
87static int
88fstabscan()
89{
90 char *cp, *p;
91#define MAXLINELENGTH 1024
92 static char line[MAXLINELENGTH];
93 char subline[MAXLINELENGTH];
94 int typexx;
95
96 for (;;) {
97
98 if (!(p = fgets(line, sizeof(line), _fs_fp)))
99 return(0);
100/* OLD_STYLE_FSTAB */
101 ++LineNo;
102 if (*line == '#' || *line == '\n')
103 continue;
104 if (!strpbrk(p, " \t")) {
105 _fs_fstab.fs_spec = strsep(&p, ":\n");
106 _fs_fstab.fs_file = strsep(&p, ":\n");
107 fixfsfile();
108 _fs_fstab.fs_type = strsep(&p, ":\n");
109 if (_fs_fstab.fs_type) {
110 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
111 continue;
112 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
113 _fs_fstab.fs_vfstype =
114 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
115 "ufs" : "swap";
116 if ((cp = strsep(&p, ":\n")) != NULL) {
117 _fs_fstab.fs_freq = atoi(cp);
118 if ((cp = strsep(&p, ":\n")) != NULL) {
119 _fs_fstab.fs_passno = atoi(cp);
120 return(1);
121 }
122 }
123 }
124 goto bad;
125 }
126/* OLD_STYLE_FSTAB */
127 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
128 ;
129 _fs_fstab.fs_spec = cp;
130 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
131 continue;
132 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
133 ;
134 _fs_fstab.fs_file = cp;
135 fixfsfile();
136 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
137 ;
138 _fs_fstab.fs_vfstype = cp;
139 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
140 ;
141 _fs_fstab.fs_mntops = cp;
142 if (_fs_fstab.fs_mntops == NULL)
143 goto bad;
144 _fs_fstab.fs_freq = 0;
145 _fs_fstab.fs_passno = 0;
146 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
147 ;
148 if (cp != NULL) {
149 _fs_fstab.fs_freq = atoi(cp);
150 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
151 ;
152 if (cp != NULL)
153 _fs_fstab.fs_passno = atoi(cp);
154 }
155 strcpy(subline, _fs_fstab.fs_mntops);
156 p = subline;
157 for (typexx = 0, cp = strsep(&p, ","); cp;
158 cp = strsep(&p, ",")) {
159 if (strlen(cp) != 2)
160 continue;
161 if (!strcmp(cp, FSTAB_RW)) {
162 _fs_fstab.fs_type = FSTAB_RW;
163 break;
164 }
165 if (!strcmp(cp, FSTAB_RQ)) {
166 _fs_fstab.fs_type = FSTAB_RQ;
167 break;
168 }
169 if (!strcmp(cp, FSTAB_RO)) {
170 _fs_fstab.fs_type = FSTAB_RO;
171 break;
172 }
173 if (!strcmp(cp, FSTAB_SW)) {
174 _fs_fstab.fs_type = FSTAB_SW;
175 break;
176 }
177 if (!strcmp(cp, FSTAB_XX)) {
178 _fs_fstab.fs_type = FSTAB_XX;
179 typexx++;
180 break;
181 }
182 }
183 if (typexx)
184 continue;
185 if (cp != NULL)
186 return(1);
187
188bad: /* no way to distinguish between EOF and syntax error */
189 error(EFTYPE);
190 }
191 /* NOTREACHED */
192}
193
194struct fstab *
195getfsent()
196{
197 if ((!_fs_fp && !setfsent()) || !fstabscan())
198 return((struct fstab *)NULL);
199 return(&_fs_fstab);
200}
201
202struct fstab *
203getfsspec(name)
61
62static void
63fixfsfile()
64{
65 static char buf[sizeof(_PATH_DEV) + MNAMELEN];
66 struct stat sb;
67 struct statfs sf;
68
69 if (strcmp(_fs_fstab.fs_file, "/") != 0)
70 return;
71 if (statfs("/", &sf) != 0)
72 return;
73 if (sf.f_mntfromname[0] == '/')
74 buf[0] = '\0';
75 else
76 strcpy(buf, _PATH_DEV);
77 strcat(buf, sf.f_mntfromname);
78 if (stat(buf, &sb) != 0 ||
79 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
80 return;
81 _fs_fstab.fs_spec = buf;
82}
83
84static int
85fstabscan()
86{
87 char *cp, *p;
88#define MAXLINELENGTH 1024
89 static char line[MAXLINELENGTH];
90 char subline[MAXLINELENGTH];
91 int typexx;
92
93 for (;;) {
94
95 if (!(p = fgets(line, sizeof(line), _fs_fp)))
96 return(0);
97/* OLD_STYLE_FSTAB */
98 ++LineNo;
99 if (*line == '#' || *line == '\n')
100 continue;
101 if (!strpbrk(p, " \t")) {
102 _fs_fstab.fs_spec = strsep(&p, ":\n");
103 _fs_fstab.fs_file = strsep(&p, ":\n");
104 fixfsfile();
105 _fs_fstab.fs_type = strsep(&p, ":\n");
106 if (_fs_fstab.fs_type) {
107 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
108 continue;
109 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
110 _fs_fstab.fs_vfstype =
111 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
112 "ufs" : "swap";
113 if ((cp = strsep(&p, ":\n")) != NULL) {
114 _fs_fstab.fs_freq = atoi(cp);
115 if ((cp = strsep(&p, ":\n")) != NULL) {
116 _fs_fstab.fs_passno = atoi(cp);
117 return(1);
118 }
119 }
120 }
121 goto bad;
122 }
123/* OLD_STYLE_FSTAB */
124 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
125 ;
126 _fs_fstab.fs_spec = cp;
127 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
128 continue;
129 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
130 ;
131 _fs_fstab.fs_file = cp;
132 fixfsfile();
133 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
134 ;
135 _fs_fstab.fs_vfstype = cp;
136 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
137 ;
138 _fs_fstab.fs_mntops = cp;
139 if (_fs_fstab.fs_mntops == NULL)
140 goto bad;
141 _fs_fstab.fs_freq = 0;
142 _fs_fstab.fs_passno = 0;
143 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
144 ;
145 if (cp != NULL) {
146 _fs_fstab.fs_freq = atoi(cp);
147 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
148 ;
149 if (cp != NULL)
150 _fs_fstab.fs_passno = atoi(cp);
151 }
152 strcpy(subline, _fs_fstab.fs_mntops);
153 p = subline;
154 for (typexx = 0, cp = strsep(&p, ","); cp;
155 cp = strsep(&p, ",")) {
156 if (strlen(cp) != 2)
157 continue;
158 if (!strcmp(cp, FSTAB_RW)) {
159 _fs_fstab.fs_type = FSTAB_RW;
160 break;
161 }
162 if (!strcmp(cp, FSTAB_RQ)) {
163 _fs_fstab.fs_type = FSTAB_RQ;
164 break;
165 }
166 if (!strcmp(cp, FSTAB_RO)) {
167 _fs_fstab.fs_type = FSTAB_RO;
168 break;
169 }
170 if (!strcmp(cp, FSTAB_SW)) {
171 _fs_fstab.fs_type = FSTAB_SW;
172 break;
173 }
174 if (!strcmp(cp, FSTAB_XX)) {
175 _fs_fstab.fs_type = FSTAB_XX;
176 typexx++;
177 break;
178 }
179 }
180 if (typexx)
181 continue;
182 if (cp != NULL)
183 return(1);
184
185bad: /* no way to distinguish between EOF and syntax error */
186 error(EFTYPE);
187 }
188 /* NOTREACHED */
189}
190
191struct fstab *
192getfsent()
193{
194 if ((!_fs_fp && !setfsent()) || !fstabscan())
195 return((struct fstab *)NULL);
196 return(&_fs_fstab);
197}
198
199struct fstab *
200getfsspec(name)
204 register const char *name;
201 const char *name;
205{
206 if (setfsent())
207 while (fstabscan())
208 if (!strcmp(_fs_fstab.fs_spec, name))
209 return(&_fs_fstab);
210 return((struct fstab *)NULL);
211}
212
213struct fstab *
214getfsfile(name)
202{
203 if (setfsent())
204 while (fstabscan())
205 if (!strcmp(_fs_fstab.fs_spec, name))
206 return(&_fs_fstab);
207 return((struct fstab *)NULL);
208}
209
210struct fstab *
211getfsfile(name)
215 register const char *name;
212 const char *name;
216{
217 if (setfsent())
218 while (fstabscan())
219 if (!strcmp(_fs_fstab.fs_file, name))
220 return(&_fs_fstab);
221 return((struct fstab *)NULL);
222}
223
224int
225setfsent()
226{
227 if (_fs_fp) {
228 rewind(_fs_fp);
229 LineNo = 0;
230 return(1);
231 }
232 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) {
233 LineNo = 0;
234 return(1);
235 }
236 error(errno);
237 return(0);
238}
239
240void
241endfsent()
242{
243 if (_fs_fp) {
244 (void)fclose(_fs_fp);
245 _fs_fp = NULL;
246 }
247}
248
249static void
250error(err)
251 int err;
252{
253 char *p;
254 char num[30];
255
256 (void)_write(STDERR_FILENO, "fstab: ", 7);
257 (void)_write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
258 (void)_write(STDERR_FILENO, ":", 1);
259 sprintf(num, "%d: ", LineNo);
260 (void)_write(STDERR_FILENO, num, strlen(num));
261 p = strerror(err);
262 (void)_write(STDERR_FILENO, p, strlen(p));
263 (void)_write(STDERR_FILENO, "\n", 1);
264}
213{
214 if (setfsent())
215 while (fstabscan())
216 if (!strcmp(_fs_fstab.fs_file, name))
217 return(&_fs_fstab);
218 return((struct fstab *)NULL);
219}
220
221int
222setfsent()
223{
224 if (_fs_fp) {
225 rewind(_fs_fp);
226 LineNo = 0;
227 return(1);
228 }
229 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) {
230 LineNo = 0;
231 return(1);
232 }
233 error(errno);
234 return(0);
235}
236
237void
238endfsent()
239{
240 if (_fs_fp) {
241 (void)fclose(_fs_fp);
242 _fs_fp = NULL;
243 }
244}
245
246static void
247error(err)
248 int err;
249{
250 char *p;
251 char num[30];
252
253 (void)_write(STDERR_FILENO, "fstab: ", 7);
254 (void)_write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
255 (void)_write(STDERR_FILENO, ":", 1);
256 sprintf(num, "%d: ", LineNo);
257 (void)_write(STDERR_FILENO, num, strlen(num));
258 p = strerror(err);
259 (void)_write(STDERR_FILENO, p, strlen(p));
260 (void)_write(STDERR_FILENO, "\n", 1);
261}