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