Commit | Line | Data |
---|---|---|
bb0cfa24 | 1 | /* |
eb8edec4 | 2 | * Copyright (c) 1980, 1988 Regents of the University of California. |
19bfcb20 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
bb0cfa24 DF |
16 | */ |
17 | ||
2ce81398 | 18 | #if defined(LIBC_SCCS) && !defined(lint) |
784f7b79 | 19 | static char sccsid[] = "@(#)fstab.c 5.10 (Berkeley) %G%"; |
19bfcb20 | 20 | #endif /* LIBC_SCCS and not lint */ |
02ed2510 | 21 | |
5b16589f | 22 | #include <fstab.h> |
7d3d0709 | 23 | #include <unistd.h> |
5b16589f | 24 | #include <stdio.h> |
5b16589f | 25 | |
3a256f6a KB |
26 | static FILE *_fs_fp; |
27 | static struct fstab _fs_fstab; | |
5b16589f | 28 | |
eb8edec4 | 29 | static |
3a256f6a | 30 | fstabscan() |
5b16589f | 31 | { |
02ed2510 | 32 | register char *cp; |
92949ee3 | 33 | #define MAXLINELENGTH 1024 |
3a256f6a | 34 | static char line[MAXLINELENGTH]; |
e3e27d95 KM |
35 | char subline[MAXLINELENGTH]; |
36 | char *fgets(), *strtok(); | |
2f953898 | 37 | int typexx; |
eb8edec4 KB |
38 | |
39 | for (;;) { | |
3a256f6a KB |
40 | if (!(cp = fgets(line, sizeof(line), _fs_fp))) |
41 | return(0); | |
e3e27d95 | 42 | _fs_fstab.fs_spec = strtok(cp, " \t\n"); |
784f7b79 KB |
43 | if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') |
44 | continue; | |
e3e27d95 KM |
45 | _fs_fstab.fs_file = strtok((char *)NULL, " \t\n"); |
46 | _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n"); | |
47 | _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n"); | |
48 | if (_fs_fstab.fs_mntops == NULL) | |
49 | goto bad; | |
50 | _fs_fstab.fs_freq = 0; | |
51 | _fs_fstab.fs_passno = 0; | |
52 | if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { | |
53 | _fs_fstab.fs_freq = atoi(cp); | |
54 | if ((cp = strtok((char *)NULL, " \t\n")) != NULL) | |
55 | _fs_fstab.fs_passno = atoi(cp); | |
56 | } | |
57 | strcpy(subline, _fs_fstab.fs_mntops); | |
2f953898 | 58 | for (typexx = 0, cp = strtok(subline, ","); cp; |
e3e27d95 KM |
59 | cp = strtok((char *)NULL, ",")) { |
60 | if (strlen(cp) != 2) | |
eb8edec4 | 61 | continue; |
e3e27d95 KM |
62 | if (!strcmp(cp, FSTAB_RW)) { |
63 | _fs_fstab.fs_type = FSTAB_RW; | |
64 | break; | |
65 | } | |
66 | if (!strcmp(cp, FSTAB_RQ)) { | |
67 | _fs_fstab.fs_type = FSTAB_RQ; | |
68 | break; | |
69 | } | |
70 | if (!strcmp(cp, FSTAB_RO)) { | |
71 | _fs_fstab.fs_type = FSTAB_RO; | |
72 | break; | |
73 | } | |
74 | if (!strcmp(cp, FSTAB_SW)) { | |
75 | _fs_fstab.fs_type = FSTAB_SW; | |
76 | break; | |
77 | } | |
78 | if (!strcmp(cp, FSTAB_XX)) { | |
79 | _fs_fstab.fs_type = FSTAB_XX; | |
2f953898 | 80 | typexx++; |
e3e27d95 | 81 | break; |
7d3d0709 | 82 | } |
eb8edec4 | 83 | } |
2f953898 KM |
84 | if (typexx) |
85 | continue; | |
e3e27d95 KM |
86 | if (cp != NULL) |
87 | return(1); | |
88 | bad: | |
7d3d0709 KB |
89 | /* no way to distinguish between EOF and syntax error */ |
90 | (void)write(STDERR_FILENO, "fstab: ", 7); | |
91 | (void)write(STDERR_FILENO, _PATH_FSTAB, | |
92 | sizeof(_PATH_FSTAB) - 1); | |
93 | (void)write(STDERR_FILENO, ": syntax error.\n", 16); | |
5b16589f | 94 | } |
eb8edec4 | 95 | /* NOTREACHED */ |
5b16589f BJ |
96 | } |
97 | ||
02ed2510 SL |
98 | struct fstab * |
99 | getfsent() | |
5b16589f | 100 | { |
3a256f6a | 101 | if (!_fs_fp && !setfsent() || !fstabscan()) |
eb8edec4 | 102 | return((struct fstab *)NULL); |
3a256f6a | 103 | return(&_fs_fstab); |
5b16589f | 104 | } |
02ed2510 SL |
105 | |
106 | struct fstab * | |
107 | getfsspec(name) | |
eb8edec4 | 108 | register char *name; |
5b16589f | 109 | { |
3a256f6a KB |
110 | if (setfsent()) |
111 | while (fstabscan()) | |
112 | if (!strcmp(_fs_fstab.fs_spec, name)) | |
113 | return(&_fs_fstab); | |
eb8edec4 | 114 | return((struct fstab *)NULL); |
5b16589f | 115 | } |
02ed2510 SL |
116 | |
117 | struct fstab * | |
118 | getfsfile(name) | |
eb8edec4 | 119 | register char *name; |
5b16589f | 120 | { |
3a256f6a KB |
121 | if (setfsent()) |
122 | while (fstabscan()) | |
123 | if (!strcmp(_fs_fstab.fs_file, name)) | |
124 | return(&_fs_fstab); | |
eb8edec4 | 125 | return((struct fstab *)NULL); |
5b16589f | 126 | } |
3a256f6a KB |
127 | |
128 | setfsent() | |
129 | { | |
130 | if (_fs_fp) { | |
131 | rewind(_fs_fp); | |
132 | return(1); | |
133 | } | |
7d3d0709 | 134 | return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL); |
3a256f6a KB |
135 | } |
136 | ||
137 | void | |
138 | endfsent() | |
139 | { | |
140 | if (_fs_fp) { | |
141 | (void)fclose(_fs_fp); | |
142 | _fs_fp = NULL; | |
143 | } | |
144 | } |