summaryrefslogtreecommitdiffstats
path: root/src/setenv.c
blob: 90e679304f67c7240365bc51bc58ecde68cc49c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************
*     Exim - an Internet mail transport agent    *
*************************************************/

/* Copyright (c) Michael Haardt 2015
 * Copyright (c) Jeremy Harris 2015 - 2016
 * Copyright (c) The Exim Maintainers 2016 */
/* See the file NOTICE for conditions of use and distribution. */

/* This module provides (un)setenv routines for those environments
lacking them in libraries. It is #include'd by OS/os.c-foo files. */


int
setenv(const char * name, const char * val, int overwrite)
{
uschar * s;
if (Ustrchr(name, '=')) return -1;
if (overwrite || !getenv(name))
  putenv(CS string_copy_perm(string_sprintf("%s=%s", name, val), FALSE));
return 0;
}

int
unsetenv(const char *name)
{
size_t len;
const char * end;
extern char ** environ;

if (!name)
  {
  errno = EINVAL;
  return -1;
  }

if (!environ)
  return 0;

for (end = name; *end != '=' && *end; ) end++;
len = end - name;
  
/* Find name in environment and move remaining variables down.
Do not early-out in case there are duplicate names. */

for (char ** e = environ; *e; e++)
  if (strncmp(*e, name, len) == 0 && (*e)[len] == '=')
    {
    char ** sp = e;
    do *sp = sp[1]; while (*++sp);
    }

return 0;
}

/* vi: aw ai sw=2
*/
/* End of setenv.c */