xref: /csrg-svn/lib/libc/gen/fstab.c (revision 38671)
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*38671Smckusick static char sccsid[] = "@(#)fstab.c	5.7 (Berkeley) 08/18/89";
2035368Sbostic #endif /* LIBC_SCCS and not lint */
2112597Ssam 
222010Swnj #include <fstab.h>
2338110Sbostic #include <unistd.h>
242010Swnj #include <stdio.h>
252010Swnj 
2636214Sbostic static FILE *_fs_fp;
2736214Sbostic static struct fstab _fs_fstab;
282010Swnj 
2935370Sbostic static
3036214Sbostic fstabscan()
312010Swnj {
3212597Ssam 	register char *cp;
3336214Sbostic #define	MAXLINELENGTH	100
3436214Sbostic 	static char line[MAXLINELENGTH];
35*38671Smckusick 	char subline[MAXLINELENGTH];
36*38671Smckusick 	char *fgets(), *strtok();
3712597Ssam 
3835370Sbostic 	for (;;) {
3936214Sbostic 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
4036214Sbostic 			return(0);
41*38671Smckusick 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
42*38671Smckusick 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
43*38671Smckusick 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
44*38671Smckusick 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
45*38671Smckusick 		if (_fs_fstab.fs_mntops == NULL)
46*38671Smckusick 			goto bad;
47*38671Smckusick 		_fs_fstab.fs_freq = 0;
48*38671Smckusick 		_fs_fstab.fs_passno = 0;
49*38671Smckusick 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
50*38671Smckusick 			_fs_fstab.fs_freq = atoi(cp);
51*38671Smckusick 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
52*38671Smckusick 				_fs_fstab.fs_passno = atoi(cp);
53*38671Smckusick 		}
54*38671Smckusick 		strcpy(subline, _fs_fstab.fs_mntops);
55*38671Smckusick 		for (cp = strtok(subline, ","); cp;
56*38671Smckusick 		     cp = strtok((char *)NULL, ",")) {
57*38671Smckusick 			if (strlen(cp) != 2)
5835370Sbostic 				continue;
59*38671Smckusick 			if (!strcmp(cp, FSTAB_RW)) {
60*38671Smckusick 				_fs_fstab.fs_type = FSTAB_RW;
61*38671Smckusick 				break;
6238110Sbostic 			}
63*38671Smckusick 			if (!strcmp(cp, FSTAB_RQ)) {
64*38671Smckusick 				_fs_fstab.fs_type = FSTAB_RQ;
65*38671Smckusick 				break;
66*38671Smckusick 			}
67*38671Smckusick 			if (!strcmp(cp, FSTAB_RO)) {
68*38671Smckusick 				_fs_fstab.fs_type = FSTAB_RO;
69*38671Smckusick 				break;
70*38671Smckusick 			}
71*38671Smckusick 			if (!strcmp(cp, FSTAB_SW)) {
72*38671Smckusick 				_fs_fstab.fs_type = FSTAB_SW;
73*38671Smckusick 				break;
74*38671Smckusick 			}
75*38671Smckusick 			if (!strcmp(cp, FSTAB_XX)) {
76*38671Smckusick 				_fs_fstab.fs_type = FSTAB_XX;
77*38671Smckusick 				break;
78*38671Smckusick 			}
7935370Sbostic 		}
80*38671Smckusick 		if (cp != NULL)
81*38671Smckusick 			return(1);
82*38671Smckusick 	bad:
8338110Sbostic 		/* no way to distinguish between EOF and syntax error */
8438110Sbostic 		(void)write(STDERR_FILENO, "fstab: ", 7);
8538110Sbostic 		(void)write(STDERR_FILENO, _PATH_FSTAB,
8638110Sbostic 		    sizeof(_PATH_FSTAB) - 1);
8738110Sbostic 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
882010Swnj 	}
8935370Sbostic 	/* NOTREACHED */
902010Swnj }
912010Swnj 
9212597Ssam struct fstab *
9312597Ssam getfsent()
942010Swnj {
9536214Sbostic 	if (!_fs_fp && !setfsent() || !fstabscan())
9635370Sbostic 		return((struct fstab *)NULL);
9736214Sbostic 	return(&_fs_fstab);
982010Swnj }
9912597Ssam 
10012597Ssam struct fstab *
10112597Ssam getfsspec(name)
10235370Sbostic 	register char *name;
1032010Swnj {
10436214Sbostic 	if (setfsent())
10536214Sbostic 		while (fstabscan())
10636214Sbostic 			if (!strcmp(_fs_fstab.fs_spec, name))
10736214Sbostic 				return(&_fs_fstab);
10835370Sbostic 	return((struct fstab *)NULL);
1092010Swnj }
11012597Ssam 
11112597Ssam struct fstab *
11212597Ssam getfsfile(name)
11335370Sbostic 	register char *name;
1142010Swnj {
11536214Sbostic 	if (setfsent())
11636214Sbostic 		while (fstabscan())
11736214Sbostic 			if (!strcmp(_fs_fstab.fs_file, name))
11836214Sbostic 				return(&_fs_fstab);
11935370Sbostic 	return((struct fstab *)NULL);
1202010Swnj }
12136214Sbostic 
12236214Sbostic setfsent()
12336214Sbostic {
12436214Sbostic 	if (_fs_fp) {
12536214Sbostic 		rewind(_fs_fp);
12636214Sbostic 		return(1);
12736214Sbostic 	}
12838110Sbostic 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
12936214Sbostic }
13036214Sbostic 
13136214Sbostic void
13236214Sbostic endfsent()
13336214Sbostic {
13436214Sbostic 	if (_fs_fp) {
13536214Sbostic 		(void)fclose(_fs_fp);
13636214Sbostic 		_fs_fp = NULL;
13736214Sbostic 	}
13836214Sbostic }
139