xref: /titanic_44/usr/src/lib/libbc/libc/gen/common/fstab.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 	  /* from UCB 4.4 83/06/19 */
24 
25 #include <fstab.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <mntent.h>
29 
30 static	struct fstab *pfs;
31 static	FILE *fs_file;
32 
33 static
34 fstabscan(fs)
35 	struct fstab *fs;
36 {
37 	struct mntent *mnt;
38 
39 	/* skip over all filesystem types except '4.2', 'swap' & 'ignore' */
40 	while (((mnt = getmntent(fs_file)) != NULL) &&
41 		!((strcmp(mnt->mnt_type, MNTTYPE_42) == 0) ||
42 		  (strcmp(mnt->mnt_type, MNTTYPE_SWAP) == 0) ||
43 		  (strcmp(mnt->mnt_type, MNTTYPE_IGNORE) == 0)))
44                 continue;
45 	if (mnt == NULL)
46 		return (EOF);
47 	fs->fs_spec = mnt->mnt_fsname;
48 	fs->fs_file = mnt->mnt_dir;
49 	if (strcmp(mnt->mnt_type, MNTTYPE_IGNORE) == 0) {
50 		strcpy(mnt->mnt_opts, FSTAB_XX);
51 	} else if (strcmp(mnt->mnt_type, MNTTYPE_SWAP) == 0) {
52 		strcpy(mnt->mnt_opts, FSTAB_SW);
53 	} else if (hasmntopt(mnt, MNTOPT_RO)) {
54 		strcpy(mnt->mnt_opts, FSTAB_RO);
55 	} else if (hasmntopt(mnt, MNTOPT_QUOTA)) {
56 		strcpy(mnt->mnt_opts, FSTAB_RQ);
57 	} else {
58 		strcpy(mnt->mnt_opts, FSTAB_RW);
59 	}
60 	fs->fs_type = mnt->mnt_opts;
61 	fs->fs_freq = mnt->mnt_freq;
62 	fs->fs_passno = mnt->mnt_passno;
63 	return (5);
64 }
65 
66 setfsent()
67 {
68 
69 	if (fs_file)
70 		endfsent();
71 	if ((fs_file = setmntent(FSTAB, "r")) == NULL) {
72 		fs_file = 0;
73 		return (0);
74 	}
75 	return (1);
76 }
77 
78 endfsent()
79 {
80 
81 	if (fs_file) {
82 		endmntent(fs_file);
83 		fs_file = 0;
84 	}
85 	return (1);
86 }
87 
88 struct fstab *
89 getfsent()
90 {
91 	int nfields;
92 
93 	if ((fs_file == 0) && (setfsent() == 0))
94 		return ((struct fstab *)0);
95 	if (pfs == 0) {
96 		pfs = (struct fstab *)malloc(sizeof (struct fstab));
97 		if (pfs == 0)
98 			return (0);
99 	}
100 	nfields = fstabscan(pfs);
101 	if (nfields == EOF || nfields != 5)
102 		return ((struct fstab *)0);
103 	return (pfs);
104 }
105 
106 struct fstab *
107 getfsspec(name)
108 	char *name;
109 {
110 	register struct fstab *fsp;
111 
112 	if (setfsent() == 0)	/* start from the beginning */
113 		return ((struct fstab *)0);
114 	while((fsp = getfsent()) != 0)
115 		if (strcmp(fsp->fs_spec, name) == 0)
116 			return (fsp);
117 	return ((struct fstab *)0);
118 }
119 
120 struct fstab *
121 getfsfile(name)
122 	char *name;
123 {
124 	register struct fstab *fsp;
125 
126 	if (setfsent() == 0)	/* start from the beginning */
127 		return ((struct fstab *)0);
128 	while ((fsp = getfsent()) != 0)
129 		if (strcmp(fsp->fs_file, name) == 0)
130 			return (fsp);
131 	return ((struct fstab *)0);
132 }
133 
134 struct fstab *
135 getfstype(type)
136 	char *type;
137 {
138 	register struct fstab *fs;
139 
140 	if (setfsent() == 0)
141 		return ((struct fstab *)0);
142 	while ((fs = getfsent()) != 0)
143 		if (strcmp(fs->fs_type, type) == 0)
144 			return (fs);
145 	return ((struct fstab *)0);
146 }
147