xref: /titanic_53/usr/src/cmd/rexd/mntent.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #define	LOCK_EX		1
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static	struct mnttab *mntp = 0;
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate int getmntent(FILE *mnttabp, struct mnttab *mp);
38*7c478bd9Sstevel@tonic-gate static int mntdigit(char **p);
39*7c478bd9Sstevel@tonic-gate extern	char	*calloc();
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate struct mnttab *
42*7c478bd9Sstevel@tonic-gate _mnt()
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	if (mntp == 0)
46*7c478bd9Sstevel@tonic-gate 		mntp = (struct mnttab *)calloc(1, sizeof (struct mnttab));
47*7c478bd9Sstevel@tonic-gate 	return (mntp);
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static char *
51*7c478bd9Sstevel@tonic-gate mntstr(p)
52*7c478bd9Sstevel@tonic-gate 	register char **p;
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	char *cp = *p;
55*7c478bd9Sstevel@tonic-gate 	char *retstr;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	while (*cp && isspace(*cp))
58*7c478bd9Sstevel@tonic-gate 		cp++;
59*7c478bd9Sstevel@tonic-gate 	retstr = cp;
60*7c478bd9Sstevel@tonic-gate 	while (*cp && !isspace(*cp))
61*7c478bd9Sstevel@tonic-gate 		cp++;
62*7c478bd9Sstevel@tonic-gate 	if (*cp)
63*7c478bd9Sstevel@tonic-gate 	{
64*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
65*7c478bd9Sstevel@tonic-gate 		cp++;
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate 	*p = cp;
68*7c478bd9Sstevel@tonic-gate 	return (retstr);
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static int
72*7c478bd9Sstevel@tonic-gate mntdigit(p)
73*7c478bd9Sstevel@tonic-gate 	register char **p;
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	register int value = 0;
76*7c478bd9Sstevel@tonic-gate 	char *cp = *p;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	while (*cp && isspace(*cp))
79*7c478bd9Sstevel@tonic-gate 		cp++;
80*7c478bd9Sstevel@tonic-gate 	for (; *cp && isdigit(*cp); cp++)
81*7c478bd9Sstevel@tonic-gate 	{
82*7c478bd9Sstevel@tonic-gate 		value *= 10;
83*7c478bd9Sstevel@tonic-gate 		value += *cp - '0';
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	while (*cp && !isspace(*cp))
86*7c478bd9Sstevel@tonic-gate 		cp++;
87*7c478bd9Sstevel@tonic-gate 	if (*cp)
88*7c478bd9Sstevel@tonic-gate 	{
89*7c478bd9Sstevel@tonic-gate 		*cp = '\0';
90*7c478bd9Sstevel@tonic-gate 		cp++;
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 	*p = cp;
93*7c478bd9Sstevel@tonic-gate 	return (value);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static
97*7c478bd9Sstevel@tonic-gate mnttabscan(mnttabp, mnt)
98*7c478bd9Sstevel@tonic-gate 	FILE *mnttabp;
99*7c478bd9Sstevel@tonic-gate 	struct mnttab *mnt;
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	static	char *line = NULL;
102*7c478bd9Sstevel@tonic-gate 	char *cp;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (line == NULL)
105*7c478bd9Sstevel@tonic-gate 		line = (char *)malloc(BUFSIZ+1);
106*7c478bd9Sstevel@tonic-gate 	do
107*7c478bd9Sstevel@tonic-gate 	{
108*7c478bd9Sstevel@tonic-gate 		cp = fgets(line, 256, mnttabp);
109*7c478bd9Sstevel@tonic-gate 		if (cp == NULL)
110*7c478bd9Sstevel@tonic-gate 		{
111*7c478bd9Sstevel@tonic-gate 			return (EOF);
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 	} while (*cp == '#');
114*7c478bd9Sstevel@tonic-gate 	mnt->mnt_special = mntstr(&cp);
115*7c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
116*7c478bd9Sstevel@tonic-gate 		return (1);
117*7c478bd9Sstevel@tonic-gate 	mnt->mnt_mountp = mntstr(&cp);
118*7c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
119*7c478bd9Sstevel@tonic-gate 		return (2);
120*7c478bd9Sstevel@tonic-gate 	mnt->mnt_fstype = mntstr(&cp);
121*7c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
122*7c478bd9Sstevel@tonic-gate 		return (3);
123*7c478bd9Sstevel@tonic-gate 	mnt->mnt_mntopts = mntstr(&cp);
124*7c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
125*7c478bd9Sstevel@tonic-gate 		return (4);
126*7c478bd9Sstevel@tonic-gate 	mnt->mnt_time = mntstr(&cp);
127*7c478bd9Sstevel@tonic-gate 	return (5);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate FILE *
131*7c478bd9Sstevel@tonic-gate setmntent(fname, flag)
132*7c478bd9Sstevel@tonic-gate 	char *fname;
133*7c478bd9Sstevel@tonic-gate 	char *flag;
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	FILE *mnttabp;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if ((mnttabp = fopen(fname, flag)) == NULL)
138*7c478bd9Sstevel@tonic-gate 	{
139*7c478bd9Sstevel@tonic-gate 		return (NULL);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	for (; *flag ; flag++)
142*7c478bd9Sstevel@tonic-gate 	{
143*7c478bd9Sstevel@tonic-gate 		if (*flag == 'w' || *flag == 'a' || *flag == '+')
144*7c478bd9Sstevel@tonic-gate 		{
145*7c478bd9Sstevel@tonic-gate 			if (lockf(fileno(mnttabp), LOCK_EX, 0) < 0)
146*7c478bd9Sstevel@tonic-gate 			{
147*7c478bd9Sstevel@tonic-gate 				fclose(mnttabp);
148*7c478bd9Sstevel@tonic-gate 				return (NULL);
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 			break;
151*7c478bd9Sstevel@tonic-gate 		}
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	return (mnttabp);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate int
157*7c478bd9Sstevel@tonic-gate endmntent(mnttabp)
158*7c478bd9Sstevel@tonic-gate 	FILE *mnttabp;
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (mnttabp)
162*7c478bd9Sstevel@tonic-gate 	{
163*7c478bd9Sstevel@tonic-gate 		fclose(mnttabp);
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 	return (1);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /* #ifdef	NOWAY
169*7c478bd9Sstevel@tonic-gate /* int getmntent (mnttabp, mp)
170*7c478bd9Sstevel@tonic-gate /* 	FILE *mnttabp;
171*7c478bd9Sstevel@tonic-gate /* 	struct mnttab *mp;
172*7c478bd9Sstevel@tonic-gate /* {
173*7c478bd9Sstevel@tonic-gate /* 	int nfields;
174*7c478bd9Sstevel@tonic-gate /*
175*7c478bd9Sstevel@tonic-gate /* 	if (mnttabp == 0)
176*7c478bd9Sstevel@tonic-gate /* 		return (-1);
177*7c478bd9Sstevel@tonic-gate /*
178*7c478bd9Sstevel@tonic-gate /* 	if (_mnt() == 0)
179*7c478bd9Sstevel@tonic-gate /* 		return (-1);
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate /* 	nfields = mnttabscan(mnttabp, mntp);
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate /* 	if (nfields == EOF || nfields != 5)
184*7c478bd9Sstevel@tonic-gate /* 		return (-1);
185*7c478bd9Sstevel@tonic-gate /*
186*7c478bd9Sstevel@tonic-gate /* 	mp = mntp;
187*7c478bd9Sstevel@tonic-gate /*
188*7c478bd9Sstevel@tonic-gate /* 	return ( 0 );
189*7c478bd9Sstevel@tonic-gate /* }
190*7c478bd9Sstevel@tonic-gate /* #endif
191*7c478bd9Sstevel@tonic-gate /* */
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate /* #ifdef	NOWAY
194*7c478bd9Sstevel@tonic-gate /* struct mnttab *
195*7c478bd9Sstevel@tonic-gate /* getmntent(mnttabp)
196*7c478bd9Sstevel@tonic-gate /* 	FILE *mnttabp;
197*7c478bd9Sstevel@tonic-gate /* {
198*7c478bd9Sstevel@tonic-gate /* 	int nfields;
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate /* 	if (mnttabp == 0)
201*7c478bd9Sstevel@tonic-gate /* 		return ((struct mnttab *)0);
202*7c478bd9Sstevel@tonic-gate /* 	if (_mnt() == 0)
203*7c478bd9Sstevel@tonic-gate /* 		return ((struct mnttab *)0);
204*7c478bd9Sstevel@tonic-gate /* 	nfields = mnttabscan(mnttabp, mntp);
205*7c478bd9Sstevel@tonic-gate /* 	if (nfields == EOF || nfields != 5)
206*7c478bd9Sstevel@tonic-gate /* 		return ((struct mnttab *)0);
207*7c478bd9Sstevel@tonic-gate /* 	return (mntp);
208*7c478bd9Sstevel@tonic-gate /* }
209*7c478bd9Sstevel@tonic-gate /* #endif
210*7c478bd9Sstevel@tonic-gate /* */
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /* addmntent(mnttabp, mnt)
213*7c478bd9Sstevel@tonic-gate /* 	FILE *mnttabp;
214*7c478bd9Sstevel@tonic-gate /* 	register struct mnttab *mnt;
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate /* 	if (fseek(mnttabp, 0L, 2) < 0)
217*7c478bd9Sstevel@tonic-gate /* 		return (1);
218*7c478bd9Sstevel@tonic-gate /* 	if (mnt == (struct mnttab *)0)
219*7c478bd9Sstevel@tonic-gate /* 		return (1);
220*7c478bd9Sstevel@tonic-gate /* 	if (mnt->mnt_special == NULL || mnt->mnt_mountp  == NULL ||
221*7c478bd9Sstevel@tonic-gate /* 	    mnt->mnt_fstype   == NULL || mnt->mnt_mntopts == NULL)
222*7c478bd9Sstevel@tonic-gate /* 		return (1);
223*7c478bd9Sstevel@tonic-gate /*
224*7c478bd9Sstevel@tonic-gate /* 	mntprtent(mnttabp, mnt);
225*7c478bd9Sstevel@tonic-gate /* 	return (0);
226*7c478bd9Sstevel@tonic-gate /* }
227*7c478bd9Sstevel@tonic-gate /* */
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate static
230*7c478bd9Sstevel@tonic-gate mntprtent(mnttabp, mnt)
231*7c478bd9Sstevel@tonic-gate 	FILE *mnttabp;
232*7c478bd9Sstevel@tonic-gate 	register struct mnttab *mnt;
233*7c478bd9Sstevel@tonic-gate {
234*7c478bd9Sstevel@tonic-gate 	fprintf(mnttabp, "%s\t%s\t%s\t%s\t%s\n",
235*7c478bd9Sstevel@tonic-gate 	    mnt->mnt_special,
236*7c478bd9Sstevel@tonic-gate 	    mnt->mnt_mountp,
237*7c478bd9Sstevel@tonic-gate 	    mnt->mnt_fstype,
238*7c478bd9Sstevel@tonic-gate 	    mnt->mnt_mntopts,
239*7c478bd9Sstevel@tonic-gate 	    mnt->mnt_time);
240*7c478bd9Sstevel@tonic-gate 	return(0);
241*7c478bd9Sstevel@tonic-gate }
242