]> diplodocus.org Git - nmh/blob - sbr/putenv.c
d8fb56f3d5d91b4b4fa756d8e5fcb21c9ffd0d8e
[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 unputenv (char *);
17 static int nvmatch (char *, char *);
18
19 /* FIXME: These functions leak memory. No easy fix since they might not
20 * be malloc'd. Switch to setenv(3) and unsetenv(3). */
21
22 int
23 unputenv (char *name)
24 {
25 char **ep, **nep;
26
27 for (ep = environ; *ep; ep++)
28 if (nvmatch (name, *ep))
29 break;
30 if (*ep == NULL)
31 return 1;
32
33 for (nep = ep + 1; *nep; nep++)
34 continue;
35 *ep = *--nep;
36 *nep = NULL;
37 return 0;
38 }
39
40
41 static int
42 nvmatch (char *s1, char *s2)
43 {
44 while (*s1 == *s2++)
45 if (*s1++ == '=')
46 return 1;
47
48 return (*s1 == '\0' && *--s2 == '=');
49 }