xref: /freebsd/contrib/sendmail/libsm/config.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
1 /*
2  * Copyright (c) 2000-2003 Sendmail, 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.27.2.1 2003/03/06 02:16:24 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 /* SM_CONF_BROKEN_STRTOD */
176 #if SM_CONF_GETOPT
177 	"SM_CONF_GETOPT",
178 #endif /* SM_CONF_GETOPT */
179 #if SM_CONF_LDAP_MEMFREE
180 	"SM_CONF_LDAP_MEMFREE",
181 #endif /* SM_CONF_LDAP_MEMFREE */
182 #if SM_CONF_LONGLONG
183 	"SM_CONF_LONGLONG",
184 #endif /* SM_CONF_LONGLONG */
185 #if SM_CONF_MEMCHR
186 	"SM_CONF_MEMCHR",
187 #endif /* SM_CONF_MEMCHR */
188 #if SM_CONF_MSG
189 	"SM_CONF_MSG",
190 #endif /* SM_CONF_MSG */
191 #if SM_CONF_QUAD_T
192 	"SM_CONF_QUAD_T",
193 #endif /* SM_CONF_QUAD_T */
194 #if SM_CONF_SEM
195 	"SM_CONF_SEM",
196 #endif /* SM_CONF_SEM */
197 #if SM_CONF_SETITIMER
198 	"SM_CONF_SETITIMER",
199 #endif /* SM_CONF_SETITIMER */
200 #if SM_CONF_SIGSETJMP
201 	"SM_CONF_SIGSETJMP",
202 #endif /* SM_CONF_SIGSETJMP */
203 #if SM_CONF_SHM
204 	"SM_CONF_SHM",
205 #endif /* SM_CONF_SHM */
206 #if SM_CONF_SHM_DELAY
207 	"SM_CONF_SHM_DELAY",
208 #endif /* SM_CONF_SHM_DELAY */
209 #if SM_CONF_SSIZE_T
210 	"SM_CONF_SSIZE_T",
211 #endif /* SM_CONF_SSIZE_T */
212 #if SM_CONF_STDBOOL_H
213 	"SM_CONF_STDBOOL_H",
214 #endif /* SM_CONF_STDBOOL_H */
215 #if SM_CONF_STDDEF_H
216 	"SM_CONF_STDDEF_H",
217 #endif /* SM_CONF_STDDEF_H */
218 
219 #if 0
220 /* XXX this is always enabled (for now) */
221 #if SM_CONF_STRL
222 	"SM_CONF_STRL",
223 #endif /* SM_CONF_STRL */
224 #endif /* 0 */
225 
226 #if SM_CONF_SYS_CDEFS_H
227 	"SM_CONF_SYS_CDEFS_H",
228 #endif /* SM_CONF_SYS_CDEFS_H */
229 #if SM_CONF_SYSEXITS_H
230 	"SM_CONF_SYSEXITS_H",
231 #endif /* SM_CONF_SYSEXITS_H */
232 #if SM_CONF_UID_GID
233 	"SM_CONF_UID_GID",
234 #endif /* SM_CONF_UID_GID */
235 #if SM_HEAP_CHECK
236 	"SM_HEAP_CHECK",
237 #endif /* SM_HEAP_CHECK */
238 #if defined(SM_OS_NAME) && defined(__STDC__)
239 	"SM_OS=sm_os_" SM_OS_NAME,
240 #endif /* defined(SM_OS_NAME) && defined(__STDC__) */
241 #if SM_VA_STD
242 	"SM_VA_STD",
243 #endif /* SM_VA_STD */
244 	NULL
245 };
246