3 * putenv.c -- (un)set an envariable
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.
15 extern char **environ;
20 int m_putenv (char *, char *);
21 int unputenv (char *);
22 static int nvmatch (char *, char *);
26 m_putenv (char *name, char *value)
29 register char **ep, **nep, *cp;
31 cp = mh_xmalloc ((size_t) (strlen (name) + strlen (value) + 2));
33 sprintf (cp, "%s=%s", name, value);
35 for (ep = environ, i = 0; *ep; ep++, i++)
36 if (nvmatch (name, *ep)) {
41 nep = (char **) mh_xmalloc ((size_t) ((i + 2) * sizeof(*nep)));
43 for (ep = environ, i = 0; *ep; nep[i++] = *ep++)
57 for (ep = environ; *ep; ep++)
58 if (nvmatch (name, *ep))
63 for (nep = ep + 1; *nep; nep++)
72 nvmatch (char *s1, char *s2)
78 return (*s1 == '\0' && *--s2 == '=');