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