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