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