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