Expand PMF_FN_* macros.
[netbsd-mini2440.git] / lib / libc / gen / fstab.c
blob19983a9dd64cb3a4b2a25ffea119304c78e4bf04
1 /* $NetBSD: fstab.c,v 1.27 2005/12/24 21:11:16 perry Exp $ */
3 /*
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: fstab.c,v 1.27 2005/12/24 21:11:16 perry Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include <sys/types.h>
44 #include <assert.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fstab.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
53 #ifdef __weak_alias
54 __weak_alias(endfsent,_endfsent)
55 __weak_alias(getfsent,_getfsent)
56 __weak_alias(getfsfile,_getfsfile)
57 __weak_alias(getfsspec,_getfsspec)
58 __weak_alias(setfsent,_setfsent)
59 #endif
61 static FILE *_fs_fp;
62 static size_t _fs_lineno = 0;
63 static const char *_fs_file = _PATH_FSTAB;
64 static struct fstab _fs_fstab;
66 static int fstabscan __P((void));
68 static char *nextfld(char **, const char *);
69 static int fstabscan(void);
72 static char *
73 nextfld(char **str, const char *sep)
75 char *ret;
77 _DIAGASSERT(str != NULL);
78 _DIAGASSERT(sep != NULL);
80 while ((ret = stresep(str, sep, '\\')) != NULL && *ret == '\0')
81 continue;
82 return ret;
86 static int
87 fstabscan(void)
89 char *cp, *lp, *sp;
90 #define MAXLINELENGTH 1024
91 static char line[MAXLINELENGTH];
92 char subline[MAXLINELENGTH];
93 static const char sep[] = ":\n";
94 static const char ws[] = " \t\n";
95 static const char *fstab_type[] = {
96 FSTAB_RW, FSTAB_RQ, FSTAB_RO, FSTAB_SW, FSTAB_DP, FSTAB_XX, NULL
99 (void)memset(&_fs_fstab, 0, sizeof(_fs_fstab));
100 for (;;) {
101 if (!(lp = fgets(line, sizeof(line), _fs_fp)))
102 return 0;
103 _fs_lineno++;
104 /* OLD_STYLE_FSTAB */
105 if (!strpbrk(lp, " \t")) {
106 _fs_fstab.fs_spec = nextfld(&lp, sep);
107 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
108 continue;
109 _fs_fstab.fs_file = nextfld(&lp, sep);
110 _fs_fstab.fs_type = nextfld(&lp, sep);
111 if (_fs_fstab.fs_type) {
112 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
113 continue;
114 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
115 _fs_fstab.fs_vfstype =
116 __UNCONST(
117 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
118 "ufs" : "swap");
119 if ((cp = nextfld(&lp, sep)) != NULL) {
120 _fs_fstab.fs_freq = atoi(cp);
121 if ((cp = nextfld(&lp, sep)) != NULL) {
122 _fs_fstab.fs_passno = atoi(cp);
123 return 1;
127 goto bad;
129 /* OLD_STYLE_FSTAB */
130 _fs_fstab.fs_spec = nextfld(&lp, ws);
131 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
132 continue;
133 _fs_fstab.fs_file = nextfld(&lp, ws);
134 _fs_fstab.fs_vfstype = nextfld(&lp, ws);
135 _fs_fstab.fs_mntops = nextfld(&lp, ws);
136 if (_fs_fstab.fs_mntops == NULL)
137 goto bad;
138 _fs_fstab.fs_freq = 0;
139 _fs_fstab.fs_passno = 0;
140 if ((cp = nextfld(&lp, ws)) != NULL) {
141 _fs_fstab.fs_freq = atoi(cp);
142 if ((cp = nextfld(&lp, ws)) != NULL)
143 _fs_fstab.fs_passno = atoi(cp);
146 /* subline truncated iff line truncated */
147 (void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline));
148 sp = subline;
150 while ((cp = nextfld(&sp, ",")) != NULL) {
151 const char **tp;
153 if (strlen(cp) != 2)
154 continue;
156 for (tp = fstab_type; *tp; tp++)
157 if (strcmp(cp, *tp) == 0) {
158 _fs_fstab.fs_type = __UNCONST(*tp);
159 break;
161 if (*tp)
162 break;
164 if (_fs_fstab.fs_type == NULL)
165 goto bad;
166 if (strcmp(_fs_fstab.fs_type, FSTAB_XX) == 0)
167 continue;
168 if (cp != NULL)
169 return 1;
171 bad:
172 warnx("%s, %lu: Missing fields", _fs_file, (u_long)_fs_lineno);
174 /* NOTREACHED */
177 struct fstab *
178 getfsent(void)
180 if ((!_fs_fp && !setfsent()) || !fstabscan())
181 return NULL;
182 return &_fs_fstab;
185 struct fstab *
186 getfsspec(const char *name)
189 _DIAGASSERT(name != NULL);
191 if (setfsent())
192 while (fstabscan())
193 if (!strcmp(_fs_fstab.fs_spec, name))
194 return &_fs_fstab;
195 return NULL;
198 struct fstab *
199 getfsfile(const char *name)
202 _DIAGASSERT(name != NULL);
204 if (setfsent())
205 while (fstabscan())
206 if (!strcmp(_fs_fstab.fs_file, name))
207 return &_fs_fstab;
208 return NULL;
212 setfsent(void)
214 _fs_lineno = 0;
215 if (_fs_fp) {
216 rewind(_fs_fp);
217 return 1;
219 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) == NULL) {
220 warn("Cannot open `%s'", _PATH_FSTAB);
221 return 0;
223 return 1;
226 void
227 endfsent(void)
229 if (_fs_fp) {
230 (void)fclose(_fs_fp);
231 _fs_fp = NULL;