xref: /csrg-svn/lib/libc/gen/fstab.c (revision 35368)
121341Sdist /*
221341Sdist  * Copyright (c) 1980 Regents of the University of California.
3*35368Sbostic  * All rights reserved.
4*35368Sbostic  *
5*35368Sbostic  * Redistribution and use in source and binary forms are permitted
6*35368Sbostic  * provided that the above copyright notice and this paragraph are
7*35368Sbostic  * duplicated in all such forms and that any documentation,
8*35368Sbostic  * advertising materials, and other materials related to such
9*35368Sbostic  * distribution and use acknowledge that the software was developed
10*35368Sbostic  * by the University of California, Berkeley.  The name of the
11*35368Sbostic  * University may not be used to endorse or promote products derived
12*35368Sbostic  * from this software without specific prior written permission.
13*35368Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35368Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35368Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621341Sdist  */
1721341Sdist 
1826552Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*35368Sbostic static char sccsid[] = "@(#)fstab.c	5.3 (Berkeley) 08/13/88";
20*35368Sbostic #endif /* LIBC_SCCS and not lint */
2112597Ssam 
222010Swnj #include <fstab.h>
232010Swnj #include <stdio.h>
242010Swnj #include <ctype.h>
252010Swnj 
2612597Ssam static	struct fstab fs;
2712597Ssam static	char line[BUFSIZ+1];
2812597Ssam static	FILE *fs_file = 0;
292010Swnj 
3012597Ssam static char *
3112597Ssam fsskip(p)
3212597Ssam 	register char *p;
332010Swnj {
3412597Ssam 
3512597Ssam 	while (*p && *p != ':')
3612597Ssam 		++p;
3712597Ssam 	if (*p)
3812597Ssam 		*p++ = 0;
3912597Ssam 	return (p);
402010Swnj }
4112597Ssam 
4212597Ssam static char *
4312597Ssam fsdigit(backp, string, end)
4412597Ssam 	int *backp;
4512597Ssam 	char *string, end;
462010Swnj {
4712597Ssam 	register int value = 0;
4812597Ssam 	register char *cp;
4912597Ssam 
5012597Ssam 	for (cp = string; *cp && isdigit(*cp); cp++) {
512010Swnj 		value *= 10;
522010Swnj 		value += *cp - '0';
532010Swnj 	}
5412597Ssam 	if (*cp == '\0')
5512699Ssam 		return ((char *)0);
562010Swnj 	*backp = value;
5712597Ssam 	while (*cp && *cp != end)
582010Swnj 		cp++;
5912597Ssam 	if (*cp == '\0')
6012699Ssam 		return ((char *)0);
6112597Ssam 	return (cp+1);
622010Swnj }
632010Swnj 
6412597Ssam static
6512597Ssam fstabscan(fs)
6612597Ssam 	struct fstab *fs;
672010Swnj {
6812597Ssam 	register char *cp;
6912597Ssam 
7012597Ssam 	cp = fgets(line, 256, fs_file);
7112597Ssam 	if (cp == NULL)
7212597Ssam 		return (EOF);
7312597Ssam 	fs->fs_spec = cp;
7412597Ssam 	cp = fsskip(cp);
7512597Ssam 	fs->fs_file = cp;
7612597Ssam 	cp = fsskip(cp);
7712597Ssam 	fs->fs_type = cp;
7812597Ssam 	cp = fsskip(cp);
7912597Ssam 	cp = fsdigit(&fs->fs_freq, cp, ':');
8012597Ssam 	if (cp == 0)
8112699Ssam 		return (3);
8212597Ssam 	cp = fsdigit(&fs->fs_passno, cp, '\n');
8312597Ssam 	if (cp == 0)
8412699Ssam 		return (4);
8512699Ssam 	return (5);
862010Swnj }
872010Swnj 
8812597Ssam setfsent()
892010Swnj {
9012597Ssam 
912010Swnj 	if (fs_file)
922010Swnj 		endfsent();
9313198Sroot 	if ((fs_file = fopen(FSTAB, "r")) == NULL) {
942010Swnj 		fs_file = 0;
9512597Ssam 		return (0);
962010Swnj 	}
9712597Ssam 	return (1);
982010Swnj }
992010Swnj 
10012597Ssam endfsent()
1012010Swnj {
10212597Ssam 
10313198Sroot 	if (fs_file) {
1042010Swnj 		fclose(fs_file);
10513198Sroot 		fs_file = 0;
10613198Sroot 	}
10712597Ssam 	return (1);
1082010Swnj }
1092010Swnj 
11012597Ssam struct fstab *
11112597Ssam getfsent()
1122010Swnj {
11312597Ssam 	int nfields;
1142010Swnj 
11512597Ssam 	if ((fs_file == 0) && (setfsent() == 0))
11612699Ssam 		return ((struct fstab *)0);
1172010Swnj 	nfields = fstabscan(&fs);
11812699Ssam 	if (nfields == EOF || nfields != 5)
11912699Ssam 		return ((struct fstab *)0);
12012597Ssam 	return (&fs);
1212010Swnj }
12212597Ssam 
12312597Ssam struct fstab *
12412597Ssam getfsspec(name)
12512597Ssam 	char *name;
1262010Swnj {
12712597Ssam 	register struct fstab *fsp;
12812597Ssam 
1292010Swnj 	if (setfsent() == 0)	/* start from the beginning */
13012699Ssam 		return ((struct fstab *)0);
13112597Ssam 	while((fsp = getfsent()) != 0)
13212597Ssam 		if (strcmp(fsp->fs_spec, name) == 0)
13312597Ssam 			return (fsp);
13412699Ssam 	return ((struct fstab *)0);
1352010Swnj }
13612597Ssam 
13712597Ssam struct fstab *
13812597Ssam getfsfile(name)
13912597Ssam 	char *name;
1402010Swnj {
14112597Ssam 	register struct fstab *fsp;
14212597Ssam 
1432010Swnj 	if (setfsent() == 0)	/* start from the beginning */
14412699Ssam 		return ((struct fstab *)0);
14512597Ssam 	while ((fsp = getfsent()) != 0)
14612597Ssam 		if (strcmp(fsp->fs_file, name) == 0)
14712597Ssam 			return (fsp);
14812699Ssam 	return ((struct fstab *)0);
1492010Swnj }
15012699Ssam 
15112699Ssam struct fstab *
15212699Ssam getfstype(type)
15312699Ssam 	char *type;
15412699Ssam {
15512699Ssam 	register struct fstab *fs;
15612699Ssam 
15712699Ssam 	if (setfsent() == 0)
15812699Ssam 		return ((struct fstab *)0);
15912699Ssam 	while ((fs = getfsent()) != 0)
16012699Ssam 		if (strcmp(fs->fs_type, type) == 0)
16112699Ssam 			return (fs);
16212699Ssam 	return ((struct fstab *)0);
16312699Ssam }
164