xref: /titanic_44/usr/src/lib/libbc/libc/gen/common/fstab.c (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
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 /*
23  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <fstab.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <mntent.h>
33 
34 static	struct fstab *pfs;
35 static	FILE *fs_file;
36 
37 static int
38 fstabscan(struct fstab *fs)
39 {
40 	struct mntent *mnt;
41 
42 	/* skip over all filesystem types except '4.2', 'swap' & 'ignore' */
43 	while (((mnt = getmntent(fs_file)) != NULL) &&
44 		!((strcmp(mnt->mnt_type, MNTTYPE_42) == 0) ||
45 		  (strcmp(mnt->mnt_type, MNTTYPE_SWAP) == 0) ||
46 		  (strcmp(mnt->mnt_type, MNTTYPE_IGNORE) == 0)))
47                 continue;
48 	if (mnt == NULL)
49 		return (EOF);
50 	fs->fs_spec = mnt->mnt_fsname;
51 	fs->fs_file = mnt->mnt_dir;
52 	if (strcmp(mnt->mnt_type, MNTTYPE_IGNORE) == 0) {
53 		strcpy(mnt->mnt_opts, FSTAB_XX);
54 	} else if (strcmp(mnt->mnt_type, MNTTYPE_SWAP) == 0) {
55 		strcpy(mnt->mnt_opts, FSTAB_SW);
56 	} else if (hasmntopt(mnt, MNTOPT_RO)) {
57 		strcpy(mnt->mnt_opts, FSTAB_RO);
58 	} else if (hasmntopt(mnt, MNTOPT_QUOTA)) {
59 		strcpy(mnt->mnt_opts, FSTAB_RQ);
60 	} else {
61 		strcpy(mnt->mnt_opts, FSTAB_RW);
62 	}
63 	fs->fs_type = mnt->mnt_opts;
64 	fs->fs_freq = mnt->mnt_freq;
65 	fs->fs_passno = mnt->mnt_passno;
66 	return (5);
67 }
68 
69 int
70 setfsent(void)
71 {
72 
73 	if (fs_file)
74 		endfsent();
75 	if ((fs_file = setmntent(FSTAB, "r")) == NULL) {
76 		fs_file = 0;
77 		return (0);
78 	}
79 	return (1);
80 }
81 
82 int
83 endfsent(void)
84 {
85 
86 	if (fs_file) {
87 		endmntent(fs_file);
88 		fs_file = 0;
89 	}
90 	return (1);
91 }
92 
93 struct fstab *
94 getfsent(void)
95 {
96 	int nfields;
97 
98 	if ((fs_file == 0) && (setfsent() == 0))
99 		return ((struct fstab *)0);
100 	if (pfs == 0) {
101 		pfs = (struct fstab *)malloc(sizeof (struct fstab));
102 		if (pfs == 0)
103 			return (0);
104 	}
105 	nfields = fstabscan(pfs);
106 	if (nfields == EOF || nfields != 5)
107 		return ((struct fstab *)0);
108 	return (pfs);
109 }
110 
111 struct fstab *
112 getfsspec(char *name)
113 {
114 	struct fstab *fsp;
115 
116 	if (setfsent() == 0)	/* start from the beginning */
117 		return ((struct fstab *)0);
118 	while((fsp = getfsent()) != 0)
119 		if (strcmp(fsp->fs_spec, name) == 0)
120 			return (fsp);
121 	return ((struct fstab *)0);
122 }
123 
124 struct fstab *
125 getfsfile(char *name)
126 {
127 	struct fstab *fsp;
128 
129 	if (setfsent() == 0)	/* start from the beginning */
130 		return ((struct fstab *)0);
131 	while ((fsp = getfsent()) != 0)
132 		if (strcmp(fsp->fs_file, name) == 0)
133 			return (fsp);
134 	return ((struct fstab *)0);
135 }
136 
137 struct fstab *
138 getfstype(char *type)
139 {
140 	struct fstab *fs;
141 
142 	if (setfsent() == 0)
143 		return ((struct fstab *)0);
144 	while ((fs = getfsent()) != 0)
145 		if (strcmp(fs->fs_type, type) == 0)
146 			return (fs);
147 	return ((struct fstab *)0);
148 }
149