xref: /csrg-svn/lib/libc/gen/fstab.c (revision 36214)
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*36214Sbostic static char sccsid[] = "@(#)fstab.c	5.5 (Berkeley) 11/14/88";
2035368Sbostic #endif /* LIBC_SCCS and not lint */
2112597Ssam 
222010Swnj #include <fstab.h>
232010Swnj #include <stdio.h>
242010Swnj 
25*36214Sbostic static FILE *_fs_fp;
26*36214Sbostic static struct fstab _fs_fstab;
272010Swnj 
2835370Sbostic static
29*36214Sbostic fstabscan()
302010Swnj {
3112597Ssam 	register char *cp;
32*36214Sbostic #define	MAXLINELENGTH	100
33*36214Sbostic 	static char line[MAXLINELENGTH];
34*36214Sbostic 	char *fgets(), *strsep();
3512597Ssam 
3635370Sbostic 	for (;;) {
37*36214Sbostic 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
38*36214Sbostic 			return(0);
39*36214Sbostic 		_fs_fstab.fs_spec = strsep(cp, ":\n");
40*36214Sbostic 		_fs_fstab.fs_file = strsep((char *)NULL, ":\n");
41*36214Sbostic 		_fs_fstab.fs_type = strsep((char *)NULL, ":\n");
42*36214Sbostic 		if (_fs_fstab.fs_type && strcmp(_fs_fstab.fs_type, FSTAB_XX)) {
43*36214Sbostic 			if (!(cp = strsep((char *)NULL, ":\n")))
4435370Sbostic 				continue;
45*36214Sbostic 			_fs_fstab.fs_freq = atoi(cp);
46*36214Sbostic 			if (!(cp = strsep((char *)NULL, ":\n")))
4735370Sbostic 				continue;
48*36214Sbostic 			_fs_fstab.fs_passno = atoi(cp);
49*36214Sbostic 			return(1);
5035370Sbostic 		}
512010Swnj 	}
5235370Sbostic 	/* NOTREACHED */
532010Swnj }
542010Swnj 
5512597Ssam struct fstab *
5612597Ssam getfsent()
572010Swnj {
58*36214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
5935370Sbostic 		return((struct fstab *)NULL);
60*36214Sbostic 	return(&_fs_fstab);
612010Swnj }
6212597Ssam 
6312597Ssam struct fstab *
6412597Ssam getfsspec(name)
6535370Sbostic 	register char *name;
662010Swnj {
67*36214Sbostic 	if (setfsent())
68*36214Sbostic 		while (fstabscan())
69*36214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
70*36214Sbostic 				return(&_fs_fstab);
7135370Sbostic 	return((struct fstab *)NULL);
722010Swnj }
7312597Ssam 
7412597Ssam struct fstab *
7512597Ssam getfsfile(name)
7635370Sbostic 	register char *name;
772010Swnj {
78*36214Sbostic 	if (setfsent())
79*36214Sbostic 		while (fstabscan())
80*36214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
81*36214Sbostic 				return(&_fs_fstab);
8235370Sbostic 	return((struct fstab *)NULL);
832010Swnj }
84*36214Sbostic 
85*36214Sbostic setfsent()
86*36214Sbostic {
87*36214Sbostic 	if (_fs_fp) {
88*36214Sbostic 		rewind(_fs_fp);
89*36214Sbostic 		return(1);
90*36214Sbostic 	}
91*36214Sbostic 	return((_fs_fp = fopen(FSTAB, "r")) != NULL);
92*36214Sbostic }
93*36214Sbostic 
94*36214Sbostic void
95*36214Sbostic endfsent()
96*36214Sbostic {
97*36214Sbostic 	if (_fs_fp) {
98*36214Sbostic 		(void)fclose(_fs_fp);
99*36214Sbostic 		_fs_fp = NULL;
100*36214Sbostic 	}
101*36214Sbostic }
102