Merge from vendor branch BZIP:
[dragonfly.git] / lib / libc / gen / fstab.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.8 2000/01/27 23:06:15 jasone Exp $
0f52d283 34 * $DragonFly: src/lib/libc/gen/fstab.c,v 1.6 2005/11/13 00:07:42 swildner Exp $
1de703da
MD
35 *
36 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
37 * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.8 2000/01/27 23:06:15 jasone Exp $
984263bc
MD
38 */
39
17ea2221 40#include "namespace.h"
984263bc
MD
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>
17ea2221 52#include "un-namespace.h"
984263bc
MD
53
54static FILE *_fs_fp;
55static struct fstab _fs_fstab;
56static int LineNo = 0;
57
064e1fb3
EN
58static void error (int);
59static void fixfsfile (void);
60static int fstabscan (void);
984263bc
MD
61
62static void
0f52d283 63fixfsfile(void)
984263bc
MD
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
0f52d283 85fstabscan(void)
984263bc
MD
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 *
0f52d283 192getfsent(void)
984263bc
MD
193{
194 if ((!_fs_fp && !setfsent()) || !fstabscan())
195 return((struct fstab *)NULL);
196 return(&_fs_fstab);
197}
198
199struct fstab *
0f52d283 200getfsspec(const char *name)
984263bc
MD
201{
202 if (setfsent())
203 while (fstabscan())
204 if (!strcmp(_fs_fstab.fs_spec, name))
205 return(&_fs_fstab);
206 return((struct fstab *)NULL);
207}
208
209struct fstab *
0f52d283 210getfsfile(const char *name)
984263bc
MD
211{
212 if (setfsent())
213 while (fstabscan())
214 if (!strcmp(_fs_fstab.fs_file, name))
215 return(&_fs_fstab);
216 return((struct fstab *)NULL);
217}
218
219int
0f52d283 220setfsent(void)
984263bc
MD
221{
222 if (_fs_fp) {
223 rewind(_fs_fp);
224 LineNo = 0;
225 return(1);
226 }
227 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) {
228 LineNo = 0;
229 return(1);
230 }
231 error(errno);
232 return(0);
233}
234
235void
0f52d283 236endfsent(void)
984263bc
MD
237{
238 if (_fs_fp) {
0f52d283 239 fclose(_fs_fp);
984263bc
MD
240 _fs_fp = NULL;
241 }
242}
243
244static void
0f52d283 245error(int err)
984263bc
MD
246{
247 char *p;
248 char num[30];
249
0f52d283
SW
250 _write(STDERR_FILENO, "fstab: ", 7);
251 _write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
252 _write(STDERR_FILENO, ":", 1);
984263bc 253 sprintf(num, "%d: ", LineNo);
0f52d283 254 _write(STDERR_FILENO, num, strlen(num));
984263bc 255 p = strerror(err);
0f52d283
SW
256 _write(STDERR_FILENO, p, strlen(p));
257 _write(STDERR_FILENO, "\n", 1);
984263bc 258}