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