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