Commit | Line | Data |
---|---|---|
bb0cfa24 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
02ed2510 | 7 | #ifndef lint |
bb0cfa24 DF |
8 | static char sccsid[] = "@(#)fstab.c 5.1 (Berkeley) %G%"; |
9 | #endif not lint | |
02ed2510 | 10 | |
5b16589f BJ |
11 | #include <fstab.h> |
12 | #include <stdio.h> | |
13 | #include <ctype.h> | |
14 | ||
02ed2510 SL |
15 | static struct fstab fs; |
16 | static char line[BUFSIZ+1]; | |
17 | static FILE *fs_file = 0; | |
5b16589f | 18 | |
02ed2510 SL |
19 | static char * |
20 | fsskip(p) | |
21 | register char *p; | |
5b16589f | 22 | { |
02ed2510 SL |
23 | |
24 | while (*p && *p != ':') | |
25 | ++p; | |
26 | if (*p) | |
27 | *p++ = 0; | |
28 | return (p); | |
5b16589f | 29 | } |
02ed2510 SL |
30 | |
31 | static char * | |
32 | fsdigit(backp, string, end) | |
33 | int *backp; | |
34 | char *string, end; | |
5b16589f | 35 | { |
02ed2510 SL |
36 | register int value = 0; |
37 | register char *cp; | |
38 | ||
39 | for (cp = string; *cp && isdigit(*cp); cp++) { | |
5b16589f BJ |
40 | value *= 10; |
41 | value += *cp - '0'; | |
42 | } | |
02ed2510 | 43 | if (*cp == '\0') |
3e969d08 | 44 | return ((char *)0); |
5b16589f | 45 | *backp = value; |
02ed2510 | 46 | while (*cp && *cp != end) |
5b16589f | 47 | cp++; |
02ed2510 | 48 | if (*cp == '\0') |
3e969d08 | 49 | return ((char *)0); |
02ed2510 | 50 | return (cp+1); |
5b16589f BJ |
51 | } |
52 | ||
02ed2510 SL |
53 | static |
54 | fstabscan(fs) | |
55 | struct fstab *fs; | |
5b16589f | 56 | { |
02ed2510 SL |
57 | register char *cp; |
58 | ||
59 | cp = fgets(line, 256, fs_file); | |
60 | if (cp == NULL) | |
61 | return (EOF); | |
62 | fs->fs_spec = cp; | |
63 | cp = fsskip(cp); | |
64 | fs->fs_file = cp; | |
65 | cp = fsskip(cp); | |
66 | fs->fs_type = cp; | |
67 | cp = fsskip(cp); | |
02ed2510 SL |
68 | cp = fsdigit(&fs->fs_freq, cp, ':'); |
69 | if (cp == 0) | |
3e969d08 | 70 | return (3); |
02ed2510 SL |
71 | cp = fsdigit(&fs->fs_passno, cp, '\n'); |
72 | if (cp == 0) | |
3e969d08 SL |
73 | return (4); |
74 | return (5); | |
5b16589f BJ |
75 | } |
76 | ||
02ed2510 | 77 | setfsent() |
5b16589f | 78 | { |
02ed2510 | 79 | |
5b16589f BJ |
80 | if (fs_file) |
81 | endfsent(); | |
78d4750f | 82 | if ((fs_file = fopen(FSTAB, "r")) == NULL) { |
5b16589f | 83 | fs_file = 0; |
02ed2510 | 84 | return (0); |
5b16589f | 85 | } |
02ed2510 | 86 | return (1); |
5b16589f BJ |
87 | } |
88 | ||
02ed2510 | 89 | endfsent() |
5b16589f | 90 | { |
02ed2510 | 91 | |
78d4750f | 92 | if (fs_file) { |
5b16589f | 93 | fclose(fs_file); |
78d4750f BJ |
94 | fs_file = 0; |
95 | } | |
02ed2510 | 96 | return (1); |
5b16589f BJ |
97 | } |
98 | ||
02ed2510 SL |
99 | struct fstab * |
100 | getfsent() | |
5b16589f | 101 | { |
02ed2510 | 102 | int nfields; |
5b16589f | 103 | |
02ed2510 | 104 | if ((fs_file == 0) && (setfsent() == 0)) |
3e969d08 | 105 | return ((struct fstab *)0); |
5b16589f | 106 | nfields = fstabscan(&fs); |
3e969d08 SL |
107 | if (nfields == EOF || nfields != 5) |
108 | return ((struct fstab *)0); | |
02ed2510 | 109 | return (&fs); |
5b16589f | 110 | } |
02ed2510 SL |
111 | |
112 | struct fstab * | |
113 | getfsspec(name) | |
114 | char *name; | |
5b16589f | 115 | { |
02ed2510 SL |
116 | register struct fstab *fsp; |
117 | ||
5b16589f | 118 | if (setfsent() == 0) /* start from the beginning */ |
3e969d08 | 119 | return ((struct fstab *)0); |
02ed2510 SL |
120 | while((fsp = getfsent()) != 0) |
121 | if (strcmp(fsp->fs_spec, name) == 0) | |
122 | return (fsp); | |
3e969d08 | 123 | return ((struct fstab *)0); |
5b16589f | 124 | } |
02ed2510 SL |
125 | |
126 | struct fstab * | |
127 | getfsfile(name) | |
128 | char *name; | |
5b16589f | 129 | { |
02ed2510 SL |
130 | register struct fstab *fsp; |
131 | ||
5b16589f | 132 | if (setfsent() == 0) /* start from the beginning */ |
3e969d08 | 133 | return ((struct fstab *)0); |
02ed2510 SL |
134 | while ((fsp = getfsent()) != 0) |
135 | if (strcmp(fsp->fs_file, name) == 0) | |
136 | return (fsp); | |
3e969d08 SL |
137 | return ((struct fstab *)0); |
138 | } | |
139 | ||
140 | struct fstab * | |
141 | getfstype(type) | |
142 | char *type; | |
143 | { | |
144 | register struct fstab *fs; | |
145 | ||
146 | if (setfsent() == 0) | |
147 | return ((struct fstab *)0); | |
148 | while ((fs = getfsent()) != 0) | |
149 | if (strcmp(fs->fs_type, type) == 0) | |
150 | return (fs); | |
151 | return ((struct fstab *)0); | |
5b16589f | 152 | } |