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