xref: /original-bsd/lib/libc/gen/fstab.c (revision 09b04dfe)
1 /*
2  * Copyright (c) 1980, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)fstab.c	5.11 (Berkeley) 06/01/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <fstab.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 
16 static FILE *_fs_fp;
17 static struct fstab _fs_fstab;
18 
19 static
20 fstabscan()
21 {
22 	register char *cp;
23 #define	MAXLINELENGTH	1024
24 	static char line[MAXLINELENGTH];
25 	char subline[MAXLINELENGTH];
26 	char *fgets(), *strtok();
27 	int typexx;
28 
29 	for (;;) {
30 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
31 			return(0);
32 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
33 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
34 			continue;
35 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
36 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
37 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
38 		if (_fs_fstab.fs_mntops == NULL)
39 			goto bad;
40 		_fs_fstab.fs_freq = 0;
41 		_fs_fstab.fs_passno = 0;
42 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
43 			_fs_fstab.fs_freq = atoi(cp);
44 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
45 				_fs_fstab.fs_passno = atoi(cp);
46 		}
47 		strcpy(subline, _fs_fstab.fs_mntops);
48 		for (typexx = 0, cp = strtok(subline, ","); cp;
49 		     cp = strtok((char *)NULL, ",")) {
50 			if (strlen(cp) != 2)
51 				continue;
52 			if (!strcmp(cp, FSTAB_RW)) {
53 				_fs_fstab.fs_type = FSTAB_RW;
54 				break;
55 			}
56 			if (!strcmp(cp, FSTAB_RQ)) {
57 				_fs_fstab.fs_type = FSTAB_RQ;
58 				break;
59 			}
60 			if (!strcmp(cp, FSTAB_RO)) {
61 				_fs_fstab.fs_type = FSTAB_RO;
62 				break;
63 			}
64 			if (!strcmp(cp, FSTAB_SW)) {
65 				_fs_fstab.fs_type = FSTAB_SW;
66 				break;
67 			}
68 			if (!strcmp(cp, FSTAB_XX)) {
69 				_fs_fstab.fs_type = FSTAB_XX;
70 				typexx++;
71 				break;
72 			}
73 		}
74 		if (typexx)
75 			continue;
76 		if (cp != NULL)
77 			return(1);
78 	bad:
79 		/* no way to distinguish between EOF and syntax error */
80 		(void)write(STDERR_FILENO, "fstab: ", 7);
81 		(void)write(STDERR_FILENO, _PATH_FSTAB,
82 		    sizeof(_PATH_FSTAB) - 1);
83 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
84 	}
85 	/* NOTREACHED */
86 }
87 
88 struct fstab *
89 getfsent()
90 {
91 	if (!_fs_fp && !setfsent() || !fstabscan())
92 		return((struct fstab *)NULL);
93 	return(&_fs_fstab);
94 }
95 
96 struct fstab *
97 getfsspec(name)
98 	register char *name;
99 {
100 	if (setfsent())
101 		while (fstabscan())
102 			if (!strcmp(_fs_fstab.fs_spec, name))
103 				return(&_fs_fstab);
104 	return((struct fstab *)NULL);
105 }
106 
107 struct fstab *
108 getfsfile(name)
109 	register char *name;
110 {
111 	if (setfsent())
112 		while (fstabscan())
113 			if (!strcmp(_fs_fstab.fs_file, name))
114 				return(&_fs_fstab);
115 	return((struct fstab *)NULL);
116 }
117 
118 setfsent()
119 {
120 	if (_fs_fp) {
121 		rewind(_fs_fp);
122 		return(1);
123 	}
124 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
125 }
126 
127 void
128 endfsent()
129 {
130 	if (_fs_fp) {
131 		(void)fclose(_fs_fp);
132 		_fs_fp = NULL;
133 	}
134 }
135