xref: /csrg-svn/lib/libc/gen/fstab.c (revision 40563)
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*40563Smckusick static char sccsid[] = "@(#)fstab.c	5.9 (Berkeley) 03/22/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;
33*40563Smckusick #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");
4338671Smckusick 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
4438671Smckusick 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
4538671Smckusick 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
4638671Smckusick 		if (_fs_fstab.fs_mntops == NULL)
4738671Smckusick 			goto bad;
4838671Smckusick 		_fs_fstab.fs_freq = 0;
4938671Smckusick 		_fs_fstab.fs_passno = 0;
5038671Smckusick 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
5138671Smckusick 			_fs_fstab.fs_freq = atoi(cp);
5238671Smckusick 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
5338671Smckusick 				_fs_fstab.fs_passno = atoi(cp);
5438671Smckusick 		}
5538671Smckusick 		strcpy(subline, _fs_fstab.fs_mntops);
5638697Smckusick 		for (typexx = 0, cp = strtok(subline, ","); cp;
5738671Smckusick 		     cp = strtok((char *)NULL, ",")) {
5838671Smckusick 			if (strlen(cp) != 2)
5935370Sbostic 				continue;
6038671Smckusick 			if (!strcmp(cp, FSTAB_RW)) {
6138671Smckusick 				_fs_fstab.fs_type = FSTAB_RW;
6238671Smckusick 				break;
6338110Sbostic 			}
6438671Smckusick 			if (!strcmp(cp, FSTAB_RQ)) {
6538671Smckusick 				_fs_fstab.fs_type = FSTAB_RQ;
6638671Smckusick 				break;
6738671Smckusick 			}
6838671Smckusick 			if (!strcmp(cp, FSTAB_RO)) {
6938671Smckusick 				_fs_fstab.fs_type = FSTAB_RO;
7038671Smckusick 				break;
7138671Smckusick 			}
7238671Smckusick 			if (!strcmp(cp, FSTAB_SW)) {
7338671Smckusick 				_fs_fstab.fs_type = FSTAB_SW;
7438671Smckusick 				break;
7538671Smckusick 			}
7638671Smckusick 			if (!strcmp(cp, FSTAB_XX)) {
7738671Smckusick 				_fs_fstab.fs_type = FSTAB_XX;
7838697Smckusick 				typexx++;
7938671Smckusick 				break;
8038671Smckusick 			}
8135370Sbostic 		}
8238697Smckusick 		if (typexx)
8338697Smckusick 			continue;
8438671Smckusick 		if (cp != NULL)
8538671Smckusick 			return(1);
8638671Smckusick 	bad:
8738110Sbostic 		/* no way to distinguish between EOF and syntax error */
8838110Sbostic 		(void)write(STDERR_FILENO, "fstab: ", 7);
8938110Sbostic 		(void)write(STDERR_FILENO, _PATH_FSTAB,
9038110Sbostic 		    sizeof(_PATH_FSTAB) - 1);
9138110Sbostic 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
922010Swnj 	}
9335370Sbostic 	/* NOTREACHED */
942010Swnj }
952010Swnj 
9612597Ssam struct fstab *
9712597Ssam getfsent()
982010Swnj {
9936214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
10035370Sbostic 		return((struct fstab *)NULL);
10136214Sbostic 	return(&_fs_fstab);
1022010Swnj }
10312597Ssam 
10412597Ssam struct fstab *
10512597Ssam getfsspec(name)
10635370Sbostic 	register char *name;
1072010Swnj {
10836214Sbostic 	if (setfsent())
10936214Sbostic 		while (fstabscan())
11036214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
11136214Sbostic 				return(&_fs_fstab);
11235370Sbostic 	return((struct fstab *)NULL);
1132010Swnj }
11412597Ssam 
11512597Ssam struct fstab *
11612597Ssam getfsfile(name)
11735370Sbostic 	register char *name;
1182010Swnj {
11936214Sbostic 	if (setfsent())
12036214Sbostic 		while (fstabscan())
12136214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
12236214Sbostic 				return(&_fs_fstab);
12335370Sbostic 	return((struct fstab *)NULL);
1242010Swnj }
12536214Sbostic 
12636214Sbostic setfsent()
12736214Sbostic {
12836214Sbostic 	if (_fs_fp) {
12936214Sbostic 		rewind(_fs_fp);
13036214Sbostic 		return(1);
13136214Sbostic 	}
13238110Sbostic 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
13336214Sbostic }
13436214Sbostic 
13536214Sbostic void
13636214Sbostic endfsent()
13736214Sbostic {
13836214Sbostic 	if (_fs_fp) {
13936214Sbostic 		(void)fclose(_fs_fp);
14036214Sbostic 		_fs_fp = NULL;
14136214Sbostic 	}
14236214Sbostic }
143