xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/config.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include	<stdlib.h>
29*0Sstevel@tonic-gate #include	<sys/types.h>
30*0Sstevel@tonic-gate #include	<string.h>
31*0Sstevel@tonic-gate #include	"rtc.h"
32*0Sstevel@tonic-gate #include	"_conv.h"
33*0Sstevel@tonic-gate #include	"config_msg.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #define	MODESZ	MSG_GBL_OSQBRKT_SIZE + \
36*0Sstevel@tonic-gate 		MSG_CONF_EDLIBPATH_SIZE + \
37*0Sstevel@tonic-gate 		MSG_CONF_ADLIBPATH_SIZE + \
38*0Sstevel@tonic-gate 		MSG_CONF_ESLIBPATH_SIZE + \
39*0Sstevel@tonic-gate 		MSG_CONF_ASLIBPATH_SIZE + \
40*0Sstevel@tonic-gate 		MSG_CONF_DIRCFG_SIZE + \
41*0Sstevel@tonic-gate 		MSG_CONF_OBJALT_SIZE + \
42*0Sstevel@tonic-gate 		MSG_CONF_ENVS_SIZE + \
43*0Sstevel@tonic-gate 		MSG_GBL_CSQBRKT_SIZE
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * String conversion routine for configuration file information.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate const char *
49*0Sstevel@tonic-gate conv_config_str(int feature)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	static	char	string[MODESZ] = { '\0' };
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	if (feature & CONF_EDLIBPATH)
56*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_EDLIBPATH));
57*0Sstevel@tonic-gate 	if (feature & CONF_ESLIBPATH)
58*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_ESLIBPATH));
59*0Sstevel@tonic-gate 	if (feature & CONF_ADLIBPATH)
60*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_ADLIBPATH));
61*0Sstevel@tonic-gate 	if (feature & CONF_ASLIBPATH)
62*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_ASLIBPATH));
63*0Sstevel@tonic-gate 	if (feature & CONF_DIRCFG)
64*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_DIRCFG));
65*0Sstevel@tonic-gate 	if (feature & CONF_OBJALT)
66*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_OBJALT));
67*0Sstevel@tonic-gate 	if (feature & CONF_MEMRESV)
68*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_MEMRESV));
69*0Sstevel@tonic-gate 	if (feature & CONF_ENVS)
70*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_ENVS));
71*0Sstevel@tonic-gate 	if (feature & CONF_FLTR)
72*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_FLTR));
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	(void) strcat(string, MSG_ORIG(MSG_GBL_CSQBRKT));
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	return ((const char *)string);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
80*0Sstevel@tonic-gate 		MSG_CONF_DIRENT_SIZE + \
81*0Sstevel@tonic-gate 		MSG_CONF_NOEXIST_SIZE + \
82*0Sstevel@tonic-gate 		MSG_CONF_ALLENTS_SIZE + \
83*0Sstevel@tonic-gate 		MSG_CONF_EXEC_SIZE + \
84*0Sstevel@tonic-gate 		MSG_CONF_ALTER_SIZE + \
85*0Sstevel@tonic-gate 		MSG_CONF_DUMP_SIZE + \
86*0Sstevel@tonic-gate 		MSG_CONF_REALPATH_SIZE + \
87*0Sstevel@tonic-gate 		MSG_CONF_GROUP_SIZE + \
88*0Sstevel@tonic-gate 		MSG_CONF_APP_SIZE + \
89*0Sstevel@tonic-gate 		MSG_CONF_CMDLINE_SIZE + \
90*0Sstevel@tonic-gate 		MSG_CONF_FILTER_SIZE + \
91*0Sstevel@tonic-gate 		MSG_CONF_FILTEE_SIZE + \
92*0Sstevel@tonic-gate 		MSG_GBL_CSQBRKT_SIZE
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate  * String conversion routine for object flags.
96*0Sstevel@tonic-gate  */
97*0Sstevel@tonic-gate const char *
98*0Sstevel@tonic-gate conv_config_obj(ushort_t flags)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	static char	string[FLAGSZ] = { '\0' };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	(void) strcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT));
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_DIRENT)
105*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_DIRENT));
106*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_ALLENTS)
107*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_ALLENTS));
108*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_NOEXIST)
109*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_NOEXIST));
110*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_EXEC)
111*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_EXEC));
112*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_ALTER) {
113*0Sstevel@tonic-gate 		if (flags & RTC_OBJ_OPTINAL)
114*0Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_CONF_OPTIONAL));
115*0Sstevel@tonic-gate 		else
116*0Sstevel@tonic-gate 			(void) strcat(string, MSG_ORIG(MSG_CONF_ALTER));
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_DUMP)
119*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_DUMP));
120*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_REALPTH)
121*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_REALPATH));
122*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_GROUP)
123*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_GROUP));
124*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_APP)
125*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_APP));
126*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_CMDLINE)
127*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_CMDLINE));
128*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_FILTER)
129*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_FILTER));
130*0Sstevel@tonic-gate 	if (flags & RTC_OBJ_FILTEE)
131*0Sstevel@tonic-gate 		(void) strcat(string, MSG_ORIG(MSG_CONF_FILTEE));
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	(void) strcat(string, MSG_ORIG(MSG_GBL_CSQBRKT));
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	if (strlen(string) == (MSG_GBL_OSQBRKT_SIZE + MSG_GBL_CSQBRKT_SIZE))
136*0Sstevel@tonic-gate 		return (MSG_ORIG(MSG_GBL_NULL));
137*0Sstevel@tonic-gate 	else
138*0Sstevel@tonic-gate 		return ((const char *)string);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * Determine whether and old pathname exists within a search path string,
143*0Sstevel@tonic-gate  * without a new pathname, i.e., does the search path string contain "/usr/lib"
144*0Sstevel@tonic-gate  * but not "/lib".  If so, add the new pathname before the old pathname.  For
145*0Sstevel@tonic-gate  * example, convert:
146*0Sstevel@tonic-gate  *
147*0Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/usr/lib
148*0Sstevel@tonic-gate  * to:
149*0Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate const char *
152*0Sstevel@tonic-gate conv_upm_string(const char *str, const char *old, const char *new,
153*0Sstevel@tonic-gate     size_t newlen)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	const char	*curstr, *ptr;
156*0Sstevel@tonic-gate 	const char	*curold = 0, *curnew = 0;
157*0Sstevel@tonic-gate 	const char	*ptrold = old, * ptrnew = new;
158*0Sstevel@tonic-gate 	int		chkold = 1, chknew = 1;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	for (curstr = ptr = str; *ptr; ptr++) {
161*0Sstevel@tonic-gate 		if (*ptr == ':') {
162*0Sstevel@tonic-gate 			/*
163*0Sstevel@tonic-gate 			 * We've come to the end of a token within the string.
164*0Sstevel@tonic-gate 			 */
165*0Sstevel@tonic-gate 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
166*0Sstevel@tonic-gate 				/*
167*0Sstevel@tonic-gate 				 * If the old or new string checking is still
168*0Sstevel@tonic-gate 				 * enabled, we've found a match.
169*0Sstevel@tonic-gate 				 */
170*0Sstevel@tonic-gate 				if (chkold)
171*0Sstevel@tonic-gate 					curold = curstr;
172*0Sstevel@tonic-gate 				if (chknew)
173*0Sstevel@tonic-gate 					curnew = curstr;
174*0Sstevel@tonic-gate 			}
175*0Sstevel@tonic-gate 			curstr = (char *)(ptr + 1);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 			/*
178*0Sstevel@tonic-gate 			 * If an old or new string hasn't yet been matched,
179*0Sstevel@tonic-gate 			 * re-enable the checking for either.
180*0Sstevel@tonic-gate 			 */
181*0Sstevel@tonic-gate 			if (curold == 0) {
182*0Sstevel@tonic-gate 				ptrold = old;
183*0Sstevel@tonic-gate 				chkold = 1;
184*0Sstevel@tonic-gate 			}
185*0Sstevel@tonic-gate 			if (curnew == 0) {
186*0Sstevel@tonic-gate 				ptrnew = new;
187*0Sstevel@tonic-gate 				chknew = 1;
188*0Sstevel@tonic-gate 			}
189*0Sstevel@tonic-gate 			continue;
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		/*
193*0Sstevel@tonic-gate 		 * Determine if the current token matches the old or new string.
194*0Sstevel@tonic-gate 		 * If not, disable the checking for each string.
195*0Sstevel@tonic-gate 		 */
196*0Sstevel@tonic-gate 		if (chkold && (*ptr != *ptrold++))
197*0Sstevel@tonic-gate 			chkold = 0;
198*0Sstevel@tonic-gate 		if (chknew && (*ptr != *ptrnew++))
199*0Sstevel@tonic-gate 			chknew = 0;
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/*
203*0Sstevel@tonic-gate 	 * We've come to the end of the string, if the old or new string
204*0Sstevel@tonic-gate 	 * checking is still enabled, we've found a match.
205*0Sstevel@tonic-gate 	 */
206*0Sstevel@tonic-gate 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
207*0Sstevel@tonic-gate 		if (chkold)
208*0Sstevel@tonic-gate 			curold = curstr;
209*0Sstevel@tonic-gate 		if (chknew)
210*0Sstevel@tonic-gate 			curnew = curstr;
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	/*
214*0Sstevel@tonic-gate 	 * If an old string hasn't been found, or it has and a new string has
215*0Sstevel@tonic-gate 	 * been found, return the original string.
216*0Sstevel@tonic-gate 	 */
217*0Sstevel@tonic-gate 	if ((curold == 0) || curnew)
218*0Sstevel@tonic-gate 		return (str);
219*0Sstevel@tonic-gate 	else {
220*0Sstevel@tonic-gate 		char	*newstr;
221*0Sstevel@tonic-gate 		size_t	len;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		/*
224*0Sstevel@tonic-gate 		 * Allocate a new string, enlarged to accommodate the new string
225*0Sstevel@tonic-gate 		 * that will be inserted, and an associated separator.
226*0Sstevel@tonic-gate 		 */
227*0Sstevel@tonic-gate 		if ((curstr = malloc(newlen + 2 +
228*0Sstevel@tonic-gate 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
229*0Sstevel@tonic-gate 			return (str);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		newstr = (char *)curstr;
232*0Sstevel@tonic-gate 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
233*0Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* copy up to */
234*0Sstevel@tonic-gate 							/*    insertion point */
235*0Sstevel@tonic-gate 		for (len = newlen; len; len--)
236*0Sstevel@tonic-gate 			*(newstr++) = *(new++);		/* add new string and */
237*0Sstevel@tonic-gate 		*(newstr++) = ':';			/*    separator */
238*0Sstevel@tonic-gate 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
239*0Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* add remaining */
240*0Sstevel@tonic-gate 		*(newstr++) = '\0';			/*	string */
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		return (curstr);
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate }
245