xref: /csrg-svn/lib/libc/gen/fstab.c (revision 35368)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
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.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)fstab.c	5.3 (Berkeley) 08/13/88";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <fstab.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 
26 static	struct fstab fs;
27 static	char line[BUFSIZ+1];
28 static	FILE *fs_file = 0;
29 
30 static char *
31 fsskip(p)
32 	register char *p;
33 {
34 
35 	while (*p && *p != ':')
36 		++p;
37 	if (*p)
38 		*p++ = 0;
39 	return (p);
40 }
41 
42 static char *
43 fsdigit(backp, string, end)
44 	int *backp;
45 	char *string, end;
46 {
47 	register int value = 0;
48 	register char *cp;
49 
50 	for (cp = string; *cp && isdigit(*cp); cp++) {
51 		value *= 10;
52 		value += *cp - '0';
53 	}
54 	if (*cp == '\0')
55 		return ((char *)0);
56 	*backp = value;
57 	while (*cp && *cp != end)
58 		cp++;
59 	if (*cp == '\0')
60 		return ((char *)0);
61 	return (cp+1);
62 }
63 
64 static
65 fstabscan(fs)
66 	struct fstab *fs;
67 {
68 	register char *cp;
69 
70 	cp = fgets(line, 256, fs_file);
71 	if (cp == NULL)
72 		return (EOF);
73 	fs->fs_spec = cp;
74 	cp = fsskip(cp);
75 	fs->fs_file = cp;
76 	cp = fsskip(cp);
77 	fs->fs_type = cp;
78 	cp = fsskip(cp);
79 	cp = fsdigit(&fs->fs_freq, cp, ':');
80 	if (cp == 0)
81 		return (3);
82 	cp = fsdigit(&fs->fs_passno, cp, '\n');
83 	if (cp == 0)
84 		return (4);
85 	return (5);
86 }
87 
88 setfsent()
89 {
90 
91 	if (fs_file)
92 		endfsent();
93 	if ((fs_file = fopen(FSTAB, "r")) == NULL) {
94 		fs_file = 0;
95 		return (0);
96 	}
97 	return (1);
98 }
99 
100 endfsent()
101 {
102 
103 	if (fs_file) {
104 		fclose(fs_file);
105 		fs_file = 0;
106 	}
107 	return (1);
108 }
109 
110 struct fstab *
111 getfsent()
112 {
113 	int nfields;
114 
115 	if ((fs_file == 0) && (setfsent() == 0))
116 		return ((struct fstab *)0);
117 	nfields = fstabscan(&fs);
118 	if (nfields == EOF || nfields != 5)
119 		return ((struct fstab *)0);
120 	return (&fs);
121 }
122 
123 struct fstab *
124 getfsspec(name)
125 	char *name;
126 {
127 	register struct fstab *fsp;
128 
129 	if (setfsent() == 0)	/* start from the beginning */
130 		return ((struct fstab *)0);
131 	while((fsp = getfsent()) != 0)
132 		if (strcmp(fsp->fs_spec, name) == 0)
133 			return (fsp);
134 	return ((struct fstab *)0);
135 }
136 
137 struct fstab *
138 getfsfile(name)
139 	char *name;
140 {
141 	register struct fstab *fsp;
142 
143 	if (setfsent() == 0)	/* start from the beginning */
144 		return ((struct fstab *)0);
145 	while ((fsp = getfsent()) != 0)
146 		if (strcmp(fsp->fs_file, name) == 0)
147 			return (fsp);
148 	return ((struct fstab *)0);
149 }
150 
151 struct fstab *
152 getfstype(type)
153 	char *type;
154 {
155 	register struct fstab *fs;
156 
157 	if (setfsent() == 0)
158 		return ((struct fstab *)0);
159 	while ((fs = getfsent()) != 0)
160 		if (strcmp(fs->fs_type, type) == 0)
161 			return (fs);
162 	return ((struct fstab *)0);
163 }
164