xref: /csrg-svn/lib/libc/gen/fstab.c (revision 45245)
121341Sdist /*
235370Sbostic  * Copyright (c) 1980, 1988 Regents of the University of California.
335368Sbostic  * All rights reserved.
435368Sbostic  *
542624Sbostic  * %sccs.include.redist.c%
621341Sdist  */
721341Sdist 
826552Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*45245Sbostic static char sccsid[] = "@(#)fstab.c	5.12 (Berkeley) 09/24/90";
1035368Sbostic #endif /* LIBC_SCCS and not lint */
1112597Ssam 
122010Swnj #include <fstab.h>
1338110Sbostic #include <unistd.h>
142010Swnj #include <stdio.h>
152010Swnj 
1636214Sbostic static FILE *_fs_fp;
1736214Sbostic static struct fstab _fs_fstab;
182010Swnj 
1935370Sbostic static
2036214Sbostic fstabscan()
212010Swnj {
2212597Ssam 	register char *cp;
2340563Smckusick #define	MAXLINELENGTH	1024
2436214Sbostic 	static char line[MAXLINELENGTH];
2538671Smckusick 	char subline[MAXLINELENGTH];
2638671Smckusick 	char *fgets(), *strtok();
2738697Smckusick 	int typexx;
2812597Ssam 
2935370Sbostic 	for (;;) {
3036214Sbostic 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
3136214Sbostic 			return(0);
32*45245Sbostic /* OLD_STYLE_FSTAB */
33*45245Sbostic 		if (!strpbrk(cp, " \t")) {
34*45245Sbostic 			_fs_fstab.fs_spec = strtok(cp, ":\n");
35*45245Sbostic 			_fs_fstab.fs_file = strtok((char *)NULL, ":\n");
36*45245Sbostic 			_fs_fstab.fs_type = strtok((char *)NULL, ":\n");
37*45245Sbostic 			if (_fs_fstab.fs_type) {
38*45245Sbostic 				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
39*45245Sbostic 					continue;
40*45245Sbostic 				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
41*45245Sbostic 				_fs_fstab.fs_vfstype =
42*45245Sbostic 				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
43*45245Sbostic 				    "ufs" : "swap";
44*45245Sbostic 				if (cp = strtok((char *)NULL, ":\n")) {
45*45245Sbostic 					_fs_fstab.fs_freq = atoi(cp);
46*45245Sbostic 					if (cp = strtok((char *)NULL, ":\n")) {
47*45245Sbostic 						_fs_fstab.fs_passno = atoi(cp);
48*45245Sbostic 						return(1);
49*45245Sbostic 					}
50*45245Sbostic 				}
51*45245Sbostic 			}
52*45245Sbostic 			goto bad;
53*45245Sbostic 		}
54*45245Sbostic /* OLD_STYLE_FSTAB */
5538671Smckusick 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
5641578Sbostic 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
5741578Sbostic 			continue;
5838671Smckusick 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
5938671Smckusick 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
6038671Smckusick 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
6138671Smckusick 		if (_fs_fstab.fs_mntops == NULL)
6238671Smckusick 			goto bad;
6338671Smckusick 		_fs_fstab.fs_freq = 0;
6438671Smckusick 		_fs_fstab.fs_passno = 0;
6538671Smckusick 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
6638671Smckusick 			_fs_fstab.fs_freq = atoi(cp);
6738671Smckusick 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
6838671Smckusick 				_fs_fstab.fs_passno = atoi(cp);
6938671Smckusick 		}
7038671Smckusick 		strcpy(subline, _fs_fstab.fs_mntops);
7138697Smckusick 		for (typexx = 0, cp = strtok(subline, ","); cp;
7238671Smckusick 		     cp = strtok((char *)NULL, ",")) {
7338671Smckusick 			if (strlen(cp) != 2)
7435370Sbostic 				continue;
7538671Smckusick 			if (!strcmp(cp, FSTAB_RW)) {
7638671Smckusick 				_fs_fstab.fs_type = FSTAB_RW;
7738671Smckusick 				break;
7838110Sbostic 			}
7938671Smckusick 			if (!strcmp(cp, FSTAB_RQ)) {
8038671Smckusick 				_fs_fstab.fs_type = FSTAB_RQ;
8138671Smckusick 				break;
8238671Smckusick 			}
8338671Smckusick 			if (!strcmp(cp, FSTAB_RO)) {
8438671Smckusick 				_fs_fstab.fs_type = FSTAB_RO;
8538671Smckusick 				break;
8638671Smckusick 			}
8738671Smckusick 			if (!strcmp(cp, FSTAB_SW)) {
8838671Smckusick 				_fs_fstab.fs_type = FSTAB_SW;
8938671Smckusick 				break;
9038671Smckusick 			}
9138671Smckusick 			if (!strcmp(cp, FSTAB_XX)) {
9238671Smckusick 				_fs_fstab.fs_type = FSTAB_XX;
9338697Smckusick 				typexx++;
9438671Smckusick 				break;
9538671Smckusick 			}
9635370Sbostic 		}
9738697Smckusick 		if (typexx)
9838697Smckusick 			continue;
9938671Smckusick 		if (cp != NULL)
10038671Smckusick 			return(1);
101*45245Sbostic 
102*45245Sbostic bad:		/* no way to distinguish between EOF and syntax error */
10338110Sbostic 		(void)write(STDERR_FILENO, "fstab: ", 7);
10438110Sbostic 		(void)write(STDERR_FILENO, _PATH_FSTAB,
10538110Sbostic 		    sizeof(_PATH_FSTAB) - 1);
10638110Sbostic 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
1072010Swnj 	}
10835370Sbostic 	/* NOTREACHED */
1092010Swnj }
1102010Swnj 
11112597Ssam struct fstab *
11212597Ssam getfsent()
1132010Swnj {
11436214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
11535370Sbostic 		return((struct fstab *)NULL);
11636214Sbostic 	return(&_fs_fstab);
1172010Swnj }
11812597Ssam 
11912597Ssam struct fstab *
12012597Ssam getfsspec(name)
12135370Sbostic 	register char *name;
1222010Swnj {
12336214Sbostic 	if (setfsent())
12436214Sbostic 		while (fstabscan())
12536214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
12636214Sbostic 				return(&_fs_fstab);
12735370Sbostic 	return((struct fstab *)NULL);
1282010Swnj }
12912597Ssam 
13012597Ssam struct fstab *
13112597Ssam getfsfile(name)
13235370Sbostic 	register char *name;
1332010Swnj {
13436214Sbostic 	if (setfsent())
13536214Sbostic 		while (fstabscan())
13636214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
13736214Sbostic 				return(&_fs_fstab);
13835370Sbostic 	return((struct fstab *)NULL);
1392010Swnj }
14036214Sbostic 
14136214Sbostic setfsent()
14236214Sbostic {
14336214Sbostic 	if (_fs_fp) {
14436214Sbostic 		rewind(_fs_fp);
14536214Sbostic 		return(1);
14636214Sbostic 	}
14738110Sbostic 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
14836214Sbostic }
14936214Sbostic 
15036214Sbostic void
15136214Sbostic endfsent()
15236214Sbostic {
15336214Sbostic 	if (_fs_fp) {
15436214Sbostic 		(void)fclose(_fs_fp);
15536214Sbostic 		_fs_fp = NULL;
15636214Sbostic 	}
15736214Sbostic }
158