xref: /original-bsd/lib/libc/gen/fstab.c (revision 82ca1924)
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.6 (Berkeley) 05/23/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <fstab.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 
26 static FILE *_fs_fp;
27 static struct fstab _fs_fstab;
28 
29 static
30 fstabscan()
31 {
32 	register char *cp;
33 #define	MAXLINELENGTH	100
34 	static char line[MAXLINELENGTH];
35 	char *fgets(), *strsep();
36 
37 	for (;;) {
38 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
39 			return(0);
40 		_fs_fstab.fs_spec = strsep(cp, ":\n");
41 		_fs_fstab.fs_file = strsep((char *)NULL, ":\n");
42 		_fs_fstab.fs_type = strsep((char *)NULL, ":\n");
43 		if (_fs_fstab.fs_type) {
44 			if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
45 				continue;
46 			if (cp = strsep((char *)NULL, ":\n")) {
47 				_fs_fstab.fs_freq = atoi(cp);
48 				if (cp = strsep((char *)NULL, ":\n")) {
49 					_fs_fstab.fs_passno = atoi(cp);
50 					return(1);
51 				}
52 			}
53 		}
54 		/* no way to distinguish between EOF and syntax error */
55 		(void)write(STDERR_FILENO, "fstab: ", 7);
56 		(void)write(STDERR_FILENO, _PATH_FSTAB,
57 		    sizeof(_PATH_FSTAB) - 1);
58 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
59 	}
60 	/* NOTREACHED */
61 }
62 
63 struct fstab *
64 getfsent()
65 {
66 	if (!_fs_fp && !setfsent() || !fstabscan())
67 		return((struct fstab *)NULL);
68 	return(&_fs_fstab);
69 }
70 
71 struct fstab *
72 getfsspec(name)
73 	register char *name;
74 {
75 	if (setfsent())
76 		while (fstabscan())
77 			if (!strcmp(_fs_fstab.fs_spec, name))
78 				return(&_fs_fstab);
79 	return((struct fstab *)NULL);
80 }
81 
82 struct fstab *
83 getfsfile(name)
84 	register char *name;
85 {
86 	if (setfsent())
87 		while (fstabscan())
88 			if (!strcmp(_fs_fstab.fs_file, name))
89 				return(&_fs_fstab);
90 	return((struct fstab *)NULL);
91 }
92 
93 setfsent()
94 {
95 	if (_fs_fp) {
96 		rewind(_fs_fp);
97 		return(1);
98 	}
99 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
100 }
101 
102 void
103 endfsent()
104 {
105 	if (_fs_fp) {
106 		(void)fclose(_fs_fp);
107 		_fs_fp = NULL;
108 	}
109 }
110