xref: /csrg-svn/lib/libc/gen/fstab.c (revision 35370)
121341Sdist /*
2*35370Sbostic  * 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*35370Sbostic static char sccsid[] = "@(#)fstab.c	5.4 (Berkeley) 08/15/88";
2035368Sbostic #endif /* LIBC_SCCS and not lint */
2112597Ssam 
222010Swnj #include <fstab.h>
232010Swnj #include <stdio.h>
242010Swnj 
2512597Ssam static	struct fstab fs;
26*35370Sbostic static	FILE *fs_file = NULL;
2712597Ssam static	char line[BUFSIZ+1];
282010Swnj 
29*35370Sbostic static
30*35370Sbostic fstabscan(fsp)
31*35370Sbostic 	register struct fstab *fsp;
322010Swnj {
3312597Ssam 	register char *cp;
34*35370Sbostic 	char *fgets(), *strtok();
3512597Ssam 
36*35370Sbostic 	for (;;) {
37*35370Sbostic 		if (!(cp = fgets(line, sizeof(line), fs_file)))
38*35370Sbostic 			return(1);
39*35370Sbostic 		fsp->fs_spec = strtok(cp, ":\n");
40*35370Sbostic 		fsp->fs_file = strtok((char *)NULL, ":\n");
41*35370Sbostic 		fsp->fs_type = strtok((char *)NULL, ":\n");
42*35370Sbostic 		if (fsp->fs_type && strcmp(fsp->fs_type, FSTAB_XX)) {
43*35370Sbostic 			if (!(cp = strtok((char *)NULL, ":\n")))
44*35370Sbostic 				continue;
45*35370Sbostic 			fsp->fs_freq = atoi(cp);
46*35370Sbostic 			if (!(cp = strtok((char *)NULL, ":\n")))
47*35370Sbostic 				continue;
48*35370Sbostic 			fsp->fs_passno = atoi(cp);
49*35370Sbostic 			return(0);
50*35370Sbostic 		}
512010Swnj 	}
52*35370Sbostic 	/* NOTREACHED */
532010Swnj }
542010Swnj 
5512597Ssam setfsent()
562010Swnj {
572010Swnj 	if (fs_file)
58*35370Sbostic 		(void)endfsent();
5913198Sroot 	if ((fs_file = fopen(FSTAB, "r")) == NULL) {
60*35370Sbostic 		fs_file = NULL;
61*35370Sbostic 		return(0);
622010Swnj 	}
63*35370Sbostic 	return(1);
642010Swnj }
652010Swnj 
6612597Ssam endfsent()
672010Swnj {
6813198Sroot 	if (fs_file) {
69*35370Sbostic 		(void)fclose(fs_file);
70*35370Sbostic 		fs_file = NULL;
7113198Sroot 	}
72*35370Sbostic 	return(1);
732010Swnj }
742010Swnj 
7512597Ssam struct fstab *
7612597Ssam getfsent()
772010Swnj {
78*35370Sbostic 	if (fs_file == NULL && !setfsent() || fstabscan(&fs))
79*35370Sbostic 		return((struct fstab *)NULL);
80*35370Sbostic 	return(&fs);
812010Swnj }
8212597Ssam 
8312597Ssam struct fstab *
8412597Ssam getfsspec(name)
85*35370Sbostic 	register char *name;
862010Swnj {
8712597Ssam 	register struct fstab *fsp;
8812597Ssam 
89*35370Sbostic 	if (!setfsent())		/* start from the beginning */
90*35370Sbostic 		return((struct fstab *)NULL);
91*35370Sbostic 	while (fsp = getfsent())
92*35370Sbostic 		if (!strcmp(fsp->fs_spec, name))
93*35370Sbostic 			return(fsp);
94*35370Sbostic 	return((struct fstab *)NULL);
952010Swnj }
9612597Ssam 
9712597Ssam struct fstab *
9812597Ssam getfsfile(name)
99*35370Sbostic 	register char *name;
1002010Swnj {
10112597Ssam 	register struct fstab *fsp;
10212597Ssam 
103*35370Sbostic 	if (!setfsent())		/* start from the beginning */
104*35370Sbostic 		return((struct fstab *)NULL);
105*35370Sbostic 	while (fsp = getfsent())
106*35370Sbostic 		if (!strcmp(fsp->fs_file, name))
107*35370Sbostic 			return(fsp);
108*35370Sbostic 	return((struct fstab *)NULL);
1092010Swnj }
110