xref: /titanic_44/usr/src/cmd/sendmail/libsm/config.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2003 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: config.c,v 1.30 2003/12/10 03:19:07 gshapiro Exp $")
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
17*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
18*7c478bd9Sstevel@tonic-gate #include <sm/string.h>
19*7c478bd9Sstevel@tonic-gate #include <sm/conf.h>
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate /*
22*7c478bd9Sstevel@tonic-gate **  PUTENV -- emulation of putenv() in terms of setenv()
23*7c478bd9Sstevel@tonic-gate **
24*7c478bd9Sstevel@tonic-gate **	Not needed on Posix-compliant systems.
25*7c478bd9Sstevel@tonic-gate **	This doesn't have full Posix semantics, but it's good enough
26*7c478bd9Sstevel@tonic-gate **		for sendmail.
27*7c478bd9Sstevel@tonic-gate **
28*7c478bd9Sstevel@tonic-gate **	Parameter:
29*7c478bd9Sstevel@tonic-gate **		env -- the environment to put.
30*7c478bd9Sstevel@tonic-gate **
31*7c478bd9Sstevel@tonic-gate **	Returns:
32*7c478bd9Sstevel@tonic-gate **		0 on success, < 0 on failure.
33*7c478bd9Sstevel@tonic-gate */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #if NEEDPUTENV
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate # if NEEDPUTENV == 2		/* no setenv(3) call available */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate int
40*7c478bd9Sstevel@tonic-gate putenv(str)
41*7c478bd9Sstevel@tonic-gate 	char *str;
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	char **current;
44*7c478bd9Sstevel@tonic-gate 	int matchlen, envlen = 0;
45*7c478bd9Sstevel@tonic-gate 	char *tmp;
46*7c478bd9Sstevel@tonic-gate 	char **newenv;
47*7c478bd9Sstevel@tonic-gate 	static bool first = true;
48*7c478bd9Sstevel@tonic-gate 	extern char **environ;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	/*
51*7c478bd9Sstevel@tonic-gate 	**  find out how much of str to match when searching
52*7c478bd9Sstevel@tonic-gate 	**  for a string to replace.
53*7c478bd9Sstevel@tonic-gate 	*/
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if ((tmp = strchr(str, '=')) == NULL || tmp == str)
56*7c478bd9Sstevel@tonic-gate 		matchlen = strlen(str);
57*7c478bd9Sstevel@tonic-gate 	else
58*7c478bd9Sstevel@tonic-gate 		matchlen = (int) (tmp - str);
59*7c478bd9Sstevel@tonic-gate 	++matchlen;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	/*
62*7c478bd9Sstevel@tonic-gate 	**  Search for an existing string in the environment and find the
63*7c478bd9Sstevel@tonic-gate 	**  length of environ.  If found, replace and exit.
64*7c478bd9Sstevel@tonic-gate 	*/
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	for (current = environ; *current != NULL; current++)
67*7c478bd9Sstevel@tonic-gate 	{
68*7c478bd9Sstevel@tonic-gate 		++envlen;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 		if (strncmp(str, *current, matchlen) == 0)
71*7c478bd9Sstevel@tonic-gate 		{
72*7c478bd9Sstevel@tonic-gate 			/* found it, now insert the new version */
73*7c478bd9Sstevel@tonic-gate 			*current = (char *) str;
74*7c478bd9Sstevel@tonic-gate 			return 0;
75*7c478bd9Sstevel@tonic-gate 		}
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	/*
79*7c478bd9Sstevel@tonic-gate 	**  There wasn't already a slot so add space for a new slot.
80*7c478bd9Sstevel@tonic-gate 	**  If this is our first time through, use malloc(), else realloc().
81*7c478bd9Sstevel@tonic-gate 	*/
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (first)
84*7c478bd9Sstevel@tonic-gate 	{
85*7c478bd9Sstevel@tonic-gate 		newenv = (char **) sm_malloc(sizeof(char *) * (envlen + 2));
86*7c478bd9Sstevel@tonic-gate 		if (newenv == NULL)
87*7c478bd9Sstevel@tonic-gate 			return -1;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 		first = false;
90*7c478bd9Sstevel@tonic-gate 		(void) memcpy(newenv, environ, sizeof(char *) * envlen);
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 	else
93*7c478bd9Sstevel@tonic-gate 	{
94*7c478bd9Sstevel@tonic-gate 		newenv = (char **) sm_realloc((char *) environ,
95*7c478bd9Sstevel@tonic-gate 					      sizeof(char *) * (envlen + 2));
96*7c478bd9Sstevel@tonic-gate 		if (newenv == NULL)
97*7c478bd9Sstevel@tonic-gate 			return -1;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	/* actually add in the new entry */
101*7c478bd9Sstevel@tonic-gate 	environ = newenv;
102*7c478bd9Sstevel@tonic-gate 	environ[envlen] = (char *) str;
103*7c478bd9Sstevel@tonic-gate 	environ[envlen + 1] = NULL;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	return 0;
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate # else /* NEEDPUTENV == 2 */
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate int
111*7c478bd9Sstevel@tonic-gate putenv(env)
112*7c478bd9Sstevel@tonic-gate 	char *env;
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	char *p;
115*7c478bd9Sstevel@tonic-gate 	int l;
116*7c478bd9Sstevel@tonic-gate 	char nbuf[100];
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	p = strchr(env, '=');
119*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
120*7c478bd9Sstevel@tonic-gate 		return 0;
121*7c478bd9Sstevel@tonic-gate 	l = p - env;
122*7c478bd9Sstevel@tonic-gate 	if (l > sizeof nbuf - 1)
123*7c478bd9Sstevel@tonic-gate 		l = sizeof nbuf - 1;
124*7c478bd9Sstevel@tonic-gate 	memmove(nbuf, env, l);
125*7c478bd9Sstevel@tonic-gate 	nbuf[l] = '\0';
126*7c478bd9Sstevel@tonic-gate 	return setenv(nbuf, ++p, 1);
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate # endif /* NEEDPUTENV == 2 */
130*7c478bd9Sstevel@tonic-gate #endif /* NEEDPUTENV */
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate **  UNSETENV -- remove a variable from the environment
133*7c478bd9Sstevel@tonic-gate **
134*7c478bd9Sstevel@tonic-gate **	Not needed on newer systems.
135*7c478bd9Sstevel@tonic-gate **
136*7c478bd9Sstevel@tonic-gate **	Parameters:
137*7c478bd9Sstevel@tonic-gate **		name -- the string name of the environment variable to be
138*7c478bd9Sstevel@tonic-gate **			deleted from the current environment.
139*7c478bd9Sstevel@tonic-gate **
140*7c478bd9Sstevel@tonic-gate **	Returns:
141*7c478bd9Sstevel@tonic-gate **		none.
142*7c478bd9Sstevel@tonic-gate **
143*7c478bd9Sstevel@tonic-gate **	Globals:
144*7c478bd9Sstevel@tonic-gate **		environ -- a pointer to the current environment.
145*7c478bd9Sstevel@tonic-gate **
146*7c478bd9Sstevel@tonic-gate **	Side Effects:
147*7c478bd9Sstevel@tonic-gate **		Modifies environ.
148*7c478bd9Sstevel@tonic-gate */
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate #if !HASUNSETENV
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate void
153*7c478bd9Sstevel@tonic-gate unsetenv(name)
154*7c478bd9Sstevel@tonic-gate 	char *name;
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	extern char **environ;
157*7c478bd9Sstevel@tonic-gate 	register char **pp;
158*7c478bd9Sstevel@tonic-gate 	int len = strlen(name);
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	for (pp = environ; *pp != NULL; pp++)
161*7c478bd9Sstevel@tonic-gate 	{
162*7c478bd9Sstevel@tonic-gate 		if (strncmp(name, *pp, len) == 0 &&
163*7c478bd9Sstevel@tonic-gate 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
164*7c478bd9Sstevel@tonic-gate 			break;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	for (; *pp != NULL; pp++)
168*7c478bd9Sstevel@tonic-gate 		*pp = pp[1];
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate #endif /* !HASUNSETENV */
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate char *SmCompileOptions[] =
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate #if SM_CONF_BROKEN_STRTOD
176*7c478bd9Sstevel@tonic-gate 	"SM_CONF_BROKEN_STRTOD",
177*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_BROKEN_STRTOD */
178*7c478bd9Sstevel@tonic-gate #if SM_CONF_GETOPT
179*7c478bd9Sstevel@tonic-gate 	"SM_CONF_GETOPT",
180*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_GETOPT */
181*7c478bd9Sstevel@tonic-gate #if SM_CONF_LDAP_INITIALIZE
182*7c478bd9Sstevel@tonic-gate 	"SM_CONF_LDAP_INITIALIZE",
183*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LDAP_INITIALIZE */
184*7c478bd9Sstevel@tonic-gate #if SM_CONF_LDAP_MEMFREE
185*7c478bd9Sstevel@tonic-gate 	"SM_CONF_LDAP_MEMFREE",
186*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LDAP_MEMFREE */
187*7c478bd9Sstevel@tonic-gate #if SM_CONF_LONGLONG
188*7c478bd9Sstevel@tonic-gate 	"SM_CONF_LONGLONG",
189*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LONGLONG */
190*7c478bd9Sstevel@tonic-gate #if SM_CONF_MEMCHR
191*7c478bd9Sstevel@tonic-gate 	"SM_CONF_MEMCHR",
192*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_MEMCHR */
193*7c478bd9Sstevel@tonic-gate #if SM_CONF_MSG
194*7c478bd9Sstevel@tonic-gate 	"SM_CONF_MSG",
195*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_MSG */
196*7c478bd9Sstevel@tonic-gate #if SM_CONF_QUAD_T
197*7c478bd9Sstevel@tonic-gate 	"SM_CONF_QUAD_T",
198*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_QUAD_T */
199*7c478bd9Sstevel@tonic-gate #if SM_CONF_SEM
200*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SEM",
201*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SEM */
202*7c478bd9Sstevel@tonic-gate #if SM_CONF_SETITIMER
203*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SETITIMER",
204*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SETITIMER */
205*7c478bd9Sstevel@tonic-gate #if SM_CONF_SIGSETJMP
206*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SIGSETJMP",
207*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SIGSETJMP */
208*7c478bd9Sstevel@tonic-gate #if SM_CONF_SHM
209*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SHM",
210*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SHM */
211*7c478bd9Sstevel@tonic-gate #if SM_CONF_SHM_DELAY
212*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SHM_DELAY",
213*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SHM_DELAY */
214*7c478bd9Sstevel@tonic-gate #if SM_CONF_SSIZE_T
215*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SSIZE_T",
216*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SSIZE_T */
217*7c478bd9Sstevel@tonic-gate #if SM_CONF_STDBOOL_H
218*7c478bd9Sstevel@tonic-gate 	"SM_CONF_STDBOOL_H",
219*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_STDBOOL_H */
220*7c478bd9Sstevel@tonic-gate #if SM_CONF_STDDEF_H
221*7c478bd9Sstevel@tonic-gate 	"SM_CONF_STDDEF_H",
222*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_STDDEF_H */
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate #if 0
225*7c478bd9Sstevel@tonic-gate /* XXX this is always enabled (for now) */
226*7c478bd9Sstevel@tonic-gate #if SM_CONF_STRL
227*7c478bd9Sstevel@tonic-gate 	"SM_CONF_STRL",
228*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_STRL */
229*7c478bd9Sstevel@tonic-gate #endif /* 0 */
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate #if SM_CONF_SYS_CDEFS_H
232*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SYS_CDEFS_H",
233*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SYS_CDEFS_H */
234*7c478bd9Sstevel@tonic-gate #if SM_CONF_SYSEXITS_H
235*7c478bd9Sstevel@tonic-gate 	"SM_CONF_SYSEXITS_H",
236*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SYSEXITS_H */
237*7c478bd9Sstevel@tonic-gate #if SM_CONF_UID_GID
238*7c478bd9Sstevel@tonic-gate 	"SM_CONF_UID_GID",
239*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_UID_GID */
240*7c478bd9Sstevel@tonic-gate #if DO_NOT_USE_STRCPY
241*7c478bd9Sstevel@tonic-gate 	"DO_NOT_USE_STRCPY",
242*7c478bd9Sstevel@tonic-gate #endif /* DO_NOT_USE_STRCPY */
243*7c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
244*7c478bd9Sstevel@tonic-gate 	"SM_HEAP_CHECK",
245*7c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
246*7c478bd9Sstevel@tonic-gate #if defined(SM_OS_NAME) && defined(__STDC__)
247*7c478bd9Sstevel@tonic-gate 	"SM_OS=sm_os_" SM_OS_NAME,
248*7c478bd9Sstevel@tonic-gate #endif /* defined(SM_OS_NAME) && defined(__STDC__) */
249*7c478bd9Sstevel@tonic-gate #if SM_VA_STD
250*7c478bd9Sstevel@tonic-gate 	"SM_VA_STD",
251*7c478bd9Sstevel@tonic-gate #endif /* SM_VA_STD */
252*7c478bd9Sstevel@tonic-gate 	NULL
253*7c478bd9Sstevel@tonic-gate };
254