xref: /csrg-svn/lib/libc/gen/fstab.c (revision 35370)
1 /*
2  * Copyright (c) 1980, 1988 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.4 (Berkeley) 08/15/88";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <fstab.h>
23 #include <stdio.h>
24 
25 static	struct fstab fs;
26 static	FILE *fs_file = NULL;
27 static	char line[BUFSIZ+1];
28 
29 static
30 fstabscan(fsp)
31 	register struct fstab *fsp;
32 {
33 	register char *cp;
34 	char *fgets(), *strtok();
35 
36 	for (;;) {
37 		if (!(cp = fgets(line, sizeof(line), fs_file)))
38 			return(1);
39 		fsp->fs_spec = strtok(cp, ":\n");
40 		fsp->fs_file = strtok((char *)NULL, ":\n");
41 		fsp->fs_type = strtok((char *)NULL, ":\n");
42 		if (fsp->fs_type && strcmp(fsp->fs_type, FSTAB_XX)) {
43 			if (!(cp = strtok((char *)NULL, ":\n")))
44 				continue;
45 			fsp->fs_freq = atoi(cp);
46 			if (!(cp = strtok((char *)NULL, ":\n")))
47 				continue;
48 			fsp->fs_passno = atoi(cp);
49 			return(0);
50 		}
51 	}
52 	/* NOTREACHED */
53 }
54 
55 setfsent()
56 {
57 	if (fs_file)
58 		(void)endfsent();
59 	if ((fs_file = fopen(FSTAB, "r")) == NULL) {
60 		fs_file = NULL;
61 		return(0);
62 	}
63 	return(1);
64 }
65 
66 endfsent()
67 {
68 	if (fs_file) {
69 		(void)fclose(fs_file);
70 		fs_file = NULL;
71 	}
72 	return(1);
73 }
74 
75 struct fstab *
76 getfsent()
77 {
78 	if (fs_file == NULL && !setfsent() || fstabscan(&fs))
79 		return((struct fstab *)NULL);
80 	return(&fs);
81 }
82 
83 struct fstab *
84 getfsspec(name)
85 	register char *name;
86 {
87 	register struct fstab *fsp;
88 
89 	if (!setfsent())		/* start from the beginning */
90 		return((struct fstab *)NULL);
91 	while (fsp = getfsent())
92 		if (!strcmp(fsp->fs_spec, name))
93 			return(fsp);
94 	return((struct fstab *)NULL);
95 }
96 
97 struct fstab *
98 getfsfile(name)
99 	register char *name;
100 {
101 	register struct fstab *fsp;
102 
103 	if (!setfsent())		/* start from the beginning */
104 		return((struct fstab *)NULL);
105 	while (fsp = getfsent())
106 		if (!strcmp(fsp->fs_file, name))
107 			return(fsp);
108 	return((struct fstab *)NULL);
109 }
110