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