xref: /csrg-svn/lib/libc/gen/fstab.c (revision 38110)
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*38110Sbostic static char sccsid[] = "@(#)fstab.c	5.6 (Berkeley) 05/23/89";
2035368Sbostic #endif /* LIBC_SCCS and not lint */
2112597Ssam 
222010Swnj #include <fstab.h>
23*38110Sbostic #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;
3336214Sbostic #define	MAXLINELENGTH	100
3436214Sbostic 	static char line[MAXLINELENGTH];
3536214Sbostic 	char *fgets(), *strsep();
3612597Ssam 
3735370Sbostic 	for (;;) {
3836214Sbostic 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
3936214Sbostic 			return(0);
4036214Sbostic 		_fs_fstab.fs_spec = strsep(cp, ":\n");
4136214Sbostic 		_fs_fstab.fs_file = strsep((char *)NULL, ":\n");
4236214Sbostic 		_fs_fstab.fs_type = strsep((char *)NULL, ":\n");
43*38110Sbostic 		if (_fs_fstab.fs_type) {
44*38110Sbostic 			if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
4535370Sbostic 				continue;
46*38110Sbostic 			if (cp = strsep((char *)NULL, ":\n")) {
47*38110Sbostic 				_fs_fstab.fs_freq = atoi(cp);
48*38110Sbostic 				if (cp = strsep((char *)NULL, ":\n")) {
49*38110Sbostic 					_fs_fstab.fs_passno = atoi(cp);
50*38110Sbostic 					return(1);
51*38110Sbostic 				}
52*38110Sbostic 			}
5335370Sbostic 		}
54*38110Sbostic 		/* no way to distinguish between EOF and syntax error */
55*38110Sbostic 		(void)write(STDERR_FILENO, "fstab: ", 7);
56*38110Sbostic 		(void)write(STDERR_FILENO, _PATH_FSTAB,
57*38110Sbostic 		    sizeof(_PATH_FSTAB) - 1);
58*38110Sbostic 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
592010Swnj 	}
6035370Sbostic 	/* NOTREACHED */
612010Swnj }
622010Swnj 
6312597Ssam struct fstab *
6412597Ssam getfsent()
652010Swnj {
6636214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
6735370Sbostic 		return((struct fstab *)NULL);
6836214Sbostic 	return(&_fs_fstab);
692010Swnj }
7012597Ssam 
7112597Ssam struct fstab *
7212597Ssam getfsspec(name)
7335370Sbostic 	register char *name;
742010Swnj {
7536214Sbostic 	if (setfsent())
7636214Sbostic 		while (fstabscan())
7736214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
7836214Sbostic 				return(&_fs_fstab);
7935370Sbostic 	return((struct fstab *)NULL);
802010Swnj }
8112597Ssam 
8212597Ssam struct fstab *
8312597Ssam getfsfile(name)
8435370Sbostic 	register char *name;
852010Swnj {
8636214Sbostic 	if (setfsent())
8736214Sbostic 		while (fstabscan())
8836214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
8936214Sbostic 				return(&_fs_fstab);
9035370Sbostic 	return((struct fstab *)NULL);
912010Swnj }
9236214Sbostic 
9336214Sbostic setfsent()
9436214Sbostic {
9536214Sbostic 	if (_fs_fp) {
9636214Sbostic 		rewind(_fs_fp);
9736214Sbostic 		return(1);
9836214Sbostic 	}
99*38110Sbostic 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
10036214Sbostic }
10136214Sbostic 
10236214Sbostic void
10336214Sbostic endfsent()
10436214Sbostic {
10536214Sbostic 	if (_fs_fp) {
10636214Sbostic 		(void)fclose(_fs_fp);
10736214Sbostic 		_fs_fp = NULL;
10836214Sbostic 	}
10936214Sbostic }
110