xref: /titanic_44/usr/src/cmd/rexd/mntent.c (revision aad98a6d8e89f8f5a62a1793da807d4bc5e5b159)
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 2005 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 int
97 mnttabscan(FILE *mnttabp, struct mnttab *mnt)
98 {
99 	static	char *line = NULL;
100 	char *cp;
101 
102 	if (line == NULL)
103 		line = (char *)malloc(BUFSIZ+1);
104 	do
105 	{
106 		cp = fgets(line, 256, mnttabp);
107 		if (cp == NULL)
108 		{
109 			return (EOF);
110 		}
111 	} while (*cp == '#');
112 	mnt->mnt_special = mntstr(&cp);
113 	if (*cp == '\0')
114 		return (1);
115 	mnt->mnt_mountp = mntstr(&cp);
116 	if (*cp == '\0')
117 		return (2);
118 	mnt->mnt_fstype = mntstr(&cp);
119 	if (*cp == '\0')
120 		return (3);
121 	mnt->mnt_mntopts = mntstr(&cp);
122 	if (*cp == '\0')
123 		return (4);
124 	mnt->mnt_time = mntstr(&cp);
125 	return (5);
126 }
127 
128 FILE *
129 setmntent(fname, flag)
130 	char *fname;
131 	char *flag;
132 {
133 	FILE *mnttabp;
134 
135 	if ((mnttabp = fopen(fname, flag)) == NULL)
136 	{
137 		return (NULL);
138 	}
139 	for (; *flag ; flag++)
140 	{
141 		if (*flag == 'w' || *flag == 'a' || *flag == '+')
142 		{
143 			if (lockf(fileno(mnttabp), LOCK_EX, 0) < 0)
144 			{
145 				fclose(mnttabp);
146 				return (NULL);
147 			}
148 			break;
149 		}
150 	}
151 	return (mnttabp);
152 }
153 
154 int
155 endmntent(mnttabp)
156 	FILE *mnttabp;
157 {
158 
159 	if (mnttabp)
160 	{
161 		fclose(mnttabp);
162 	}
163 	return (1);
164 }
165 
166 /*
167  * #ifdef	NOWAY
168  * int getmntent (mnttabp, mp)
169  * 	FILE *mnttabp;
170  * 	struct mnttab *mp;
171  * {
172  * 	int nfields;
173  *
174  * 	if (mnttabp == 0)
175  * 		return (-1);
176  *
177  * 	if (_mnt() == 0)
178  * 		return (-1);
179  *
180  * 	nfields = mnttabscan(mnttabp, mntp);
181  *
182  * 	if (nfields == EOF || nfields != 5)
183  * 		return (-1);
184  *
185  * 	mp = mntp;
186  *
187  * 	return ( 0 );
188  * }
189  * #endif
190  *
191  *
192  * #ifdef	NOWAY
193  * struct mnttab *
194  * getmntent(mnttabp)
195  * 	FILE *mnttabp;
196  * {
197  * 	int nfields;
198  *
199  * 	if (mnttabp == 0)
200  * 		return ((struct mnttab *)0);
201  * 	if (_mnt() == 0)
202  * 		return ((struct mnttab *)0);
203  * 	nfields = mnttabscan(mnttabp, mntp);
204  * 	if (nfields == EOF || nfields != 5)
205  * 		return ((struct mnttab *)0);
206  * 	return (mntp);
207  * }
208  * #endif
209  */
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 int
230 mntprtent(FILE *mnttabp, struct mnttab *mnt)
231 {
232 	fprintf(mnttabp, "%s\t%s\t%s\t%s\t%s\n",
233 	    mnt->mnt_special,
234 	    mnt->mnt_mountp,
235 	    mnt->mnt_fstype,
236 	    mnt->mnt_mntopts,
237 	    mnt->mnt_time);
238 	return(0);
239 }
240