Merge branch 'master'
[exim.git] / src / src / setenv.c
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
10 lacking them in libraries. */
11
12
13 static int
14 setenv(const char * name, const char * val, int overwrite)
15 {
16 uschar * s;
17 if (Ustrchr(name, '=')) return -1;
18 if (overwrite || !getenv(name))
19 putenv(CS string_copy_malloc(string_sprintf("%s=%s", name, val)));
20 return 0;
21 }
22
23 static int
24 unsetenv(const char *name)
25 {
26 size_t len;
27 const char * end;
28 char ** e;
29 extern char ** environ;
30
31 if (!name)
32 {
33 errno = EINVAL;
34 return -1;
35 }
36
37 if (!environ)
38 return 0;
39
40 for (end = name; *end != '=' && *end; ) end++;
41 len = end - name;
42
43 /* Find name in environment and move remaining variables down.
44 Do not early-out in case there are duplicate names. */
45
46 for (e = environ; *e; e++)
47 if (strncmp(*e, name, len) == 0 && (*e)[len] == '=')
48 {
49 char ** sp = e;
50 do *sp = sp[1]; while (*++sp);
51 }
52
53 return 0;
54 }
55
56 /* vi: aw ai sw=2
57 */
58 /* End of setenv.c */