xref: /csrg-svn/lib/libc/gen/fstab.c (revision 42624)
121341Sdist /*
235370Sbostic  * Copyright (c) 1980, 1988 Regents of the University of California.
335368Sbostic  * All rights reserved.
435368Sbostic  *
5*42624Sbostic  * %sccs.include.redist.c%
621341Sdist  */
721341Sdist 
826552Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42624Sbostic static char sccsid[] = "@(#)fstab.c	5.11 (Berkeley) 06/01/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);
3238671Smckusick 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
3341578Sbostic 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
3441578Sbostic 			continue;
3538671Smckusick 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
3638671Smckusick 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
3738671Smckusick 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
3838671Smckusick 		if (_fs_fstab.fs_mntops == NULL)
3938671Smckusick 			goto bad;
4038671Smckusick 		_fs_fstab.fs_freq = 0;
4138671Smckusick 		_fs_fstab.fs_passno = 0;
4238671Smckusick 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
4338671Smckusick 			_fs_fstab.fs_freq = atoi(cp);
4438671Smckusick 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
4538671Smckusick 				_fs_fstab.fs_passno = atoi(cp);
4638671Smckusick 		}
4738671Smckusick 		strcpy(subline, _fs_fstab.fs_mntops);
4838697Smckusick 		for (typexx = 0, cp = strtok(subline, ","); cp;
4938671Smckusick 		     cp = strtok((char *)NULL, ",")) {
5038671Smckusick 			if (strlen(cp) != 2)
5135370Sbostic 				continue;
5238671Smckusick 			if (!strcmp(cp, FSTAB_RW)) {
5338671Smckusick 				_fs_fstab.fs_type = FSTAB_RW;
5438671Smckusick 				break;
5538110Sbostic 			}
5638671Smckusick 			if (!strcmp(cp, FSTAB_RQ)) {
5738671Smckusick 				_fs_fstab.fs_type = FSTAB_RQ;
5838671Smckusick 				break;
5938671Smckusick 			}
6038671Smckusick 			if (!strcmp(cp, FSTAB_RO)) {
6138671Smckusick 				_fs_fstab.fs_type = FSTAB_RO;
6238671Smckusick 				break;
6338671Smckusick 			}
6438671Smckusick 			if (!strcmp(cp, FSTAB_SW)) {
6538671Smckusick 				_fs_fstab.fs_type = FSTAB_SW;
6638671Smckusick 				break;
6738671Smckusick 			}
6838671Smckusick 			if (!strcmp(cp, FSTAB_XX)) {
6938671Smckusick 				_fs_fstab.fs_type = FSTAB_XX;
7038697Smckusick 				typexx++;
7138671Smckusick 				break;
7238671Smckusick 			}
7335370Sbostic 		}
7438697Smckusick 		if (typexx)
7538697Smckusick 			continue;
7638671Smckusick 		if (cp != NULL)
7738671Smckusick 			return(1);
7838671Smckusick 	bad:
7938110Sbostic 		/* no way to distinguish between EOF and syntax error */
8038110Sbostic 		(void)write(STDERR_FILENO, "fstab: ", 7);
8138110Sbostic 		(void)write(STDERR_FILENO, _PATH_FSTAB,
8238110Sbostic 		    sizeof(_PATH_FSTAB) - 1);
8338110Sbostic 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
842010Swnj 	}
8535370Sbostic 	/* NOTREACHED */
862010Swnj }
872010Swnj 
8812597Ssam struct fstab *
8912597Ssam getfsent()
902010Swnj {
9136214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
9235370Sbostic 		return((struct fstab *)NULL);
9336214Sbostic 	return(&_fs_fstab);
942010Swnj }
9512597Ssam 
9612597Ssam struct fstab *
9712597Ssam getfsspec(name)
9835370Sbostic 	register char *name;
992010Swnj {
10036214Sbostic 	if (setfsent())
10136214Sbostic 		while (fstabscan())
10236214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
10336214Sbostic 				return(&_fs_fstab);
10435370Sbostic 	return((struct fstab *)NULL);
1052010Swnj }
10612597Ssam 
10712597Ssam struct fstab *
10812597Ssam getfsfile(name)
10935370Sbostic 	register char *name;
1102010Swnj {
11136214Sbostic 	if (setfsent())
11236214Sbostic 		while (fstabscan())
11336214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
11436214Sbostic 				return(&_fs_fstab);
11535370Sbostic 	return((struct fstab *)NULL);
1162010Swnj }
11736214Sbostic 
11836214Sbostic setfsent()
11936214Sbostic {
12036214Sbostic 	if (_fs_fp) {
12136214Sbostic 		rewind(_fs_fp);
12236214Sbostic 		return(1);
12336214Sbostic 	}
12438110Sbostic 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
12536214Sbostic }
12636214Sbostic 
12736214Sbostic void
12836214Sbostic endfsent()
12936214Sbostic {
13036214Sbostic 	if (_fs_fp) {
13136214Sbostic 		(void)fclose(_fs_fp);
13236214Sbostic 		_fs_fp = NULL;
13336214Sbostic 	}
13436214Sbostic }
135