xref: /titanic_44/usr/src/lib/libbc/libc/gen/common/mntent.c (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
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 <stdio.h>
30 #include <ctype.h>
31 #include <mntent.h>
32 #include <sys/file.h>
33 
34 static	struct mntent *mntp;
35 char	*calloc();
36 
37 struct mntent *
38 _mnt()
39 {
40 
41 	if (mntp == 0)
42 		mntp = (struct mntent *)calloc(1, sizeof (struct mntent));
43 	return (mntp);
44 }
45 
46 static char *
47 mntstr(p)
48 	register char **p;
49 {
50 	unsigned char *cp = (unsigned char *) *p;
51 	unsigned char *retstr;
52 
53 	while (*cp && isspace(*cp))
54 		cp++;
55 	retstr = cp;
56 	while (*cp && !isspace(*cp))
57 		cp++;
58 	if (*cp) {
59 		*cp = '\0';
60 		cp++;
61 	}
62 	*p = (char *) cp;
63 	return ((char *)retstr);
64 }
65 
66 static int
67 mntdigit(p)
68 	register char **p;
69 {
70 	register int value = 0;
71 	unsigned char *cp = (unsigned char *) *p;
72 
73 	while (*cp && isspace(*cp))
74 		cp++;
75 	for (; *cp && isdigit(*cp); cp++) {
76 		value *= 10;
77 		value += *cp - '0';
78 	}
79 	while (*cp && !isspace(*cp))
80 		cp++;
81 	if (*cp) {
82 		*cp = '\0';
83 		cp++;
84 	}
85 	*p =  (char *) cp;
86 	return (value);
87 }
88 
89 static
90 mnttabscan(mnttabp, mnt)
91 	FILE *mnttabp;
92 	struct mntent *mnt;
93 {
94 	static	char *line = NULL;
95 	char *cp;
96 
97 	if (line == NULL)
98 		line = (char *)malloc(BUFSIZ+1);
99 	do {
100 		cp = fgets(line, BUFSIZ, mnttabp);
101 		if (cp == NULL) {
102 			return (EOF);
103 		}
104 	} while (*cp == '#');
105 	mnt->mnt_fsname = mntstr(&cp);
106 	if (*cp == '\0')
107 		return (1);
108 	mnt->mnt_dir = mntstr(&cp);
109 	if (*cp == '\0')
110 		return (2);
111 	mnt->mnt_type = mntstr(&cp);
112 	if (*cp == '\0')
113 		return (3);
114 	mnt->mnt_opts = mntstr(&cp);
115 	if (*cp == '\0')
116 		return (4);
117 	mnt->mnt_freq = mntdigit(&cp);
118 	if (*cp == '\0')
119 		return (5);
120 	mnt->mnt_passno = mntdigit(&cp);
121 	return (6);
122 }
123 
124 FILE *
125 setmntent(fname, flag)
126 	char *fname;
127 	char *flag;
128 {
129 	FILE *mnttabp;
130 
131 	if ((mnttabp = fopen(fname, flag)) == NULL) {
132 		return (NULL);
133 	}
134 	for (; *flag ; flag++) {
135 		if (*flag == 'w' || *flag == 'a' || *flag == '+') {
136 			if (flock(fileno(mnttabp), LOCK_EX) < 0) {
137 				fclose(mnttabp);
138 				return (NULL);
139 			}
140 			break;
141 		}
142 	}
143 	return (mnttabp);
144 }
145 
146 int
147 endmntent(mnttabp)
148 	FILE *mnttabp;
149 {
150 
151 	if (mnttabp) {
152 		fclose(mnttabp);
153 	}
154 	return (1);
155 }
156 
157 struct mntent *
158 getmntent(mnttabp)
159 	FILE *mnttabp;
160 {
161 	int nfields;
162 
163 	if (mnttabp == 0)
164 		return ((struct mntent *)0);
165 	if (_mnt() == 0)
166 		return ((struct mntent *)0);
167 	nfields = mnttabscan(mnttabp, mntp);
168 	if (nfields == EOF || nfields != 6)
169 		return ((struct mntent *)0);
170 	return (mntp);
171 }
172 
173 addmntent(mnttabp, mnt)
174 	FILE *mnttabp;
175 	register struct mntent *mnt;
176 {
177 	if (fseek(mnttabp, 0L, 2) < 0)
178 		return (1);
179 	if (mnt == (struct mntent *)0)
180 		return (1);
181 	if (mnt->mnt_fsname == NULL || mnt->mnt_dir  == NULL ||
182 	    mnt->mnt_type   == NULL || mnt->mnt_opts == NULL)
183 		return (1);
184 
185 	mntprtent(mnttabp, mnt);
186 	return (0);
187 }
188 
189 static char *
190 mntopt(p)
191 	char **p;
192 {
193 	unsigned char *cp = (unsigned char *) *p;
194 	unsigned char *retstr;
195 
196 	while (*cp && isspace(*cp))
197 		cp++;
198 	retstr = cp;
199 	while (*cp && *cp != ',')
200 		cp++;
201 	if (*cp) {
202 		*cp = '\0';
203 		cp++;
204 	}
205 	*p =  (char *) cp;
206 	return ((char *)retstr);
207 }
208 
209 char *
210 hasmntopt(mnt, opt)
211 	register struct mntent *mnt;
212 	register char *opt;
213 {
214 	char *f, *opts;
215 	static char *tmpopts;
216 
217 	if (tmpopts == 0) {
218 		tmpopts = (char *)calloc(256, sizeof (char));
219 		if (tmpopts == 0)
220 			return (0);
221 	}
222 	strcpy(tmpopts, mnt->mnt_opts);
223 	opts = tmpopts;
224 	f = mntopt(&opts);
225 	for (; *f; f = mntopt(&opts)) {
226 		if (strncmp(opt, f, strlen(opt)) == 0)
227 			return (f - tmpopts + mnt->mnt_opts);
228 	}
229 	return (NULL);
230 }
231 
232 static
233 mntprtent(mnttabp, mnt)
234 	FILE *mnttabp;
235 	register struct mntent *mnt;
236 {
237 	fprintf(mnttabp, "%s %s %s %s %d %d\n",
238 	    mnt->mnt_fsname,
239 	    mnt->mnt_dir,
240 	    mnt->mnt_type,
241 	    mnt->mnt_opts,
242 	    mnt->mnt_freq,
243 	    mnt->mnt_passno);
244 	return(0);
245 }
246