xref: /titanic_52/usr/src/cmd/rexd/mntent.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  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #define	LOCK_EX		1
29 
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <sys/mntent.h>
33 #include <sys/mnttab.h>
34 
35 static	struct mnttab *mntp = 0;
36 
37 int getmntent(FILE *mnttabp, struct mnttab *mp);
38 static int mntdigit(char **p);
39 extern	char	*calloc();
40 
41 struct mnttab *
42 _mnt()
43 {
44 
45 	if (mntp == 0)
46 		mntp = (struct mnttab *)calloc(1, sizeof (struct mnttab));
47 	return (mntp);
48 }
49 
50 static char *
51 mntstr(p)
52 	register char **p;
53 {
54 	char *cp = *p;
55 	char *retstr;
56 
57 	while (*cp && isspace(*cp))
58 		cp++;
59 	retstr = cp;
60 	while (*cp && !isspace(*cp))
61 		cp++;
62 	if (*cp)
63 	{
64 		*cp = '\0';
65 		cp++;
66 	}
67 	*p = cp;
68 	return (retstr);
69 }
70 
71 static int
72 mntdigit(p)
73 	register char **p;
74 {
75 	register int value = 0;
76 	char *cp = *p;
77 
78 	while (*cp && isspace(*cp))
79 		cp++;
80 	for (; *cp && isdigit(*cp); cp++)
81 	{
82 		value *= 10;
83 		value += *cp - '0';
84 	}
85 	while (*cp && !isspace(*cp))
86 		cp++;
87 	if (*cp)
88 	{
89 		*cp = '\0';
90 		cp++;
91 	}
92 	*p = cp;
93 	return (value);
94 }
95 
96 static
97 mnttabscan(mnttabp, mnt)
98 	FILE *mnttabp;
99 	struct mnttab *mnt;
100 {
101 	static	char *line = NULL;
102 	char *cp;
103 
104 	if (line == NULL)
105 		line = (char *)malloc(BUFSIZ+1);
106 	do
107 	{
108 		cp = fgets(line, 256, mnttabp);
109 		if (cp == NULL)
110 		{
111 			return (EOF);
112 		}
113 	} while (*cp == '#');
114 	mnt->mnt_special = mntstr(&cp);
115 	if (*cp == '\0')
116 		return (1);
117 	mnt->mnt_mountp = mntstr(&cp);
118 	if (*cp == '\0')
119 		return (2);
120 	mnt->mnt_fstype = mntstr(&cp);
121 	if (*cp == '\0')
122 		return (3);
123 	mnt->mnt_mntopts = mntstr(&cp);
124 	if (*cp == '\0')
125 		return (4);
126 	mnt->mnt_time = mntstr(&cp);
127 	return (5);
128 }
129 
130 FILE *
131 setmntent(fname, flag)
132 	char *fname;
133 	char *flag;
134 {
135 	FILE *mnttabp;
136 
137 	if ((mnttabp = fopen(fname, flag)) == NULL)
138 	{
139 		return (NULL);
140 	}
141 	for (; *flag ; flag++)
142 	{
143 		if (*flag == 'w' || *flag == 'a' || *flag == '+')
144 		{
145 			if (lockf(fileno(mnttabp), LOCK_EX, 0) < 0)
146 			{
147 				fclose(mnttabp);
148 				return (NULL);
149 			}
150 			break;
151 		}
152 	}
153 	return (mnttabp);
154 }
155 
156 int
157 endmntent(mnttabp)
158 	FILE *mnttabp;
159 {
160 
161 	if (mnttabp)
162 	{
163 		fclose(mnttabp);
164 	}
165 	return (1);
166 }
167 
168 /* #ifdef	NOWAY
169 /* int getmntent (mnttabp, mp)
170 /* 	FILE *mnttabp;
171 /* 	struct mnttab *mp;
172 /* {
173 /* 	int nfields;
174 /*
175 /* 	if (mnttabp == 0)
176 /* 		return (-1);
177 /*
178 /* 	if (_mnt() == 0)
179 /* 		return (-1);
180 /*
181 /* 	nfields = mnttabscan(mnttabp, mntp);
182 /*
183 /* 	if (nfields == EOF || nfields != 5)
184 /* 		return (-1);
185 /*
186 /* 	mp = mntp;
187 /*
188 /* 	return ( 0 );
189 /* }
190 /* #endif
191 /* */
192 
193 /* #ifdef	NOWAY
194 /* struct mnttab *
195 /* getmntent(mnttabp)
196 /* 	FILE *mnttabp;
197 /* {
198 /* 	int nfields;
199 /*
200 /* 	if (mnttabp == 0)
201 /* 		return ((struct mnttab *)0);
202 /* 	if (_mnt() == 0)
203 /* 		return ((struct mnttab *)0);
204 /* 	nfields = mnttabscan(mnttabp, mntp);
205 /* 	if (nfields == EOF || nfields != 5)
206 /* 		return ((struct mnttab *)0);
207 /* 	return (mntp);
208 /* }
209 /* #endif
210 /* */
211 
212 /* addmntent(mnttabp, mnt)
213 /* 	FILE *mnttabp;
214 /* 	register struct mnttab *mnt;
215 /*
216 /* 	if (fseek(mnttabp, 0L, 2) < 0)
217 /* 		return (1);
218 /* 	if (mnt == (struct mnttab *)0)
219 /* 		return (1);
220 /* 	if (mnt->mnt_special == NULL || mnt->mnt_mountp  == NULL ||
221 /* 	    mnt->mnt_fstype   == NULL || mnt->mnt_mntopts == NULL)
222 /* 		return (1);
223 /*
224 /* 	mntprtent(mnttabp, mnt);
225 /* 	return (0);
226 /* }
227 /* */
228 
229 static
230 mntprtent(mnttabp, mnt)
231 	FILE *mnttabp;
232 	register struct mnttab *mnt;
233 {
234 	fprintf(mnttabp, "%s\t%s\t%s\t%s\t%s\n",
235 	    mnt->mnt_special,
236 	    mnt->mnt_mountp,
237 	    mnt->mnt_fstype,
238 	    mnt->mnt_mntopts,
239 	    mnt->mnt_time);
240 	return(0);
241 }
242