xref: /original-bsd/lib/libc/gen/fstab.c (revision 3d46ae69)
1 /*
2  * Copyright (c) 1980, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)fstab.c	5.12 (Berkeley) 09/24/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <fstab.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 
16 static FILE *_fs_fp;
17 static struct fstab _fs_fstab;
18 
19 static
20 fstabscan()
21 {
22 	register char *cp;
23 #define	MAXLINELENGTH	1024
24 	static char line[MAXLINELENGTH];
25 	char subline[MAXLINELENGTH];
26 	char *fgets(), *strtok();
27 	int typexx;
28 
29 	for (;;) {
30 		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
31 			return(0);
32 /* OLD_STYLE_FSTAB */
33 		if (!strpbrk(cp, " \t")) {
34 			_fs_fstab.fs_spec = strtok(cp, ":\n");
35 			_fs_fstab.fs_file = strtok((char *)NULL, ":\n");
36 			_fs_fstab.fs_type = strtok((char *)NULL, ":\n");
37 			if (_fs_fstab.fs_type) {
38 				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
39 					continue;
40 				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
41 				_fs_fstab.fs_vfstype =
42 				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
43 				    "ufs" : "swap";
44 				if (cp = strtok((char *)NULL, ":\n")) {
45 					_fs_fstab.fs_freq = atoi(cp);
46 					if (cp = strtok((char *)NULL, ":\n")) {
47 						_fs_fstab.fs_passno = atoi(cp);
48 						return(1);
49 					}
50 				}
51 			}
52 			goto bad;
53 		}
54 /* OLD_STYLE_FSTAB */
55 		_fs_fstab.fs_spec = strtok(cp, " \t\n");
56 		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
57 			continue;
58 		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
59 		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
60 		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
61 		if (_fs_fstab.fs_mntops == NULL)
62 			goto bad;
63 		_fs_fstab.fs_freq = 0;
64 		_fs_fstab.fs_passno = 0;
65 		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
66 			_fs_fstab.fs_freq = atoi(cp);
67 			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
68 				_fs_fstab.fs_passno = atoi(cp);
69 		}
70 		strcpy(subline, _fs_fstab.fs_mntops);
71 		for (typexx = 0, cp = strtok(subline, ","); cp;
72 		     cp = strtok((char *)NULL, ",")) {
73 			if (strlen(cp) != 2)
74 				continue;
75 			if (!strcmp(cp, FSTAB_RW)) {
76 				_fs_fstab.fs_type = FSTAB_RW;
77 				break;
78 			}
79 			if (!strcmp(cp, FSTAB_RQ)) {
80 				_fs_fstab.fs_type = FSTAB_RQ;
81 				break;
82 			}
83 			if (!strcmp(cp, FSTAB_RO)) {
84 				_fs_fstab.fs_type = FSTAB_RO;
85 				break;
86 			}
87 			if (!strcmp(cp, FSTAB_SW)) {
88 				_fs_fstab.fs_type = FSTAB_SW;
89 				break;
90 			}
91 			if (!strcmp(cp, FSTAB_XX)) {
92 				_fs_fstab.fs_type = FSTAB_XX;
93 				typexx++;
94 				break;
95 			}
96 		}
97 		if (typexx)
98 			continue;
99 		if (cp != NULL)
100 			return(1);
101 
102 bad:		/* no way to distinguish between EOF and syntax error */
103 		(void)write(STDERR_FILENO, "fstab: ", 7);
104 		(void)write(STDERR_FILENO, _PATH_FSTAB,
105 		    sizeof(_PATH_FSTAB) - 1);
106 		(void)write(STDERR_FILENO, ": syntax error.\n", 16);
107 	}
108 	/* NOTREACHED */
109 }
110 
111 struct fstab *
112 getfsent()
113 {
114 	if (!_fs_fp && !setfsent() || !fstabscan())
115 		return((struct fstab *)NULL);
116 	return(&_fs_fstab);
117 }
118 
119 struct fstab *
120 getfsspec(name)
121 	register char *name;
122 {
123 	if (setfsent())
124 		while (fstabscan())
125 			if (!strcmp(_fs_fstab.fs_spec, name))
126 				return(&_fs_fstab);
127 	return((struct fstab *)NULL);
128 }
129 
130 struct fstab *
131 getfsfile(name)
132 	register char *name;
133 {
134 	if (setfsent())
135 		while (fstabscan())
136 			if (!strcmp(_fs_fstab.fs_file, name))
137 				return(&_fs_fstab);
138 	return((struct fstab *)NULL);
139 }
140 
141 setfsent()
142 {
143 	if (_fs_fp) {
144 		rewind(_fs_fp);
145 		return(1);
146 	}
147 	return((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL);
148 }
149 
150 void
151 endfsent()
152 {
153 	if (_fs_fp) {
154 		(void)fclose(_fs_fp);
155 		_fs_fp = NULL;
156 	}
157 }
158