new copyright notice
[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)
269a7923 9static char sccsid[] = "@(#)fstab.c 5.11 (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);
e3e27d95 32 _fs_fstab.fs_spec = strtok(cp, " \t\n");
784f7b79
KB
33 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
34 continue;
e3e27d95
KM
35 _fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
36 _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
37 _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
38 if (_fs_fstab.fs_mntops == NULL)
39 goto bad;
40 _fs_fstab.fs_freq = 0;
41 _fs_fstab.fs_passno = 0;
42 if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
43 _fs_fstab.fs_freq = atoi(cp);
44 if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
45 _fs_fstab.fs_passno = atoi(cp);
46 }
47 strcpy(subline, _fs_fstab.fs_mntops);
2f953898 48 for (typexx = 0, cp = strtok(subline, ","); cp;
e3e27d95
KM
49 cp = strtok((char *)NULL, ",")) {
50 if (strlen(cp) != 2)
eb8edec4 51 continue;
e3e27d95
KM
52 if (!strcmp(cp, FSTAB_RW)) {
53 _fs_fstab.fs_type = FSTAB_RW;
54 break;
55 }
56 if (!strcmp(cp, FSTAB_RQ)) {
57 _fs_fstab.fs_type = FSTAB_RQ;
58 break;
59 }
60 if (!strcmp(cp, FSTAB_RO)) {
61 _fs_fstab.fs_type = FSTAB_RO;
62 break;
63 }
64 if (!strcmp(cp, FSTAB_SW)) {
65 _fs_fstab.fs_type = FSTAB_SW;
66 break;
67 }
68 if (!strcmp(cp, FSTAB_XX)) {
69 _fs_fstab.fs_type = FSTAB_XX;
2f953898 70 typexx++;
e3e27d95 71 break;
7d3d0709 72 }
eb8edec4 73 }
2f953898
KM
74 if (typexx)
75 continue;
e3e27d95
KM
76 if (cp != NULL)
77 return(1);
78 bad:
7d3d0709
KB
79 /* no way to distinguish between EOF and syntax error */
80 (void)write(STDERR_FILENO, "fstab: ", 7);
81 (void)write(STDERR_FILENO, _PATH_FSTAB,
82 sizeof(_PATH_FSTAB) - 1);
83 (void)write(STDERR_FILENO, ": syntax error.\n", 16);
5b16589f 84 }
eb8edec4 85 /* NOTREACHED */
5b16589f
BJ
86}
87
02ed2510
SL
88struct fstab *
89getfsent()
5b16589f 90{
3a256f6a 91 if (!_fs_fp && !setfsent() || !fstabscan())
eb8edec4 92 return((struct fstab *)NULL);
3a256f6a 93 return(&_fs_fstab);
5b16589f 94}
02ed2510
SL
95
96struct fstab *
97getfsspec(name)
eb8edec4 98 register char *name;
5b16589f 99{
3a256f6a
KB
100 if (setfsent())
101 while (fstabscan())
102 if (!strcmp(_fs_fstab.fs_spec, name))
103 return(&_fs_fstab);
eb8edec4 104 return((struct fstab *)NULL);
5b16589f 105}
02ed2510
SL
106
107struct fstab *
108getfsfile(name)
eb8edec4 109 register char *name;
5b16589f 110{
3a256f6a
KB
111 if (setfsent())
112 while (fstabscan())
113 if (!strcmp(_fs_fstab.fs_file, name))
114 return(&_fs_fstab);
eb8edec4 115 return((struct fstab *)NULL);
5b16589f 116}
3a256f6a
KB
117
118setfsent()
119{
120 if (_fs_fp) {
121 rewind(_fs_fp);
122 return(1);
123 }
7d3d0709 124 return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
3a256f6a
KB
125}
126
127void
128endfsent()
129{
130 if (_fs_fp) {
131 (void)fclose(_fs_fp);
132 _fs_fp = NULL;
133 }
134}