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