]> diplodocus.org Git - nmh/blob - sbr/putenv.c
uip/sortm.c: Remove unused variable and dlocaltimenow() call.
[nmh] / sbr / putenv.c
1 /* putenv.c -- (un)set an envariable
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10
11 extern char **environ;
12
13 /*
14 * prototypes
15 */
16 int m_putenv (char *, char *);
17 int unputenv (char *);
18 static int nvmatch (char *, char *);
19
20 /* FIXME: These functions leak memory. No easy fix since they might not
21 * be malloc'd. Switch to setenv(3) and unsetenv(3). */
22
23 int
24 m_putenv (char *name, char *value)
25 {
26 int i;
27 char **ep, **nep, *cp;
28
29 cp = concat(name, "=", value, NULL);
30
31 for (ep = environ, i = 0; *ep; ep++, i++)
32 if (nvmatch (name, *ep)) {
33 *ep = cp;
34 return 0;
35 }
36
37 nep = (char **) mh_xmalloc ((size_t) ((i + 2) * sizeof(*nep)));
38
39 for (ep = environ, i = 0; *ep; nep[i++] = *ep++)
40 continue;
41 nep[i++] = cp;
42 nep[i] = NULL;
43 environ = nep;
44 return 0;
45 }
46
47
48 int
49 unputenv (char *name)
50 {
51 char **ep, **nep;
52
53 for (ep = environ; *ep; ep++)
54 if (nvmatch (name, *ep))
55 break;
56 if (*ep == NULL)
57 return 1;
58
59 for (nep = ep + 1; *nep; nep++)
60 continue;
61 *ep = *--nep;
62 *nep = NULL;
63 return 0;
64 }
65
66
67 static int
68 nvmatch (char *s1, char *s2)
69 {
70 while (*s1 == *s2++)
71 if (*s1++ == '=')
72 return 1;
73
74 return (*s1 == '\0' && *--s2 == '=');
75 }