Annotation of src/lib/libc/gen/fstab.c, Revision 1.31
1.31 ! christos 1: /* $NetBSD: fstab.c,v 1.30 2012/02/12 13:52:51 wiz Exp $ */
1.6 cgd 2:
1.1 cgd 3: /*
1.6 cgd 4: * Copyright (c) 1980, 1988, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
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.
1.25 agc 15: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
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.
30: */
31:
1.10 christos 32: #include <sys/cdefs.h>
1.1 cgd 33: #if defined(LIBC_SCCS) && !defined(lint)
1.6 cgd 34: #if 0
35: static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93";
36: #else
1.31 ! christos 37: __RCSID("$NetBSD: fstab.c,v 1.30 2012/02/12 13:52:51 wiz Exp $");
1.6 cgd 38: #endif
1.1 cgd 39: #endif /* LIBC_SCCS and not lint */
40:
1.15 kleink 41: #include "namespace.h"
1.7 cgd 42: #include <sys/types.h>
1.20 lukem 43:
44: #include <assert.h>
1.14 mycroft 45: #include <err.h>
1.6 cgd 46: #include <errno.h>
1.1 cgd 47: #include <fstab.h>
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <string.h>
1.6 cgd 51: #include <unistd.h>
1.11 jtc 52:
53: #ifdef __weak_alias
1.22 mycroft 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)
1.11 jtc 59: #endif
1.1 cgd 60:
61: static FILE *_fs_fp;
1.17 christos 62: static size_t _fs_lineno = 0;
63: static const char *_fs_file = _PATH_FSTAB;
1.1 cgd 64: static struct fstab _fs_fstab;
1.6 cgd 65:
1.28 christos 66: static char *nextfld(char **, const char *);
67: static int fstabscan(void);
1.17 christos 68:
69:
1.28 christos 70: static char *
71: nextfld(char **str, const char *sep)
1.17 christos 72: {
73: char *ret;
1.20 lukem 74:
75: _DIAGASSERT(str != NULL);
76: _DIAGASSERT(sep != NULL);
77:
1.28 christos 78: while ((ret = stresep(str, sep, '\\')) != NULL && *ret == '\0')
1.17 christos 79: continue;
80: return ret;
81: }
82:
83:
1.8 jtc 84: static int
1.28 christos 85: fstabscan(void)
1.1 cgd 86: {
1.17 christos 87: char *cp, *lp, *sp;
1.1 cgd 88: #define MAXLINELENGTH 1024
89: static char line[MAXLINELENGTH];
90: char subline[MAXLINELENGTH];
1.10 christos 91: static const char sep[] = ":\n";
92: static const char ws[] = " \t\n";
1.26 christos 93: static const char *fstab_type[] = {
1.19 mrg 94: FSTAB_RW, FSTAB_RQ, FSTAB_RO, FSTAB_SW, FSTAB_DP, FSTAB_XX, NULL
1.17 christos 95: };
1.1 cgd 96:
1.23 lukem 97: (void)memset(&_fs_fstab, 0, sizeof(_fs_fstab));
1.1 cgd 98: for (;;) {
1.31 ! christos 99: if (!(lp = fgets(line, (int)sizeof(line), _fs_fp)))
1.17 christos 100: return 0;
101: _fs_lineno++;
1.1 cgd 102: /* OLD_STYLE_FSTAB */
1.17 christos 103: if (!strpbrk(lp, " \t")) {
104: _fs_fstab.fs_spec = nextfld(&lp, sep);
1.4 pk 105: if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
106: continue;
1.17 christos 107: _fs_fstab.fs_file = nextfld(&lp, sep);
108: _fs_fstab.fs_type = nextfld(&lp, sep);
1.1 cgd 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 =
1.26 christos 114: __UNCONST(
1.1 cgd 115: strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
1.26 christos 116: "ufs" : "swap");
1.17 christos 117: if ((cp = nextfld(&lp, sep)) != NULL) {
1.1 cgd 118: _fs_fstab.fs_freq = atoi(cp);
1.17 christos 119: if ((cp = nextfld(&lp, sep)) != NULL) {
1.1 cgd 120: _fs_fstab.fs_passno = atoi(cp);
1.17 christos 121: return 1;
1.1 cgd 122: }
123: }
124: }
125: goto bad;
126: }
127: /* OLD_STYLE_FSTAB */
1.17 christos 128: _fs_fstab.fs_spec = nextfld(&lp, ws);
1.1 cgd 129: if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
130: continue;
1.17 christos 131: _fs_fstab.fs_file = nextfld(&lp, ws);
132: _fs_fstab.fs_vfstype = nextfld(&lp, ws);
133: _fs_fstab.fs_mntops = nextfld(&lp, ws);
1.1 cgd 134: if (_fs_fstab.fs_mntops == NULL)
135: goto bad;
136: _fs_fstab.fs_freq = 0;
137: _fs_fstab.fs_passno = 0;
1.17 christos 138: if ((cp = nextfld(&lp, ws)) != NULL) {
1.1 cgd 139: _fs_fstab.fs_freq = atoi(cp);
1.17 christos 140: if ((cp = nextfld(&lp, ws)) != NULL)
1.1 cgd 141: _fs_fstab.fs_passno = atoi(cp);
142: }
1.24 groo 143:
144: /* subline truncated iff line truncated */
145: (void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline));
146: sp = subline;
147:
1.17 christos 148: while ((cp = nextfld(&sp, ",")) != NULL) {
1.26 christos 149: const char **tp;
1.17 christos 150:
1.1 cgd 151: if (strlen(cp) != 2)
152: continue;
1.17 christos 153:
154: for (tp = fstab_type; *tp; tp++)
155: if (strcmp(cp, *tp) == 0) {
1.26 christos 156: _fs_fstab.fs_type = __UNCONST(*tp);
1.17 christos 157: break;
158: }
159: if (*tp)
1.1 cgd 160: break;
161: }
1.23 lukem 162: if (_fs_fstab.fs_type == NULL)
163: goto bad;
1.17 christos 164: if (strcmp(_fs_fstab.fs_type, FSTAB_XX) == 0)
1.1 cgd 165: continue;
166: if (cp != NULL)
1.17 christos 167: return 1;
1.1 cgd 168:
1.17 christos 169: bad:
1.18 thorpej 170: warnx("%s, %lu: Missing fields", _fs_file, (u_long)_fs_lineno);
1.1 cgd 171: }
172: /* NOTREACHED */
173: }
174:
175: struct fstab *
1.28 christos 176: getfsent(void)
1.1 cgd 177: {
1.10 christos 178: if ((!_fs_fp && !setfsent()) || !fstabscan())
1.17 christos 179: return NULL;
180: return &_fs_fstab;
1.1 cgd 181: }
182:
183: struct fstab *
1.28 christos 184: getfsspec(const char *name)
1.1 cgd 185: {
1.20 lukem 186:
187: _DIAGASSERT(name != NULL);
188:
1.1 cgd 189: if (setfsent())
190: while (fstabscan())
191: if (!strcmp(_fs_fstab.fs_spec, name))
1.17 christos 192: return &_fs_fstab;
193: return NULL;
1.1 cgd 194: }
195:
196: struct fstab *
1.28 christos 197: getfsfile(const char *name)
1.1 cgd 198: {
1.20 lukem 199:
200: _DIAGASSERT(name != NULL);
201:
1.1 cgd 202: if (setfsent())
203: while (fstabscan())
204: if (!strcmp(_fs_fstab.fs_file, name))
1.17 christos 205: return &_fs_fstab;
206: return NULL;
1.1 cgd 207: }
208:
1.8 jtc 209: int
1.28 christos 210: setfsent(void)
1.1 cgd 211: {
1.17 christos 212: _fs_lineno = 0;
1.1 cgd 213: if (_fs_fp) {
214: rewind(_fs_fp);
1.17 christos 215: return 1;
1.1 cgd 216: }
1.29 christos 217: if ((_fs_fp = fopen(_PATH_FSTAB, "re")) == NULL) {
1.17 christos 218: warn("Cannot open `%s'", _PATH_FSTAB);
219: return 0;
220: }
221: return 1;
1.1 cgd 222: }
223:
224: void
1.28 christos 225: endfsent(void)
1.1 cgd 226: {
227: if (_fs_fp) {
228: (void)fclose(_fs_fp);
229: _fs_fp = NULL;
230: }
231: }
CVSweb <webmaster@jp.NetBSD.org>