xref: /csrg-svn/lib/libc/gen/fstab.c (revision 61111)
121341Sdist /*
2*61111Sbostic  * Copyright (c) 1980, 1988, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
435368Sbostic  *
542624Sbostic  * %sccs.include.redist.c%
621341Sdist  */
721341Sdist 
826552Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)fstab.c	8.1 (Berkeley) 06/04/93";
1035368Sbostic #endif /* LIBC_SCCS and not lint */
1112597Ssam 
1256410Sbostic #include <errno.h>
132010Swnj #include <fstab.h>
142010Swnj #include <stdio.h>
1546597Sdonn #include <stdlib.h>
1645646Sbostic #include <string.h>
1756410Sbostic #include <unistd.h>
182010Swnj 
1936214Sbostic static FILE *_fs_fp;
2036214Sbostic static struct fstab _fs_fstab;
212010Swnj 
2256410Sbostic static error __P((int));
2356410Sbostic static fstabscan __P((void));
2456410Sbostic 
2535370Sbostic static
fstabscan()2636214Sbostic fstabscan()
272010Swnj {
2812597Ssam 	register char *cp;
2940563Smckusick #define	MAXLINELENGTH	1024
3036214Sbostic 	static char line[MAXLINELENGTH];
3138671Smckusick 	char subline[MAXLINELENGTH];
3238697Smckusick 	int typexx;
3312597Ssam 
3435370Sbostic 	for (;;) {
3536214Sbostic 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
3636214Sbostic 			return(0);
3745245Sbostic /* OLD_STYLE_FSTAB */
3845245Sbostic 		if (!strpbrk(cp, " \t")) {
3945245Sbostic 			_fs_fstab.fs_spec = strtok(cp, ":\n");
4045245Sbostic 			_fs_fstab.fs_file = strtok((char *)NULL, ":\n");
4145245Sbostic 			_fs_fstab.fs_type = strtok((char *)NULL, ":\n");
4245245Sbostic 			if (_fs_fstab.fs_type) {
4345245Sbostic 				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
4445245Sbostic 					continue;
4545245Sbostic 				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
4645245Sbostic 				_fs_fstab.fs_vfstype =
4745245Sbostic 				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
4845245Sbostic 				    "ufs" : "swap";
4945245Sbostic 				if (cp = strtok((char *)NULL, ":\n")) {
5045245Sbostic 					_fs_fstab.fs_freq = atoi(cp);
5145245Sbostic 					if (cp = strtok((char *)NULL, ":\n")) {
5245245Sbostic 						_fs_fstab.fs_passno = atoi(cp);
5345245Sbostic 						return(1);
5445245Sbostic 					}
5545245Sbostic 				}
5645245Sbostic 			}
5745245Sbostic 			goto bad;
5845245Sbostic 		}
5945245Sbostic /* OLD_STYLE_FSTAB */
6038671Smckusick 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
6141578Sbostic 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
6241578Sbostic 			continue;
6338671Smckusick 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
6438671Smckusick 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
6538671Smckusick 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
6638671Smckusick 		if (_fs_fstab.fs_mntops == NULL)
6738671Smckusick 			goto bad;
6838671Smckusick 		_fs_fstab.fs_freq = 0;
6938671Smckusick 		_fs_fstab.fs_passno = 0;
7038671Smckusick 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
7138671Smckusick 			_fs_fstab.fs_freq = atoi(cp);
7238671Smckusick 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
7338671Smckusick 				_fs_fstab.fs_passno = atoi(cp);
7438671Smckusick 		}
7538671Smckusick 		strcpy(subline, _fs_fstab.fs_mntops);
7638697Smckusick 		for (typexx = 0, cp = strtok(subline, ","); cp;
7738671Smckusick 		     cp = strtok((char *)NULL, ",")) {
7838671Smckusick 			if (strlen(cp) != 2)
7935370Sbostic 				continue;
8038671Smckusick 			if (!strcmp(cp, FSTAB_RW)) {
8138671Smckusick 				_fs_fstab.fs_type = FSTAB_RW;
8238671Smckusick 				break;
8338110Sbostic 			}
8438671Smckusick 			if (!strcmp(cp, FSTAB_RQ)) {
8538671Smckusick 				_fs_fstab.fs_type = FSTAB_RQ;
8638671Smckusick 				break;
8738671Smckusick 			}
8838671Smckusick 			if (!strcmp(cp, FSTAB_RO)) {
8938671Smckusick 				_fs_fstab.fs_type = FSTAB_RO;
9038671Smckusick 				break;
9138671Smckusick 			}
9238671Smckusick 			if (!strcmp(cp, FSTAB_SW)) {
9338671Smckusick 				_fs_fstab.fs_type = FSTAB_SW;
9438671Smckusick 				break;
9538671Smckusick 			}
9638671Smckusick 			if (!strcmp(cp, FSTAB_XX)) {
9738671Smckusick 				_fs_fstab.fs_type = FSTAB_XX;
9838697Smckusick 				typexx++;
9938671Smckusick 				break;
10038671Smckusick 			}
10135370Sbostic 		}
10238697Smckusick 		if (typexx)
10338697Smckusick 			continue;
10438671Smckusick 		if (cp != NULL)
10538671Smckusick 			return(1);
10645245Sbostic 
10745245Sbostic bad:		/* no way to distinguish between EOF and syntax error */
10846597Sdonn 		error(EFTYPE);
1092010Swnj 	}
11035370Sbostic 	/* NOTREACHED */
1112010Swnj }
1122010Swnj 
11312597Ssam struct fstab *
getfsent()11412597Ssam getfsent()
1152010Swnj {
11636214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
11735370Sbostic 		return((struct fstab *)NULL);
11836214Sbostic 	return(&_fs_fstab);
1192010Swnj }
12012597Ssam 
12112597Ssam struct fstab *
getfsspec(name)12212597Ssam getfsspec(name)
12346597Sdonn 	register const char *name;
1242010Swnj {
12536214Sbostic 	if (setfsent())
12636214Sbostic 		while (fstabscan())
12736214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
12836214Sbostic 				return(&_fs_fstab);
12935370Sbostic 	return((struct fstab *)NULL);
1302010Swnj }
13112597Ssam 
13212597Ssam struct fstab *
getfsfile(name)13312597Ssam getfsfile(name)
13446597Sdonn 	register const char *name;
1352010Swnj {
13636214Sbostic 	if (setfsent())
13736214Sbostic 		while (fstabscan())
13836214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
13936214Sbostic 				return(&_fs_fstab);
14035370Sbostic 	return((struct fstab *)NULL);
1412010Swnj }
14236214Sbostic 
setfsent()14336214Sbostic setfsent()
14436214Sbostic {
14536214Sbostic 	if (_fs_fp) {
14636214Sbostic 		rewind(_fs_fp);
14736214Sbostic 		return(1);
14836214Sbostic 	}
14945646Sbostic 	if (_fs_fp = fopen(_PATH_FSTAB, "r"))
15045646Sbostic 		return(1);
15145646Sbostic 	error(errno);
15245646Sbostic 	return(0);
15336214Sbostic }
15436214Sbostic 
15536214Sbostic void
endfsent()15636214Sbostic endfsent()
15736214Sbostic {
15836214Sbostic 	if (_fs_fp) {
15936214Sbostic 		(void)fclose(_fs_fp);
16036214Sbostic 		_fs_fp = NULL;
16136214Sbostic 	}
16236214Sbostic }
16345646Sbostic 
16445646Sbostic static
error(err)16545646Sbostic error(err)
16645646Sbostic 	int err;
16745646Sbostic {
16845646Sbostic 	char *p;
16945646Sbostic 
17045646Sbostic 	(void)write(STDERR_FILENO, "fstab: ", 7);
17145646Sbostic 	(void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
17257124Sbostic 	(void)write(STDERR_FILENO, ": ", 1);
17345646Sbostic 	p = strerror(err);
17445646Sbostic 	(void)write(STDERR_FILENO, p, strlen(p));
17545646Sbostic 	(void)write(STDERR_FILENO, "\n", 1);
17645646Sbostic }
177