xref: /csrg-svn/lib/libc/gen/fstab.c (revision 13198)
112597Ssam #ifndef lint
2*13198Sroot static char sccsid[] = "@(#)fstab.c	4.4 (Berkeley) 06/19/83";
312597Ssam #endif
412597Ssam 
52010Swnj #include <fstab.h>
62010Swnj #include <stdio.h>
72010Swnj #include <ctype.h>
82010Swnj 
912597Ssam static	struct fstab fs;
1012597Ssam static	char line[BUFSIZ+1];
1112597Ssam static	FILE *fs_file = 0;
122010Swnj 
1312597Ssam static char *
1412597Ssam fsskip(p)
1512597Ssam 	register char *p;
162010Swnj {
1712597Ssam 
1812597Ssam 	while (*p && *p != ':')
1912597Ssam 		++p;
2012597Ssam 	if (*p)
2112597Ssam 		*p++ = 0;
2212597Ssam 	return (p);
232010Swnj }
2412597Ssam 
2512597Ssam static char *
2612597Ssam fsdigit(backp, string, end)
2712597Ssam 	int *backp;
2812597Ssam 	char *string, end;
292010Swnj {
3012597Ssam 	register int value = 0;
3112597Ssam 	register char *cp;
3212597Ssam 
3312597Ssam 	for (cp = string; *cp && isdigit(*cp); cp++) {
342010Swnj 		value *= 10;
352010Swnj 		value += *cp - '0';
362010Swnj 	}
3712597Ssam 	if (*cp == '\0')
3812699Ssam 		return ((char *)0);
392010Swnj 	*backp = value;
4012597Ssam 	while (*cp && *cp != end)
412010Swnj 		cp++;
4212597Ssam 	if (*cp == '\0')
4312699Ssam 		return ((char *)0);
4412597Ssam 	return (cp+1);
452010Swnj }
462010Swnj 
4712597Ssam static
4812597Ssam fstabscan(fs)
4912597Ssam 	struct fstab *fs;
502010Swnj {
5112597Ssam 	register char *cp;
5212597Ssam 
5312597Ssam 	cp = fgets(line, 256, fs_file);
5412597Ssam 	if (cp == NULL)
5512597Ssam 		return (EOF);
5612597Ssam 	fs->fs_spec = cp;
5712597Ssam 	cp = fsskip(cp);
5812597Ssam 	fs->fs_file = cp;
5912597Ssam 	cp = fsskip(cp);
6012597Ssam 	fs->fs_type = cp;
6112597Ssam 	cp = fsskip(cp);
6212597Ssam 	cp = fsdigit(&fs->fs_freq, cp, ':');
6312597Ssam 	if (cp == 0)
6412699Ssam 		return (3);
6512597Ssam 	cp = fsdigit(&fs->fs_passno, cp, '\n');
6612597Ssam 	if (cp == 0)
6712699Ssam 		return (4);
6812699Ssam 	return (5);
692010Swnj }
702010Swnj 
7112597Ssam setfsent()
722010Swnj {
7312597Ssam 
742010Swnj 	if (fs_file)
752010Swnj 		endfsent();
76*13198Sroot 	if ((fs_file = fopen(FSTAB, "r")) == NULL) {
772010Swnj 		fs_file = 0;
7812597Ssam 		return (0);
792010Swnj 	}
8012597Ssam 	return (1);
812010Swnj }
822010Swnj 
8312597Ssam endfsent()
842010Swnj {
8512597Ssam 
86*13198Sroot 	if (fs_file) {
872010Swnj 		fclose(fs_file);
88*13198Sroot 		fs_file = 0;
89*13198Sroot 	}
9012597Ssam 	return (1);
912010Swnj }
922010Swnj 
9312597Ssam struct fstab *
9412597Ssam getfsent()
952010Swnj {
9612597Ssam 	int nfields;
972010Swnj 
9812597Ssam 	if ((fs_file == 0) && (setfsent() == 0))
9912699Ssam 		return ((struct fstab *)0);
1002010Swnj 	nfields = fstabscan(&fs);
10112699Ssam 	if (nfields == EOF || nfields != 5)
10212699Ssam 		return ((struct fstab *)0);
10312597Ssam 	return (&fs);
1042010Swnj }
10512597Ssam 
10612597Ssam struct fstab *
10712597Ssam getfsspec(name)
10812597Ssam 	char *name;
1092010Swnj {
11012597Ssam 	register struct fstab *fsp;
11112597Ssam 
1122010Swnj 	if (setfsent() == 0)	/* start from the beginning */
11312699Ssam 		return ((struct fstab *)0);
11412597Ssam 	while((fsp = getfsent()) != 0)
11512597Ssam 		if (strcmp(fsp->fs_spec, name) == 0)
11612597Ssam 			return (fsp);
11712699Ssam 	return ((struct fstab *)0);
1182010Swnj }
11912597Ssam 
12012597Ssam struct fstab *
12112597Ssam getfsfile(name)
12212597Ssam 	char *name;
1232010Swnj {
12412597Ssam 	register struct fstab *fsp;
12512597Ssam 
1262010Swnj 	if (setfsent() == 0)	/* start from the beginning */
12712699Ssam 		return ((struct fstab *)0);
12812597Ssam 	while ((fsp = getfsent()) != 0)
12912597Ssam 		if (strcmp(fsp->fs_file, name) == 0)
13012597Ssam 			return (fsp);
13112699Ssam 	return ((struct fstab *)0);
1322010Swnj }
13312699Ssam 
13412699Ssam struct fstab *
13512699Ssam getfstype(type)
13612699Ssam 	char *type;
13712699Ssam {
13812699Ssam 	register struct fstab *fs;
13912699Ssam 
14012699Ssam 	if (setfsent() == 0)
14112699Ssam 		return ((struct fstab *)0);
14212699Ssam 	while ((fs = getfsent()) != 0)
14312699Ssam 		if (strcmp(fs->fs_type, type) == 0)
14412699Ssam 			return (fs);
14512699Ssam 	return ((struct fstab *)0);
14612699Ssam }
147