xref: /original-bsd/lib/libc/gen/fstab.c (revision e718337e)
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.14 (Berkeley) 11/28/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/errno.h>
13 #include <fstab.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <string.h>
17 
18 static FILE *_fs_fp;
19 static struct fstab _fs_fstab;
20 
21 static
22 fstabscan()
23 {
24 	register char *cp;
25 #define	MAXLINELENGTH	1024
26 	static char line[MAXLINELENGTH];
27 	char subline[MAXLINELENGTH];
28 	char *fgets(), *strtok();
29 	int typexx;
30 
31 	for (;;) {
32 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
33 			return(0);
34 /* OLD_STYLE_FSTAB */
35 		if (!strpbrk(cp, " \t")) {
36 			_fs_fstab.fs_spec = strtok(cp, ":\n");
37 			_fs_fstab.fs_file = strtok((char *)NULL, ":\n");
38 			_fs_fstab.fs_type = strtok((char *)NULL, ":\n");
39 			if (_fs_fstab.fs_type) {
40 				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
41 					continue;
42 				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
43 				_fs_fstab.fs_vfstype =
44 				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
45 				    "ufs" : "swap";
46 				if (cp = strtok((char *)NULL, ":\n")) {
47 					_fs_fstab.fs_freq = atoi(cp);
48 					if (cp = strtok((char *)NULL, ":\n")) {
49 						_fs_fstab.fs_passno = atoi(cp);
50 						return(1);
51 					}
52 				}
53 			}
54 			goto bad;
55 		}
56 /* OLD_STYLE_FSTAB */
57 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
58 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
59 			continue;
60 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
61 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
62 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
63 		if (_fs_fstab.fs_mntops == NULL)
64 			goto bad;
65 		_fs_fstab.fs_freq = 0;
66 		_fs_fstab.fs_passno = 0;
67 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
68 			_fs_fstab.fs_freq = atoi(cp);
69 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
70 				_fs_fstab.fs_passno = atoi(cp);
71 		}
72 		strcpy(subline, _fs_fstab.fs_mntops);
73 		for (typexx = 0, cp = strtok(subline, ","); cp;
74 		     cp = strtok((char *)NULL, ",")) {
75 			if (strlen(cp) != 2)
76 				continue;
77 			if (!strcmp(cp, FSTAB_RW)) {
78 				_fs_fstab.fs_type = FSTAB_RW;
79 				break;
80 			}
81 			if (!strcmp(cp, FSTAB_RQ)) {
82 				_fs_fstab.fs_type = FSTAB_RQ;
83 				break;
84 			}
85 			if (!strcmp(cp, FSTAB_RO)) {
86 				_fs_fstab.fs_type = FSTAB_RO;
87 				break;
88 			}
89 			if (!strcmp(cp, FSTAB_SW)) {
90 				_fs_fstab.fs_type = FSTAB_SW;
91 				break;
92 			}
93 			if (!strcmp(cp, FSTAB_XX)) {
94 				_fs_fstab.fs_type = FSTAB_XX;
95 				typexx++;
96 				break;
97 			}
98 		}
99 		if (typexx)
100 			continue;
101 		if (cp != NULL)
102 			return(1);
103 
104 bad:		/* no way to distinguish between EOF and syntax error */
105 		write(EFTYPE);
106 	}
107 	/* NOTREACHED */
108 }
109 
110 struct fstab *
111 getfsent()
112 {
113 	if (!_fs_fp && !setfsent() || !fstabscan())
114 		return((struct fstab *)NULL);
115 	return(&_fs_fstab);
116 }
117 
118 struct fstab *
119 getfsspec(name)
120 	register char *name;
121 {
122 	if (setfsent())
123 		while (fstabscan())
124 			if (!strcmp(_fs_fstab.fs_spec, name))
125 				return(&_fs_fstab);
126 	return((struct fstab *)NULL);
127 }
128 
129 struct fstab *
130 getfsfile(name)
131 	register char *name;
132 {
133 	if (setfsent())
134 		while (fstabscan())
135 			if (!strcmp(_fs_fstab.fs_file, name))
136 				return(&_fs_fstab);
137 	return((struct fstab *)NULL);
138 }
139 
140 setfsent()
141 {
142 	if (_fs_fp) {
143 		rewind(_fs_fp);
144 		return(1);
145 	}
146 	if (_fs_fp = fopen(_PATH_FSTAB, "r"))
147 		return(1);
148 	error(errno);
149 	return(0);
150 }
151 
152 void
153 endfsent()
154 {
155 	if (_fs_fp) {
156 		(void)fclose(_fs_fp);
157 		_fs_fp = NULL;
158 	}
159 }
160 
161 static
162 error(err)
163 	int err;
164 {
165 	char *p;
166 
167 	(void)write(STDERR_FILENO, "fstab: ", 7);
168 	(void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
169 	p = strerror(err);
170 	(void)write(STDERR_FILENO, p, strlen(p));
171 	(void)write(STDERR_FILENO, "\n", 1);
172 }
173