xref: /original-bsd/lib/libc/gen/fstab.c (revision 5998a314)
1 /* @(#)fstab.c	4.1 (Berkeley) 12/21/80 */
2 #include <fstab.h>
3 #include <stdio.h>
4 #include <ctype.h>
5 
6 static	struct	fstab fs;
7 static	FILE	*fs_file = 0;
8 
9 static	char	*fs_string(back, string, lg, end)
10 	char	*string, *back;
11 	int	lg;	/* length of field to stuff into */
12 	char	end;
13 {
14 	register	char	*cp;
15 	for (cp = string; *cp && *cp != end; cp++)
16 		continue;
17 	if (*cp == '\0') return(0);
18 	*cp = '\0';
19 	strncpy(back, string, lg-1);
20 	return(cp+1);
21 }
22 static char *fs_digit(backp, string, end)
23 	int	*backp;
24 	char	*string;
25 	char	end;
26 {
27 	register	int	value = 0;
28 	register	char	*cp;
29 	for (cp = string; *cp && isdigit(*cp); cp++){
30 		value *= 10;
31 		value += *cp - '0';
32 	}
33 	if (*cp == '\0') return(0);
34 	*backp = value;
35 	while ( *cp && *cp != end)
36 		cp++;
37 	if (*cp == '\0') return(0);
38 	return(cp+1);
39 }
40 
41 static	int	fstabscan(fsp)
42 	struct	fstab *fsp;
43 {
44 	register	char	*cp;
45 	char	buf[256];
46 	if (fgets(buf, 256, fs_file) == NULL)
47 		return(EOF);
48 	cp = buf;
49 	cp = fs_string(&fsp->fs_spec[0], cp, FSNMLG, ':');
50 	if (cp == 0)	return(0);
51 	cp = fs_string(&fsp->fs_file[0], cp, FSNMLG, ':');
52 	if (cp == 0)	return(1);
53 	cp = fs_string(&fsp->fs_type[0], cp, 3, ':');
54 	if (cp == 0)	return(2);
55 	cp = fs_digit(&fsp->fs_freq, cp, ':');
56 	if (cp == 0)	return(3);
57 	cp = fs_digit(&fsp->fs_passno, cp, '\n');
58 	if (cp == 0)	return(4);
59 	return(5);
60 }
61 
62 int	setfsent()
63 {
64 	if (fs_file)
65 		endfsent();
66 	if ( (fs_file = fopen(FSTAB, "r")) == NULL){
67 		fs_file = 0;
68 		return(0);
69 	}
70 	return(1);
71 }
72 
73 int endfsent()
74 {
75 	if (fs_file){
76 		fclose(fs_file);
77 	}
78 	return(1);
79 }
80 
81 struct fstab *getfsent()
82 {
83 	int	nfields;
84 
85 	if ( (fs_file == 0) && (setfsent() == 0) )
86 		return(0);
87 	nfields = fstabscan(&fs);
88 	if (nfields == EOF || nfields != FSTABNARGS)
89 		return(0);
90 	return(&fs);
91 }
92 struct fstab *getfsspec(name)
93 	char	*name;
94 {
95 	register	struct	fstab *fsp;
96 	if (setfsent() == 0)	/* start from the beginning */
97 		return(0);
98 	while( (fsp = getfsent()) != 0){
99 		if (strncmp(fsp->fs_spec, name, sizeof(fsp->fs_spec)) == 0)
100 			return(fsp);
101 	}
102 	return(0);
103 }
104 struct fstab *getfsfile(name)
105 	char	*name;
106 {
107 	register	struct fstab *fsp;
108 	if (setfsent() == 0)	/* start from the beginning */
109 		return(0);
110 	while ( (fsp = getfsent()) != 0){
111 		if (strncmp(fsp->fs_file, name, sizeof(fsp->fs_spec)) == 0)
112 			return(fsp);
113 	}
114 	return(0);
115 }
116