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