2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)fstab.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.15 2007/01/09 00:27:53 imp Exp $
31 * $DragonFly: src/lib/libc/gen/fstab.c,v 1.6 2005/11/13 00:07:42 swildner Exp $
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/mount.h>
46 #include "un-namespace.h"
49 static struct fstab _fs_fstab
;
50 static int LineNo
= 0;
51 static char *path_fstab
;
52 static char fstab_path
[PATH_MAX
];
53 static int fsp_set
= 0;
55 static void error(int);
56 static void fixfsfile(void);
57 static int fstabscan(void);
60 setfstab(const char *file
)
64 path_fstab
= _PATH_FSTAB
;
66 strncpy(fstab_path
, file
, PATH_MAX
);
67 fstab_path
[PATH_MAX
- 1] = '\0';
68 path_fstab
= fstab_path
;
86 static char buf
[sizeof(_PATH_DEV
) + MNAMELEN
];
90 if (strcmp(_fs_fstab
.fs_file
, "/") != 0)
92 if (statfs("/", &sf
) != 0)
94 if (sf
.f_mntfromname
[0] == '/')
97 strcpy(buf
, _PATH_DEV
);
98 strcat(buf
, sf
.f_mntfromname
);
99 if (stat(buf
, &sb
) != 0 ||
100 (!S_ISBLK(sb
.st_mode
) && !S_ISCHR(sb
.st_mode
)))
102 _fs_fstab
.fs_spec
= buf
;
109 #define MAXLINELENGTH 1024
110 static char line
[MAXLINELENGTH
];
111 char subline
[MAXLINELENGTH
];
116 if (!(p
= fgets(line
, sizeof(line
), _fs_fp
)))
118 /* OLD_STYLE_FSTAB */
120 if (*line
== '#' || *line
== '\n')
122 if (!strpbrk(p
, " \t")) {
123 _fs_fstab
.fs_spec
= strsep(&p
, ":\n");
124 _fs_fstab
.fs_file
= strsep(&p
, ":\n");
126 _fs_fstab
.fs_type
= strsep(&p
, ":\n");
127 if (_fs_fstab
.fs_type
) {
128 if (!strcmp(_fs_fstab
.fs_type
, FSTAB_XX
))
130 _fs_fstab
.fs_mntops
= _fs_fstab
.fs_type
;
131 _fs_fstab
.fs_vfstype
=
132 strcmp(_fs_fstab
.fs_type
, FSTAB_SW
) ?
134 if ((cp
= strsep(&p
, ":\n")) != NULL
) {
135 _fs_fstab
.fs_freq
= atoi(cp
);
136 if ((cp
= strsep(&p
, ":\n")) != NULL
) {
137 _fs_fstab
.fs_passno
= atoi(cp
);
144 /* OLD_STYLE_FSTAB */
145 while ((cp
= strsep(&p
, " \t\n")) != NULL
&& *cp
== '\0')
147 _fs_fstab
.fs_spec
= cp
;
148 if (!_fs_fstab
.fs_spec
|| *_fs_fstab
.fs_spec
== '#')
150 while ((cp
= strsep(&p
, " \t\n")) != NULL
&& *cp
== '\0')
152 _fs_fstab
.fs_file
= cp
;
154 while ((cp
= strsep(&p
, " \t\n")) != NULL
&& *cp
== '\0')
156 _fs_fstab
.fs_vfstype
= cp
;
157 while ((cp
= strsep(&p
, " \t\n")) != NULL
&& *cp
== '\0')
159 _fs_fstab
.fs_mntops
= cp
;
160 if (_fs_fstab
.fs_mntops
== NULL
)
162 _fs_fstab
.fs_freq
= 0;
163 _fs_fstab
.fs_passno
= 0;
164 while ((cp
= strsep(&p
, " \t\n")) != NULL
&& *cp
== '\0')
167 _fs_fstab
.fs_freq
= atoi(cp
);
168 while ((cp
= strsep(&p
, " \t\n")) != NULL
&& *cp
== '\0')
171 _fs_fstab
.fs_passno
= atoi(cp
);
173 strcpy(subline
, _fs_fstab
.fs_mntops
);
175 for (typexx
= 0, cp
= strsep(&p
, ","); cp
;
176 cp
= strsep(&p
, ",")) {
179 if (!strcmp(cp
, FSTAB_RW
)) {
180 _fs_fstab
.fs_type
= FSTAB_RW
;
183 if (!strcmp(cp
, FSTAB_RQ
)) {
184 _fs_fstab
.fs_type
= FSTAB_RQ
;
187 if (!strcmp(cp
, FSTAB_RO
)) {
188 _fs_fstab
.fs_type
= FSTAB_RO
;
191 if (!strcmp(cp
, FSTAB_SW
)) {
192 _fs_fstab
.fs_type
= FSTAB_SW
;
195 if (!strcmp(cp
, FSTAB_XX
)) {
196 _fs_fstab
.fs_type
= FSTAB_XX
;
206 bad
: /* no way to distinguish between EOF and syntax error */
215 if ((!_fs_fp
&& !setfsent()) || !fstabscan())
221 getfsspec(const char *name
)
225 if (!strcmp(_fs_fstab
.fs_spec
, name
))
231 getfsfile(const char *name
)
235 if (!strcmp(_fs_fstab
.fs_file
, name
))
252 setfstab(getenv("PATH_FSTAB"));
254 if ((_fs_fp
= fopen(path_fstab
, "r")) != NULL
) {
279 _write(STDERR_FILENO
, "fstab: ", 7);
280 _write(STDERR_FILENO
, path_fstab
, strlen(path_fstab
));
281 _write(STDERR_FILENO
, ":", 1);
282 sprintf(num
, "%d: ", LineNo
);
283 _write(STDERR_FILENO
, num
, strlen(num
));
285 _write(STDERR_FILENO
, p
, strlen(p
));
286 _write(STDERR_FILENO
, "\n", 1);