reduce size of devname entry
[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)
cb12ecbf 9static char sccsid[] = "@(#)fstab.c 5.12 (Berkeley) %G%";
19bfcb20 10#endif /* LIBC_SCCS and not lint */
02ed2510 11
5b16589f 12#include <fstab.h>
7d3d0709 13#include <unistd.h>
5b16589f 14#include <stdio.h>
5b16589f 15
3a256f6a
KB
16static FILE *_fs_fp;
17static struct fstab _fs_fstab;
5b16589f 18
eb8edec4 19static
3a256f6a 20fstabscan()
5b16589f 21{
02ed2510 22 register char *cp;
92949ee3 23#define MAXLINELENGTH 1024
3a256f6a 24 static char line[MAXLINELENGTH];
e3e27d95
KM
25 char subline[MAXLINELENGTH];
26 char *fgets(), *strtok();
2f953898 27 int typexx;
eb8edec4
KB
28
29 for (;;) {
3a256f6a
KB
30 if (!(cp = fgets(line, sizeof(line), _fs_fp)))
31 return(0);
cb12ecbf
KB
32/* OLD_STYLE_FSTAB */
33 if (!strpbrk(cp, " \t")) {
34 _fs_fstab.fs_spec = strtok(cp, ":\n");
35 _fs_fstab.fs_file = strtok((char *)NULL, ":\n");
36 _fs_fstab.fs_type = strtok((char *)NULL, ":\n");
37 if (_fs_fstab.fs_type) {
38 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
39 continue;
40 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
41 _fs_fstab.fs_vfstype =
42 strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
43 "ufs" : "swap";
44 if (cp = strtok((char *)NULL, ":\n")) {
45 _fs_fstab.fs_freq = atoi(cp);
46 if (cp = strtok((char *)NULL, ":\n")) {
47 _fs_fstab.fs_passno = atoi(cp);
48 return(1);
49 }
50 }
51 }
52 goto bad;
53 }
54/* OLD_STYLE_FSTAB */
e3e27d95 55 _fs_fstab.fs_spec = strtok(cp, " \t\n");
784f7b79
KB
56 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
57 continue;
e3e27d95
KM
58 _fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
59 _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
60 _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
61 if (_fs_fstab.fs_mntops == NULL)
62 goto bad;
63 _fs_fstab.fs_freq = 0;
64 _fs_fstab.fs_passno = 0;
65 if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
66 _fs_fstab.fs_freq = atoi(cp);
67 if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
68 _fs_fstab.fs_passno = atoi(cp);
69 }
70 strcpy(subline, _fs_fstab.fs_mntops);
2f953898 71 for (typexx = 0, cp = strtok(subline, ","); cp;
e3e27d95
KM
72 cp = strtok((char *)NULL, ",")) {
73 if (strlen(cp) != 2)
eb8edec4 74 continue;
e3e27d95
KM
75 if (!strcmp(cp, FSTAB_RW)) {
76 _fs_fstab.fs_type = FSTAB_RW;
77 break;
78 }
79 if (!strcmp(cp, FSTAB_RQ)) {
80 _fs_fstab.fs_type = FSTAB_RQ;
81 break;
82 }
83 if (!strcmp(cp, FSTAB_RO)) {
84 _fs_fstab.fs_type = FSTAB_RO;
85 break;
86 }
87 if (!strcmp(cp, FSTAB_SW)) {
88 _fs_fstab.fs_type = FSTAB_SW;
89 break;
90 }
91 if (!strcmp(cp, FSTAB_XX)) {
92 _fs_fstab.fs_type = FSTAB_XX;
2f953898 93 typexx++;
e3e27d95 94 break;
7d3d0709 95 }
eb8edec4 96 }
2f953898
KM
97 if (typexx)
98 continue;
e3e27d95
KM
99 if (cp != NULL)
100 return(1);
cb12ecbf
KB
101
102bad: /* no way to distinguish between EOF and syntax error */
7d3d0709
KB
103 (void)write(STDERR_FILENO, "fstab: ", 7);
104 (void)write(STDERR_FILENO, _PATH_FSTAB,
105 sizeof(_PATH_FSTAB) - 1);
106 (void)write(STDERR_FILENO, ": syntax error.\n", 16);
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)
eb8edec4 121 register 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)
eb8edec4 132 register 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 }
7d3d0709 147 return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
3a256f6a
KB
148}
149
150void
151endfsent()
152{
153 if (_fs_fp) {
154 (void)fclose(_fs_fp);
155 _fs_fp = NULL;
156 }
157}