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