xref: /csrg-svn/lib/libc/gen/fstab.c (revision 26552)
121341Sdist /*
221341Sdist  * Copyright (c) 1980 Regents of the University of California.
321341Sdist  * All rights reserved.  The Berkeley software License Agreement
421341Sdist  * specifies the terms and conditions for redistribution.
521341Sdist  */
621341Sdist 
7*26552Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*26552Sdonn static char sccsid[] = "@(#)fstab.c	5.2 (Berkeley) 03/09/86";
9*26552Sdonn #endif LIBC_SCCS and not lint
1012597Ssam 
112010Swnj #include <fstab.h>
122010Swnj #include <stdio.h>
132010Swnj #include <ctype.h>
142010Swnj 
1512597Ssam static	struct fstab fs;
1612597Ssam static	char line[BUFSIZ+1];
1712597Ssam static	FILE *fs_file = 0;
182010Swnj 
1912597Ssam static char *
2012597Ssam fsskip(p)
2112597Ssam 	register char *p;
222010Swnj {
2312597Ssam 
2412597Ssam 	while (*p && *p != ':')
2512597Ssam 		++p;
2612597Ssam 	if (*p)
2712597Ssam 		*p++ = 0;
2812597Ssam 	return (p);
292010Swnj }
3012597Ssam 
3112597Ssam static char *
3212597Ssam fsdigit(backp, string, end)
3312597Ssam 	int *backp;
3412597Ssam 	char *string, end;
352010Swnj {
3612597Ssam 	register int value = 0;
3712597Ssam 	register char *cp;
3812597Ssam 
3912597Ssam 	for (cp = string; *cp && isdigit(*cp); cp++) {
402010Swnj 		value *= 10;
412010Swnj 		value += *cp - '0';
422010Swnj 	}
4312597Ssam 	if (*cp == '\0')
4412699Ssam 		return ((char *)0);
452010Swnj 	*backp = value;
4612597Ssam 	while (*cp && *cp != end)
472010Swnj 		cp++;
4812597Ssam 	if (*cp == '\0')
4912699Ssam 		return ((char *)0);
5012597Ssam 	return (cp+1);
512010Swnj }
522010Swnj 
5312597Ssam static
5412597Ssam fstabscan(fs)
5512597Ssam 	struct fstab *fs;
562010Swnj {
5712597Ssam 	register char *cp;
5812597Ssam 
5912597Ssam 	cp = fgets(line, 256, fs_file);
6012597Ssam 	if (cp == NULL)
6112597Ssam 		return (EOF);
6212597Ssam 	fs->fs_spec = cp;
6312597Ssam 	cp = fsskip(cp);
6412597Ssam 	fs->fs_file = cp;
6512597Ssam 	cp = fsskip(cp);
6612597Ssam 	fs->fs_type = cp;
6712597Ssam 	cp = fsskip(cp);
6812597Ssam 	cp = fsdigit(&fs->fs_freq, cp, ':');
6912597Ssam 	if (cp == 0)
7012699Ssam 		return (3);
7112597Ssam 	cp = fsdigit(&fs->fs_passno, cp, '\n');
7212597Ssam 	if (cp == 0)
7312699Ssam 		return (4);
7412699Ssam 	return (5);
752010Swnj }
762010Swnj 
7712597Ssam setfsent()
782010Swnj {
7912597Ssam 
802010Swnj 	if (fs_file)
812010Swnj 		endfsent();
8213198Sroot 	if ((fs_file = fopen(FSTAB, "r")) == NULL) {
832010Swnj 		fs_file = 0;
8412597Ssam 		return (0);
852010Swnj 	}
8612597Ssam 	return (1);
872010Swnj }
882010Swnj 
8912597Ssam endfsent()
902010Swnj {
9112597Ssam 
9213198Sroot 	if (fs_file) {
932010Swnj 		fclose(fs_file);
9413198Sroot 		fs_file = 0;
9513198Sroot 	}
9612597Ssam 	return (1);
972010Swnj }
982010Swnj 
9912597Ssam struct fstab *
10012597Ssam getfsent()
1012010Swnj {
10212597Ssam 	int nfields;
1032010Swnj 
10412597Ssam 	if ((fs_file == 0) && (setfsent() == 0))
10512699Ssam 		return ((struct fstab *)0);
1062010Swnj 	nfields = fstabscan(&fs);
10712699Ssam 	if (nfields == EOF || nfields != 5)
10812699Ssam 		return ((struct fstab *)0);
10912597Ssam 	return (&fs);
1102010Swnj }
11112597Ssam 
11212597Ssam struct fstab *
11312597Ssam getfsspec(name)
11412597Ssam 	char *name;
1152010Swnj {
11612597Ssam 	register struct fstab *fsp;
11712597Ssam 
1182010Swnj 	if (setfsent() == 0)	/* start from the beginning */
11912699Ssam 		return ((struct fstab *)0);
12012597Ssam 	while((fsp = getfsent()) != 0)
12112597Ssam 		if (strcmp(fsp->fs_spec, name) == 0)
12212597Ssam 			return (fsp);
12312699Ssam 	return ((struct fstab *)0);
1242010Swnj }
12512597Ssam 
12612597Ssam struct fstab *
12712597Ssam getfsfile(name)
12812597Ssam 	char *name;
1292010Swnj {
13012597Ssam 	register struct fstab *fsp;
13112597Ssam 
1322010Swnj 	if (setfsent() == 0)	/* start from the beginning */
13312699Ssam 		return ((struct fstab *)0);
13412597Ssam 	while ((fsp = getfsent()) != 0)
13512597Ssam 		if (strcmp(fsp->fs_file, name) == 0)
13612597Ssam 			return (fsp);
13712699Ssam 	return ((struct fstab *)0);
1382010Swnj }
13912699Ssam 
14012699Ssam struct fstab *
14112699Ssam getfstype(type)
14212699Ssam 	char *type;
14312699Ssam {
14412699Ssam 	register struct fstab *fs;
14512699Ssam 
14612699Ssam 	if (setfsent() == 0)
14712699Ssam 		return ((struct fstab *)0);
14812699Ssam 	while ((fs = getfsent()) != 0)
14912699Ssam 		if (strcmp(fs->fs_type, type) == 0)
15012699Ssam 			return (fs);
15112699Ssam 	return ((struct fstab *)0);
15212699Ssam }
153