Home | History | Annotate | Line # | Download | only in gen
fstab.c revision 1.15
      1 /*	$NetBSD: fstab.c,v 1.15 1998/10/11 19:42:29 kleink 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.15 1998/10/11 19:42:29 kleink Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include "namespace.h"
     46 #include <sys/types.h>
     47 #include <sys/uio.h>
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <fstab.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #ifdef __weak_alias
     57 __weak_alias(endfsent,_endfsent);
     58 __weak_alias(getfsent,_getfsent);
     59 __weak_alias(getfsfile,_getfsfile);
     60 __weak_alias(getfsspec,_getfsspec);
     61 __weak_alias(setfsent,_setfsent);
     62 #endif
     63 
     64 static FILE *_fs_fp;
     65 static struct fstab _fs_fstab;
     66 
     67 static void error __P((int));
     68 static int fstabscan __P((void));
     69 
     70 static int
     71 fstabscan()
     72 {
     73 	char *cp;
     74 #define	MAXLINELENGTH	1024
     75 	static char line[MAXLINELENGTH];
     76 	char subline[MAXLINELENGTH];
     77 	int typexx;
     78 	static const char sep[] = ":\n";
     79 	static const char ws[] = " \t\n";
     80 
     81 	for (;;) {
     82 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
     83 			return(0);
     84 /* OLD_STYLE_FSTAB */
     85 		if (!strpbrk(cp, " \t")) {
     86 			_fs_fstab.fs_spec = strtok(cp, sep);
     87 			if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
     88 				continue;
     89 			_fs_fstab.fs_file = strtok(NULL, sep);
     90 			_fs_fstab.fs_type = strtok(NULL, sep);
     91 			if (_fs_fstab.fs_type) {
     92 				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
     93 					continue;
     94 				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
     95 				_fs_fstab.fs_vfstype =
     96 				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
     97 				    "ufs" : "swap";
     98 				if ((cp = strtok(NULL, sep)) != NULL) {
     99 					_fs_fstab.fs_freq = atoi(cp);
    100 					if ((cp = strtok(NULL, sep)) != NULL) {
    101 						_fs_fstab.fs_passno = atoi(cp);
    102 						return(1);
    103 					}
    104 				}
    105 			}
    106 			goto bad;
    107 		}
    108 /* OLD_STYLE_FSTAB */
    109 		_fs_fstab.fs_spec = strtok(cp, ws);
    110 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
    111 			continue;
    112 		_fs_fstab.fs_file = strtok(NULL, ws);
    113 		_fs_fstab.fs_vfstype = strtok(NULL, ws);
    114 		_fs_fstab.fs_mntops = strtok(NULL, ws);
    115 		if (_fs_fstab.fs_mntops == NULL)
    116 			goto bad;
    117 		_fs_fstab.fs_freq = 0;
    118 		_fs_fstab.fs_passno = 0;
    119 		if ((cp = strtok(NULL, ws)) != NULL) {
    120 			_fs_fstab.fs_freq = atoi(cp);
    121 			if ((cp = strtok(NULL, ws)) != NULL)
    122 				_fs_fstab.fs_passno = atoi(cp);
    123 		}
    124 		(void)strncpy(subline, _fs_fstab.fs_mntops, sizeof(subline)-1);
    125 		for (typexx = 0, cp = strtok(subline, ","); cp;
    126 		     cp = strtok((char *)NULL, ",")) {
    127 			if (strlen(cp) != 2)
    128 				continue;
    129 			if (!strcmp(cp, FSTAB_RW)) {
    130 				_fs_fstab.fs_type = FSTAB_RW;
    131 				break;
    132 			}
    133 			if (!strcmp(cp, FSTAB_RQ)) {
    134 				_fs_fstab.fs_type = FSTAB_RQ;
    135 				break;
    136 			}
    137 			if (!strcmp(cp, FSTAB_RO)) {
    138 				_fs_fstab.fs_type = FSTAB_RO;
    139 				break;
    140 			}
    141 			if (!strcmp(cp, FSTAB_SW)) {
    142 				_fs_fstab.fs_type = FSTAB_SW;
    143 				break;
    144 			}
    145 			if (!strcmp(cp, FSTAB_XX)) {
    146 				_fs_fstab.fs_type = FSTAB_XX;
    147 				typexx++;
    148 				break;
    149 			}
    150 		}
    151 		if (typexx)
    152 			continue;
    153 		if (cp != NULL)
    154 			return(1);
    155 
    156 bad:		/* no way to distinguish between EOF and syntax error */
    157 		error(EFTYPE);
    158 	}
    159 	/* NOTREACHED */
    160 }
    161 
    162 struct fstab *
    163 getfsent()
    164 {
    165 	if ((!_fs_fp && !setfsent()) || !fstabscan())
    166 		return((struct fstab *)NULL);
    167 	return(&_fs_fstab);
    168 }
    169 
    170 struct fstab *
    171 getfsspec(name)
    172 	const char *name;
    173 {
    174 	if (setfsent())
    175 		while (fstabscan())
    176 			if (!strcmp(_fs_fstab.fs_spec, name))
    177 				return(&_fs_fstab);
    178 	return((struct fstab *)NULL);
    179 }
    180 
    181 struct fstab *
    182 getfsfile(name)
    183 	const char *name;
    184 {
    185 	if (setfsent())
    186 		while (fstabscan())
    187 			if (!strcmp(_fs_fstab.fs_file, name))
    188 				return(&_fs_fstab);
    189 	return((struct fstab *)NULL);
    190 }
    191 
    192 int
    193 setfsent()
    194 {
    195 	if (_fs_fp) {
    196 		rewind(_fs_fp);
    197 		return(1);
    198 	}
    199 	if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL)
    200 		return(1);
    201 	error(errno);
    202 	return(0);
    203 }
    204 
    205 void
    206 endfsent()
    207 {
    208 	if (_fs_fp) {
    209 		(void)fclose(_fs_fp);
    210 		_fs_fp = NULL;
    211 	}
    212 }
    213 
    214 static void
    215 error(err)
    216 	int err;
    217 {
    218 
    219 	errno = err;
    220 	warn(_PATH_FSTAB);
    221 }
    222