xref: /titanic_41/usr/src/cmd/rexd/mntent.c (revision 16ab6e0b56ccd36f9a870ff0b87e64c55599a6f0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
22*16ab6e0bSsm26363  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #define	LOCK_EX		1
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
337c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static	struct mnttab *mntp = 0;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate int getmntent(FILE *mnttabp, struct mnttab *mp);
387c478bd9Sstevel@tonic-gate static int mntdigit(char **p);
397c478bd9Sstevel@tonic-gate extern	char	*calloc();
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate struct mnttab *
_mnt()427c478bd9Sstevel@tonic-gate _mnt()
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	if (mntp == 0)
467c478bd9Sstevel@tonic-gate 		mntp = (struct mnttab *)calloc(1, sizeof (struct mnttab));
477c478bd9Sstevel@tonic-gate 	return (mntp);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static char *
mntstr(p)517c478bd9Sstevel@tonic-gate mntstr(p)
527c478bd9Sstevel@tonic-gate 	register char **p;
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	char *cp = *p;
557c478bd9Sstevel@tonic-gate 	char *retstr;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	while (*cp && isspace(*cp))
587c478bd9Sstevel@tonic-gate 		cp++;
597c478bd9Sstevel@tonic-gate 	retstr = cp;
607c478bd9Sstevel@tonic-gate 	while (*cp && !isspace(*cp))
617c478bd9Sstevel@tonic-gate 		cp++;
627c478bd9Sstevel@tonic-gate 	if (*cp)
637c478bd9Sstevel@tonic-gate 	{
647c478bd9Sstevel@tonic-gate 		*cp = '\0';
657c478bd9Sstevel@tonic-gate 		cp++;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	*p = cp;
687c478bd9Sstevel@tonic-gate 	return (retstr);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int
mntdigit(p)727c478bd9Sstevel@tonic-gate mntdigit(p)
737c478bd9Sstevel@tonic-gate 	register char **p;
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	register int value = 0;
767c478bd9Sstevel@tonic-gate 	char *cp = *p;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	while (*cp && isspace(*cp))
797c478bd9Sstevel@tonic-gate 		cp++;
807c478bd9Sstevel@tonic-gate 	for (; *cp && isdigit(*cp); cp++)
817c478bd9Sstevel@tonic-gate 	{
827c478bd9Sstevel@tonic-gate 		value *= 10;
837c478bd9Sstevel@tonic-gate 		value += *cp - '0';
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	while (*cp && !isspace(*cp))
867c478bd9Sstevel@tonic-gate 		cp++;
877c478bd9Sstevel@tonic-gate 	if (*cp)
887c478bd9Sstevel@tonic-gate 	{
897c478bd9Sstevel@tonic-gate 		*cp = '\0';
907c478bd9Sstevel@tonic-gate 		cp++;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	*p = cp;
937c478bd9Sstevel@tonic-gate 	return (value);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
96*16ab6e0bSsm26363 static int
mnttabscan(FILE * mnttabp,struct mnttab * mnt)97*16ab6e0bSsm26363 mnttabscan(FILE *mnttabp, struct mnttab *mnt)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	static	char *line = NULL;
1007c478bd9Sstevel@tonic-gate 	char *cp;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (line == NULL)
1037c478bd9Sstevel@tonic-gate 		line = (char *)malloc(BUFSIZ+1);
1047c478bd9Sstevel@tonic-gate 	do
1057c478bd9Sstevel@tonic-gate 	{
1067c478bd9Sstevel@tonic-gate 		cp = fgets(line, 256, mnttabp);
1077c478bd9Sstevel@tonic-gate 		if (cp == NULL)
1087c478bd9Sstevel@tonic-gate 		{
1097c478bd9Sstevel@tonic-gate 			return (EOF);
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 	} while (*cp == '#');
1127c478bd9Sstevel@tonic-gate 	mnt->mnt_special = mntstr(&cp);
1137c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
1147c478bd9Sstevel@tonic-gate 		return (1);
1157c478bd9Sstevel@tonic-gate 	mnt->mnt_mountp = mntstr(&cp);
1167c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
1177c478bd9Sstevel@tonic-gate 		return (2);
1187c478bd9Sstevel@tonic-gate 	mnt->mnt_fstype = mntstr(&cp);
1197c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
1207c478bd9Sstevel@tonic-gate 		return (3);
1217c478bd9Sstevel@tonic-gate 	mnt->mnt_mntopts = mntstr(&cp);
1227c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
1237c478bd9Sstevel@tonic-gate 		return (4);
1247c478bd9Sstevel@tonic-gate 	mnt->mnt_time = mntstr(&cp);
1257c478bd9Sstevel@tonic-gate 	return (5);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate FILE *
setmntent(fname,flag)1297c478bd9Sstevel@tonic-gate setmntent(fname, flag)
1307c478bd9Sstevel@tonic-gate 	char *fname;
1317c478bd9Sstevel@tonic-gate 	char *flag;
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	FILE *mnttabp;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if ((mnttabp = fopen(fname, flag)) == NULL)
1367c478bd9Sstevel@tonic-gate 	{
1377c478bd9Sstevel@tonic-gate 		return (NULL);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	for (; *flag ; flag++)
1407c478bd9Sstevel@tonic-gate 	{
1417c478bd9Sstevel@tonic-gate 		if (*flag == 'w' || *flag == 'a' || *flag == '+')
1427c478bd9Sstevel@tonic-gate 		{
1437c478bd9Sstevel@tonic-gate 			if (lockf(fileno(mnttabp), LOCK_EX, 0) < 0)
1447c478bd9Sstevel@tonic-gate 			{
1457c478bd9Sstevel@tonic-gate 				fclose(mnttabp);
1467c478bd9Sstevel@tonic-gate 				return (NULL);
1477c478bd9Sstevel@tonic-gate 			}
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	return (mnttabp);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
endmntent(mnttabp)1557c478bd9Sstevel@tonic-gate endmntent(mnttabp)
1567c478bd9Sstevel@tonic-gate 	FILE *mnttabp;
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (mnttabp)
1607c478bd9Sstevel@tonic-gate 	{
1617c478bd9Sstevel@tonic-gate 		fclose(mnttabp);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	return (1);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
167*16ab6e0bSsm26363  * #ifdef	NOWAY
168*16ab6e0bSsm26363  * int getmntent (mnttabp, mp)
169*16ab6e0bSsm26363  * 	FILE *mnttabp;
170*16ab6e0bSsm26363  * 	struct mnttab *mp;
171*16ab6e0bSsm26363  * {
172*16ab6e0bSsm26363  * 	int nfields;
173*16ab6e0bSsm26363  *
174*16ab6e0bSsm26363  * 	if (mnttabp == 0)
175*16ab6e0bSsm26363  * 		return (-1);
176*16ab6e0bSsm26363  *
177*16ab6e0bSsm26363  * 	if (_mnt() == 0)
178*16ab6e0bSsm26363  * 		return (-1);
179*16ab6e0bSsm26363  *
180*16ab6e0bSsm26363  * 	nfields = mnttabscan(mnttabp, mntp);
181*16ab6e0bSsm26363  *
182*16ab6e0bSsm26363  * 	if (nfields == EOF || nfields != 5)
183*16ab6e0bSsm26363  * 		return (-1);
184*16ab6e0bSsm26363  *
185*16ab6e0bSsm26363  * 	mp = mntp;
186*16ab6e0bSsm26363  *
187*16ab6e0bSsm26363  * 	return ( 0 );
188*16ab6e0bSsm26363  * }
189*16ab6e0bSsm26363  * #endif
190*16ab6e0bSsm26363  *
191*16ab6e0bSsm26363  *
192*16ab6e0bSsm26363  * #ifdef	NOWAY
193*16ab6e0bSsm26363  * struct mnttab *
194*16ab6e0bSsm26363  * getmntent(mnttabp)
195*16ab6e0bSsm26363  * 	FILE *mnttabp;
196*16ab6e0bSsm26363  * {
197*16ab6e0bSsm26363  * 	int nfields;
198*16ab6e0bSsm26363  *
199*16ab6e0bSsm26363  * 	if (mnttabp == 0)
200*16ab6e0bSsm26363  * 		return ((struct mnttab *)0);
201*16ab6e0bSsm26363  * 	if (_mnt() == 0)
202*16ab6e0bSsm26363  * 		return ((struct mnttab *)0);
203*16ab6e0bSsm26363  * 	nfields = mnttabscan(mnttabp, mntp);
204*16ab6e0bSsm26363  * 	if (nfields == EOF || nfields != 5)
205*16ab6e0bSsm26363  * 		return ((struct mnttab *)0);
206*16ab6e0bSsm26363  * 	return (mntp);
207*16ab6e0bSsm26363  * }
208*16ab6e0bSsm26363  * #endif
209*16ab6e0bSsm26363  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
212*16ab6e0bSsm26363  * addmntent(mnttabp, mnt)
213*16ab6e0bSsm26363  * 	FILE *mnttabp;
214*16ab6e0bSsm26363  * 	register struct mnttab *mnt;
215*16ab6e0bSsm26363  *
216*16ab6e0bSsm26363  * 	if (fseek(mnttabp, 0L, 2) < 0)
217*16ab6e0bSsm26363  * 		return (1);
218*16ab6e0bSsm26363  * 	if (mnt == (struct mnttab *)0)
219*16ab6e0bSsm26363  * 		return (1);
220*16ab6e0bSsm26363  * 	if (mnt->mnt_special == NULL || mnt->mnt_mountp  == NULL ||
221*16ab6e0bSsm26363  * 	    mnt->mnt_fstype   == NULL || mnt->mnt_mntopts == NULL)
222*16ab6e0bSsm26363  * 		return (1);
223*16ab6e0bSsm26363  *
224*16ab6e0bSsm26363  * 	mntprtent(mnttabp, mnt);
225*16ab6e0bSsm26363  * 	return (0);
226*16ab6e0bSsm26363  * }
227*16ab6e0bSsm26363  */
2287c478bd9Sstevel@tonic-gate 
229*16ab6e0bSsm26363 static int
mntprtent(FILE * mnttabp,struct mnttab * mnt)230*16ab6e0bSsm26363 mntprtent(FILE *mnttabp, struct mnttab *mnt)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	fprintf(mnttabp, "%s\t%s\t%s\t%s\t%s\n",
2337c478bd9Sstevel@tonic-gate 	    mnt->mnt_special,
2347c478bd9Sstevel@tonic-gate 	    mnt->mnt_mountp,
2357c478bd9Sstevel@tonic-gate 	    mnt->mnt_fstype,
2367c478bd9Sstevel@tonic-gate 	    mnt->mnt_mntopts,
2377c478bd9Sstevel@tonic-gate 	    mnt->mnt_time);
2387c478bd9Sstevel@tonic-gate 	return(0);
2397c478bd9Sstevel@tonic-gate }
240