xref: /original-bsd/lib/libc/gen/fstab.c (revision 2745187c)
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.8 (Berkeley) 08/21/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 subline[MAXLINELENGTH];
36 	char *fgets(), *strtok();
37 	int typexx;
38 
39 	for (;;) {
40 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
41 			return(0);
42 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
43 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
44 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
45 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
46 		if (_fs_fstab.fs_mntops == NULL)
47 			goto bad;
48 		_fs_fstab.fs_freq = 0;
49 		_fs_fstab.fs_passno = 0;
50 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
51 			_fs_fstab.fs_freq = atoi(cp);
52 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
53 				_fs_fstab.fs_passno = atoi(cp);
54 		}
55 		strcpy(subline, _fs_fstab.fs_mntops);
56 		for (typexx = 0, cp = strtok(subline, ","); cp;
57 		     cp = strtok((char *)NULL, ",")) {
58 			if (strlen(cp) != 2)
59 				continue;
60 			if (!strcmp(cp, FSTAB_RW)) {
61 				_fs_fstab.fs_type = FSTAB_RW;
62 				break;
63 			}
64 			if (!strcmp(cp, FSTAB_RQ)) {
65 				_fs_fstab.fs_type = FSTAB_RQ;
66 				break;
67 			}
68 			if (!strcmp(cp, FSTAB_RO)) {
69 				_fs_fstab.fs_type = FSTAB_RO;
70 				break;
71 			}
72 			if (!strcmp(cp, FSTAB_SW)) {
73 				_fs_fstab.fs_type = FSTAB_SW;
74 				break;
75 			}
76 			if (!strcmp(cp, FSTAB_XX)) {
77 				_fs_fstab.fs_type = FSTAB_XX;
78 				typexx++;
79 				break;
80 			}
81 		}
82 		if (typexx)
83 			continue;
84 		if (cp != NULL)
85 			return(1);
86 	bad:
87 		/* no way to distinguish between EOF and syntax error */
88 		(void)write(STDERR_FILENO, "fstab: ", 7);
89 		(void)write(STDERR_FILENO, _PATH_FSTAB,
90 		    sizeof(_PATH_FSTAB) - 1);
91 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
92 	}
93 	/* NOTREACHED */
94 }
95 
96 struct fstab *
97 getfsent()
98 {
99 	if (!_fs_fp && !setfsent() || !fstabscan())
100 		return((struct fstab *)NULL);
101 	return(&_fs_fstab);
102 }
103 
104 struct fstab *
105 getfsspec(name)
106 	register char *name;
107 {
108 	if (setfsent())
109 		while (fstabscan())
110 			if (!strcmp(_fs_fstab.fs_spec, name))
111 				return(&_fs_fstab);
112 	return((struct fstab *)NULL);
113 }
114 
115 struct fstab *
116 getfsfile(name)
117 	register char *name;
118 {
119 	if (setfsent())
120 		while (fstabscan())
121 			if (!strcmp(_fs_fstab.fs_file, name))
122 				return(&_fs_fstab);
123 	return((struct fstab *)NULL);
124 }
125 
126 setfsent()
127 {
128 	if (_fs_fp) {
129 		rewind(_fs_fp);
130 		return(1);
131 	}
132 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
133 }
134 
135 void
136 endfsent()
137 {
138 	if (_fs_fp) {
139 		(void)fclose(_fs_fp);
140 		_fs_fp = NULL;
141 	}
142 }
143