xref: /illumos-gate/usr/src/cmd/sendmail/libsm/config.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
2*4aac33d3Sjbeck  * Copyright (c) 2000-2003, 2007 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #include <sm/gen.h>
12*4aac33d3Sjbeck SM_RCSID("@(#)$Id: config.c,v 1.31 2007/03/14 21:21:49 ca Exp $")
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <stdlib.h>
157c478bd9Sstevel@tonic-gate #include <sm/heap.h>
167c478bd9Sstevel@tonic-gate #include <sm/string.h>
177c478bd9Sstevel@tonic-gate #include <sm/conf.h>
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate /*
207c478bd9Sstevel@tonic-gate **  PUTENV -- emulation of putenv() in terms of setenv()
217c478bd9Sstevel@tonic-gate **
227c478bd9Sstevel@tonic-gate **	Not needed on Posix-compliant systems.
237c478bd9Sstevel@tonic-gate **	This doesn't have full Posix semantics, but it's good enough
247c478bd9Sstevel@tonic-gate **		for sendmail.
257c478bd9Sstevel@tonic-gate **
267c478bd9Sstevel@tonic-gate **	Parameter:
277c478bd9Sstevel@tonic-gate **		env -- the environment to put.
287c478bd9Sstevel@tonic-gate **
297c478bd9Sstevel@tonic-gate **	Returns:
307c478bd9Sstevel@tonic-gate **		0 on success, < 0 on failure.
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #if NEEDPUTENV
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate # if NEEDPUTENV == 2		/* no setenv(3) call available */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate int
387c478bd9Sstevel@tonic-gate putenv(str)
397c478bd9Sstevel@tonic-gate 	char *str;
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	char **current;
427c478bd9Sstevel@tonic-gate 	int matchlen, envlen = 0;
437c478bd9Sstevel@tonic-gate 	char *tmp;
447c478bd9Sstevel@tonic-gate 	char **newenv;
457c478bd9Sstevel@tonic-gate 	static bool first = true;
467c478bd9Sstevel@tonic-gate 	extern char **environ;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	/*
497c478bd9Sstevel@tonic-gate 	**  find out how much of str to match when searching
507c478bd9Sstevel@tonic-gate 	**  for a string to replace.
517c478bd9Sstevel@tonic-gate 	*/
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if ((tmp = strchr(str, '=')) == NULL || tmp == str)
547c478bd9Sstevel@tonic-gate 		matchlen = strlen(str);
557c478bd9Sstevel@tonic-gate 	else
567c478bd9Sstevel@tonic-gate 		matchlen = (int) (tmp - str);
577c478bd9Sstevel@tonic-gate 	++matchlen;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
607c478bd9Sstevel@tonic-gate 	**  Search for an existing string in the environment and find the
617c478bd9Sstevel@tonic-gate 	**  length of environ.  If found, replace and exit.
627c478bd9Sstevel@tonic-gate 	*/
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	for (current = environ; *current != NULL; current++)
657c478bd9Sstevel@tonic-gate 	{
667c478bd9Sstevel@tonic-gate 		++envlen;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 		if (strncmp(str, *current, matchlen) == 0)
697c478bd9Sstevel@tonic-gate 		{
707c478bd9Sstevel@tonic-gate 			/* found it, now insert the new version */
717c478bd9Sstevel@tonic-gate 			*current = (char *) str;
727c478bd9Sstevel@tonic-gate 			return 0;
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	**  There wasn't already a slot so add space for a new slot.
787c478bd9Sstevel@tonic-gate 	**  If this is our first time through, use malloc(), else realloc().
797c478bd9Sstevel@tonic-gate 	*/
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (first)
827c478bd9Sstevel@tonic-gate 	{
837c478bd9Sstevel@tonic-gate 		newenv = (char **) sm_malloc(sizeof(char *) * (envlen + 2));
847c478bd9Sstevel@tonic-gate 		if (newenv == NULL)
857c478bd9Sstevel@tonic-gate 			return -1;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 		first = false;
887c478bd9Sstevel@tonic-gate 		(void) memcpy(newenv, environ, sizeof(char *) * envlen);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	else
917c478bd9Sstevel@tonic-gate 	{
927c478bd9Sstevel@tonic-gate 		newenv = (char **) sm_realloc((char *) environ,
937c478bd9Sstevel@tonic-gate 					      sizeof(char *) * (envlen + 2));
947c478bd9Sstevel@tonic-gate 		if (newenv == NULL)
957c478bd9Sstevel@tonic-gate 			return -1;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* actually add in the new entry */
997c478bd9Sstevel@tonic-gate 	environ = newenv;
1007c478bd9Sstevel@tonic-gate 	environ[envlen] = (char *) str;
1017c478bd9Sstevel@tonic-gate 	environ[envlen + 1] = NULL;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return 0;
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate # else /* NEEDPUTENV == 2 */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int
1097c478bd9Sstevel@tonic-gate putenv(env)
1107c478bd9Sstevel@tonic-gate 	char *env;
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	char *p;
1137c478bd9Sstevel@tonic-gate 	int l;
1147c478bd9Sstevel@tonic-gate 	char nbuf[100];
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	p = strchr(env, '=');
1177c478bd9Sstevel@tonic-gate 	if (p == NULL)
1187c478bd9Sstevel@tonic-gate 		return 0;
1197c478bd9Sstevel@tonic-gate 	l = p - env;
1207c478bd9Sstevel@tonic-gate 	if (l > sizeof nbuf - 1)
1217c478bd9Sstevel@tonic-gate 		l = sizeof nbuf - 1;
1227c478bd9Sstevel@tonic-gate 	memmove(nbuf, env, l);
1237c478bd9Sstevel@tonic-gate 	nbuf[l] = '\0';
1247c478bd9Sstevel@tonic-gate 	return setenv(nbuf, ++p, 1);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate # endif /* NEEDPUTENV == 2 */
1287c478bd9Sstevel@tonic-gate #endif /* NEEDPUTENV */
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate **  UNSETENV -- remove a variable from the environment
1317c478bd9Sstevel@tonic-gate **
1327c478bd9Sstevel@tonic-gate **	Not needed on newer systems.
1337c478bd9Sstevel@tonic-gate **
1347c478bd9Sstevel@tonic-gate **	Parameters:
1357c478bd9Sstevel@tonic-gate **		name -- the string name of the environment variable to be
1367c478bd9Sstevel@tonic-gate **			deleted from the current environment.
1377c478bd9Sstevel@tonic-gate **
1387c478bd9Sstevel@tonic-gate **	Returns:
1397c478bd9Sstevel@tonic-gate **		none.
1407c478bd9Sstevel@tonic-gate **
1417c478bd9Sstevel@tonic-gate **	Globals:
1427c478bd9Sstevel@tonic-gate **		environ -- a pointer to the current environment.
1437c478bd9Sstevel@tonic-gate **
1447c478bd9Sstevel@tonic-gate **	Side Effects:
1457c478bd9Sstevel@tonic-gate **		Modifies environ.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #if !HASUNSETENV
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate void
unsetenv(name)1517c478bd9Sstevel@tonic-gate unsetenv(name)
1527c478bd9Sstevel@tonic-gate 	char *name;
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	extern char **environ;
1557c478bd9Sstevel@tonic-gate 	register char **pp;
1567c478bd9Sstevel@tonic-gate 	int len = strlen(name);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	for (pp = environ; *pp != NULL; pp++)
1597c478bd9Sstevel@tonic-gate 	{
1607c478bd9Sstevel@tonic-gate 		if (strncmp(name, *pp, len) == 0 &&
1617c478bd9Sstevel@tonic-gate 		    ((*pp)[len] == '=' || (*pp)[len] == '\0'))
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	for (; *pp != NULL; pp++)
1667c478bd9Sstevel@tonic-gate 		*pp = pp[1];
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #endif /* !HASUNSETENV */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate char *SmCompileOptions[] =
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate #if SM_CONF_BROKEN_STRTOD
1747c478bd9Sstevel@tonic-gate 	"SM_CONF_BROKEN_STRTOD",
1757c478bd9Sstevel@tonic-gate #endif /* SM_CONF_BROKEN_STRTOD */
1767c478bd9Sstevel@tonic-gate #if SM_CONF_GETOPT
1777c478bd9Sstevel@tonic-gate 	"SM_CONF_GETOPT",
1787c478bd9Sstevel@tonic-gate #endif /* SM_CONF_GETOPT */
1797c478bd9Sstevel@tonic-gate #if SM_CONF_LDAP_INITIALIZE
1807c478bd9Sstevel@tonic-gate 	"SM_CONF_LDAP_INITIALIZE",
1817c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LDAP_INITIALIZE */
1827c478bd9Sstevel@tonic-gate #if SM_CONF_LDAP_MEMFREE
1837c478bd9Sstevel@tonic-gate 	"SM_CONF_LDAP_MEMFREE",
1847c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LDAP_MEMFREE */
1857c478bd9Sstevel@tonic-gate #if SM_CONF_LONGLONG
1867c478bd9Sstevel@tonic-gate 	"SM_CONF_LONGLONG",
1877c478bd9Sstevel@tonic-gate #endif /* SM_CONF_LONGLONG */
1887c478bd9Sstevel@tonic-gate #if SM_CONF_MEMCHR
1897c478bd9Sstevel@tonic-gate 	"SM_CONF_MEMCHR",
1907c478bd9Sstevel@tonic-gate #endif /* SM_CONF_MEMCHR */
1917c478bd9Sstevel@tonic-gate #if SM_CONF_MSG
1927c478bd9Sstevel@tonic-gate 	"SM_CONF_MSG",
1937c478bd9Sstevel@tonic-gate #endif /* SM_CONF_MSG */
1947c478bd9Sstevel@tonic-gate #if SM_CONF_QUAD_T
1957c478bd9Sstevel@tonic-gate 	"SM_CONF_QUAD_T",
1967c478bd9Sstevel@tonic-gate #endif /* SM_CONF_QUAD_T */
1977c478bd9Sstevel@tonic-gate #if SM_CONF_SEM
1987c478bd9Sstevel@tonic-gate 	"SM_CONF_SEM",
1997c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SEM */
2007c478bd9Sstevel@tonic-gate #if SM_CONF_SETITIMER
2017c478bd9Sstevel@tonic-gate 	"SM_CONF_SETITIMER",
2027c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SETITIMER */
2037c478bd9Sstevel@tonic-gate #if SM_CONF_SIGSETJMP
2047c478bd9Sstevel@tonic-gate 	"SM_CONF_SIGSETJMP",
2057c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SIGSETJMP */
2067c478bd9Sstevel@tonic-gate #if SM_CONF_SHM
2077c478bd9Sstevel@tonic-gate 	"SM_CONF_SHM",
2087c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SHM */
2097c478bd9Sstevel@tonic-gate #if SM_CONF_SHM_DELAY
2107c478bd9Sstevel@tonic-gate 	"SM_CONF_SHM_DELAY",
2117c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SHM_DELAY */
2127c478bd9Sstevel@tonic-gate #if SM_CONF_SSIZE_T
2137c478bd9Sstevel@tonic-gate 	"SM_CONF_SSIZE_T",
2147c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SSIZE_T */
2157c478bd9Sstevel@tonic-gate #if SM_CONF_STDBOOL_H
2167c478bd9Sstevel@tonic-gate 	"SM_CONF_STDBOOL_H",
2177c478bd9Sstevel@tonic-gate #endif /* SM_CONF_STDBOOL_H */
2187c478bd9Sstevel@tonic-gate #if SM_CONF_STDDEF_H
2197c478bd9Sstevel@tonic-gate 	"SM_CONF_STDDEF_H",
2207c478bd9Sstevel@tonic-gate #endif /* SM_CONF_STDDEF_H */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #if 0
2237c478bd9Sstevel@tonic-gate /* XXX this is always enabled (for now) */
2247c478bd9Sstevel@tonic-gate #if SM_CONF_STRL
2257c478bd9Sstevel@tonic-gate 	"SM_CONF_STRL",
2267c478bd9Sstevel@tonic-gate #endif /* SM_CONF_STRL */
2277c478bd9Sstevel@tonic-gate #endif /* 0 */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #if SM_CONF_SYS_CDEFS_H
2307c478bd9Sstevel@tonic-gate 	"SM_CONF_SYS_CDEFS_H",
2317c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SYS_CDEFS_H */
2327c478bd9Sstevel@tonic-gate #if SM_CONF_SYSEXITS_H
2337c478bd9Sstevel@tonic-gate 	"SM_CONF_SYSEXITS_H",
2347c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SYSEXITS_H */
2357c478bd9Sstevel@tonic-gate #if SM_CONF_UID_GID
2367c478bd9Sstevel@tonic-gate 	"SM_CONF_UID_GID",
2377c478bd9Sstevel@tonic-gate #endif /* SM_CONF_UID_GID */
2387c478bd9Sstevel@tonic-gate #if DO_NOT_USE_STRCPY
2397c478bd9Sstevel@tonic-gate 	"DO_NOT_USE_STRCPY",
2407c478bd9Sstevel@tonic-gate #endif /* DO_NOT_USE_STRCPY */
2417c478bd9Sstevel@tonic-gate #if SM_HEAP_CHECK
2427c478bd9Sstevel@tonic-gate 	"SM_HEAP_CHECK",
2437c478bd9Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
2447c478bd9Sstevel@tonic-gate #if defined(SM_OS_NAME) && defined(__STDC__)
2457c478bd9Sstevel@tonic-gate 	"SM_OS=sm_os_" SM_OS_NAME,
2467c478bd9Sstevel@tonic-gate #endif /* defined(SM_OS_NAME) && defined(__STDC__) */
2477c478bd9Sstevel@tonic-gate #if SM_VA_STD
2487c478bd9Sstevel@tonic-gate 	"SM_VA_STD",
2497c478bd9Sstevel@tonic-gate #endif /* SM_VA_STD */
250*4aac33d3Sjbeck #if USEKSTAT
251*4aac33d3Sjbeck 	"USEKSTAT",
252*4aac33d3Sjbeck #endif /* USEKSTAT */
253*4aac33d3Sjbeck #if USEPROCMEMINFO
254*4aac33d3Sjbeck 	"USEPROCMEMINFO",
255*4aac33d3Sjbeck #endif /* USEPROCMEMINFO */
256*4aac33d3Sjbeck #if USESWAPCTL
257*4aac33d3Sjbeck 	"USESWAPCTL",
258*4aac33d3Sjbeck #endif /* USESWAPCTL */
2597c478bd9Sstevel@tonic-gate 	NULL
2607c478bd9Sstevel@tonic-gate };
261