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