Provide setenv/unsetenv for environments lacking them. Bug 1578
[exim.git] / src / src / setenv.c
CommitLineData
06cf6fdc
JH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) Michael Haardt 2015 */
6/* Copyright (c) Jeremy Harris 2015 */
7/* See the file NOTICE for conditions of use and distribution. */
8
9/* This module provides (un)setenv routines for those environments
10lacking them in libraries. */
11
12
13static int
14setenv(const char * name, const char * val, int overwrite)
15{
16uschar * s;
17if (Ustrchr(name, '=')) return -1;
18if (overwrite || !getenv(name))
19 putenv(CS string_copy_malloc(string_sprintf("%s=%s", name, val)));
20return 0;
21}
22
23static int
24unsetenv(const char *name)
25{
26size_t len;
27const char * end;
28char ** e;
29extern char ** environ;
30
31if (!name)
32 {
33 errno = EINVAL;
34 return -1;
35 }
36
37for (end = name; *end != '=' && *end; ) end++;
38len = end - name;
39
40/* Find name in environment and move remaining variables down.
41Do not early-out in case there are duplicate names. */
42
43for (e = environ; *e; e++)
44 if (strncmp(*e, name, len) == 0 && (*e)[len] == '=')
45 {
46 char ** sp = e;
47 do *sp = sp[1]; while (*++sp);
48 }
49
50return 0;
51}
52
53/* vi: aw ai sw=2
54*/
55/* End of setenv.c */