Remove unused code
[mmh] / sbr / putenv.c
1 /*
2 ** putenv.c -- (un)set an envariable
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12 extern char **environ;
13
14 /*
15 ** prototypes
16 */
17 int m_putenv(char *, char *);
18 int unputenv(char *);
19 static int nvmatch(char *, char *);
20
21
22 int
23 m_putenv(char *name, char *value)
24 {
25         int i;
26         char **ep, **nep, *cp;
27
28         cp = mh_xcalloc(strlen(name) + strlen(value) + 2, sizeof(char));
29
30         sprintf(cp, "%s=%s", name, value);
31
32         for (ep = environ, i = 0; *ep; ep++, i++)
33                 if (nvmatch(name, *ep)) {
34                         *ep = cp;
35                         return 0;
36                 }
37
38         nep = mh_xcalloc(i + 2, sizeof(*nep));
39
40         for (ep = environ, i = 0; *ep; nep[i++] = *ep++)
41                 continue;
42         nep[i++] = cp;
43         nep[i] = NULL;
44         environ = nep;
45         return 0;
46 }
47
48
49 int
50 unputenv(char *name)
51 {
52         char **ep, **nep;
53
54         for (ep = environ; *ep; ep++)
55                 if (nvmatch(name, *ep))
56                         break;
57         if (*ep == NULL)
58                 return 1;
59
60         for (nep = ep + 1; *nep; nep++)
61                 continue;
62         *ep = *--nep;
63         *nep = NULL;
64         return 0;
65 }
66
67
68 static int
69 nvmatch(char *s1, char *s2)
70 {
71         while (*s1 == *s2++)
72                 if (*s1++ == '=')
73                         return 1;
74
75         return (*s1 == '\0' && *--s2 == '=');
76 }