xref: /csrg-svn/lib/libc/gen/fstab.c (revision 41578)
121341Sdist /*
235370Sbostic  * Copyright (c) 1980, 1988 Regents of the University of California.
335368Sbostic  * All rights reserved.
435368Sbostic  *
535368Sbostic  * Redistribution and use in source and binary forms are permitted
635368Sbostic  * provided that the above copyright notice and this paragraph are
735368Sbostic  * duplicated in all such forms and that any documentation,
835368Sbostic  * advertising materials, and other materials related to such
935368Sbostic  * distribution and use acknowledge that the software was developed
1035368Sbostic  * by the University of California, Berkeley.  The name of the
1135368Sbostic  * University may not be used to endorse or promote products derived
1235368Sbostic  * from this software without specific prior written permission.
1335368Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435368Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535368Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621341Sdist  */
1721341Sdist 
1826552Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*41578Sbostic static char sccsid[] = "@(#)fstab.c	5.10 (Berkeley) 05/10/90";
2035368Sbostic #endif /* LIBC_SCCS and not lint */
2112597Ssam 
222010Swnj #include <fstab.h>
2338110Sbostic #include <unistd.h>
242010Swnj #include <stdio.h>
252010Swnj 
2636214Sbostic static FILE *_fs_fp;
2736214Sbostic static struct fstab _fs_fstab;
282010Swnj 
2935370Sbostic static
3036214Sbostic fstabscan()
312010Swnj {
3212597Ssam 	register char *cp;
3340563Smckusick #define	MAXLINELENGTH	1024
3436214Sbostic 	static char line[MAXLINELENGTH];
3538671Smckusick 	char subline[MAXLINELENGTH];
3638671Smckusick 	char *fgets(), *strtok();
3738697Smckusick 	int typexx;
3812597Ssam 
3935370Sbostic 	for (;;) {
4036214Sbostic 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
4136214Sbostic 			return(0);
4238671Smckusick 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
43*41578Sbostic 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
44*41578Sbostic 			continue;
4538671Smckusick 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
4638671Smckusick 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
4738671Smckusick 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
4838671Smckusick 		if (_fs_fstab.fs_mntops == NULL)
4938671Smckusick 			goto bad;
5038671Smckusick 		_fs_fstab.fs_freq = 0;
5138671Smckusick 		_fs_fstab.fs_passno = 0;
5238671Smckusick 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
5338671Smckusick 			_fs_fstab.fs_freq = atoi(cp);
5438671Smckusick 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
5538671Smckusick 				_fs_fstab.fs_passno = atoi(cp);
5638671Smckusick 		}
5738671Smckusick 		strcpy(subline, _fs_fstab.fs_mntops);
5838697Smckusick 		for (typexx = 0, cp = strtok(subline, ","); cp;
5938671Smckusick 		     cp = strtok((char *)NULL, ",")) {
6038671Smckusick 			if (strlen(cp) != 2)
6135370Sbostic 				continue;
6238671Smckusick 			if (!strcmp(cp, FSTAB_RW)) {
6338671Smckusick 				_fs_fstab.fs_type = FSTAB_RW;
6438671Smckusick 				break;
6538110Sbostic 			}
6638671Smckusick 			if (!strcmp(cp, FSTAB_RQ)) {
6738671Smckusick 				_fs_fstab.fs_type = FSTAB_RQ;
6838671Smckusick 				break;
6938671Smckusick 			}
7038671Smckusick 			if (!strcmp(cp, FSTAB_RO)) {
7138671Smckusick 				_fs_fstab.fs_type = FSTAB_RO;
7238671Smckusick 				break;
7338671Smckusick 			}
7438671Smckusick 			if (!strcmp(cp, FSTAB_SW)) {
7538671Smckusick 				_fs_fstab.fs_type = FSTAB_SW;
7638671Smckusick 				break;
7738671Smckusick 			}
7838671Smckusick 			if (!strcmp(cp, FSTAB_XX)) {
7938671Smckusick 				_fs_fstab.fs_type = FSTAB_XX;
8038697Smckusick 				typexx++;
8138671Smckusick 				break;
8238671Smckusick 			}
8335370Sbostic 		}
8438697Smckusick 		if (typexx)
8538697Smckusick 			continue;
8638671Smckusick 		if (cp != NULL)
8738671Smckusick 			return(1);
8838671Smckusick 	bad:
8938110Sbostic 		/* no way to distinguish between EOF and syntax error */
9038110Sbostic 		(void)write(STDERR_FILENO, "fstab: ", 7);
9138110Sbostic 		(void)write(STDERR_FILENO, _PATH_FSTAB,
9238110Sbostic 		    sizeof(_PATH_FSTAB) - 1);
9338110Sbostic 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
942010Swnj 	}
9535370Sbostic 	/* NOTREACHED */
962010Swnj }
972010Swnj 
9812597Ssam struct fstab *
9912597Ssam getfsent()
1002010Swnj {
10136214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
10235370Sbostic 		return((struct fstab *)NULL);
10336214Sbostic 	return(&_fs_fstab);
1042010Swnj }
10512597Ssam 
10612597Ssam struct fstab *
10712597Ssam getfsspec(name)
10835370Sbostic 	register char *name;
1092010Swnj {
11036214Sbostic 	if (setfsent())
11136214Sbostic 		while (fstabscan())
11236214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
11336214Sbostic 				return(&_fs_fstab);
11435370Sbostic 	return((struct fstab *)NULL);
1152010Swnj }
11612597Ssam 
11712597Ssam struct fstab *
11812597Ssam getfsfile(name)
11935370Sbostic 	register char *name;
1202010Swnj {
12136214Sbostic 	if (setfsent())
12236214Sbostic 		while (fstabscan())
12336214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
12436214Sbostic 				return(&_fs_fstab);
12535370Sbostic 	return((struct fstab *)NULL);
1262010Swnj }
12736214Sbostic 
12836214Sbostic setfsent()
12936214Sbostic {
13036214Sbostic 	if (_fs_fp) {
13136214Sbostic 		rewind(_fs_fp);
13236214Sbostic 		return(1);
13336214Sbostic 	}
13438110Sbostic 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
13536214Sbostic }
13636214Sbostic 
13736214Sbostic void
13836214Sbostic endfsent()
13936214Sbostic {
14036214Sbostic 	if (_fs_fp) {
14136214Sbostic 		(void)fclose(_fs_fp);
14236214Sbostic 		_fs_fp = NULL;
14336214Sbostic 	}
14436214Sbostic }
145