4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / lib / libc / gen / fstab.c
CommitLineData
bb0cfa24 1/*
74155b62
KB
2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
19bfcb20 4 *
269a7923 5 * %sccs.include.redist.c%
bb0cfa24
DF
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
74155b62 9static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) %G%";
19bfcb20 10#endif /* LIBC_SCCS and not lint */
02ed2510 11
f2f3c44b 12#include <errno.h>
5b16589f
BJ
13#include <fstab.h>
14#include <stdio.h>
c5980113 15#include <stdlib.h>
90793b84 16#include <string.h>
f2f3c44b 17#include <unistd.h>
5b16589f 18
3a256f6a
KB
19static FILE *_fs_fp;
20static struct fstab _fs_fstab;
f2f3c44b
KB
21
22static error __P((int));
23static fstabscan __P((void));
5b16589f 24
eb8edec4 25static
3a256f6a 26fstabscan()
5b16589f 27{
02ed2510 28 register char *cp;
92949ee3 29#define MAXLINELENGTH 1024
3a256f6a 30 static char line[MAXLINELENGTH];
e3e27d95 31 char subline[MAXLINELENGTH];
2f953898 32 int typexx;
eb8edec4
KB
33
34 for (;;) {
3a256f6a
KB
35 if (!(cp = fgets(line, sizeof(line), _fs_fp)))
36 return(0);
cb12ecbf
KB
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 */
e3e27d95 60 _fs_fstab.fs_spec = strtok(cp, " \t\n");
784f7b79
KB
61 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
62 continue;
e3e27d95
KM
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);
2f953898 76 for (typexx = 0, cp = strtok(subline, ","); cp;
e3e27d95
KM
77 cp = strtok((char *)NULL, ",")) {
78 if (strlen(cp) != 2)
eb8edec4 79 continue;
e3e27d95
KM
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;
2f953898 98 typexx++;
e3e27d95 99 break;
7d3d0709 100 }
eb8edec4 101 }
2f953898
KM
102 if (typexx)
103 continue;
e3e27d95
KM
104 if (cp != NULL)
105 return(1);
cb12ecbf
KB
106
107bad: /* no way to distinguish between EOF and syntax error */
c5980113 108 error(EFTYPE);
5b16589f 109 }
eb8edec4 110 /* NOTREACHED */
5b16589f
BJ
111}
112
02ed2510
SL
113struct fstab *
114getfsent()
5b16589f 115{
3a256f6a 116 if (!_fs_fp && !setfsent() || !fstabscan())
eb8edec4 117 return((struct fstab *)NULL);
3a256f6a 118 return(&_fs_fstab);
5b16589f 119}
02ed2510
SL
120
121struct fstab *
122getfsspec(name)
c5980113 123 register const char *name;
5b16589f 124{
3a256f6a
KB
125 if (setfsent())
126 while (fstabscan())
127 if (!strcmp(_fs_fstab.fs_spec, name))
128 return(&_fs_fstab);
eb8edec4 129 return((struct fstab *)NULL);
5b16589f 130}
02ed2510
SL
131
132struct fstab *
133getfsfile(name)
c5980113 134 register const char *name;
5b16589f 135{
3a256f6a
KB
136 if (setfsent())
137 while (fstabscan())
138 if (!strcmp(_fs_fstab.fs_file, name))
139 return(&_fs_fstab);
eb8edec4 140 return((struct fstab *)NULL);
5b16589f 141}
3a256f6a
KB
142
143setfsent()
144{
145 if (_fs_fp) {
146 rewind(_fs_fp);
147 return(1);
148 }
90793b84
KB
149 if (_fs_fp = fopen(_PATH_FSTAB, "r"))
150 return(1);
151 error(errno);
152 return(0);
3a256f6a
KB
153}
154
155void
156endfsent()
157{
158 if (_fs_fp) {
159 (void)fclose(_fs_fp);
160 _fs_fp = NULL;
161 }
162}
90793b84
KB
163
164static
165error(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);
781314c9 172 (void)write(STDERR_FILENO, ": ", 1);
90793b84
KB
173 p = strerror(err);
174 (void)write(STDERR_FILENO, p, strlen(p));
175 (void)write(STDERR_FILENO, "\n", 1);
176}