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