merge with 1.10n2
[pspp] / lib / putenv.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
4 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <ansidecl.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #ifndef HAVE_GNU_LD
27 #define __environ       environ
28 #endif
29
30 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
31 int
32 DEFUN(putenv, (string), CONST char *string)
33 {
34   CONST char *CONST name_end = strchr(string, '=');
35   register size_t size;
36   register char **ep;
37
38   if (name_end == NULL)
39     {
40       /* Remove the variable from the environment.  */
41       size = strlen(string);
42       for (ep = __environ; *ep != NULL; ++ep)
43         if (!strncmp(*ep, string, size) && (*ep)[size] == '=')
44           {
45             while (ep[1] != NULL)
46               {
47                 ep[0] = ep[1];
48                 ++ep;
49               }
50             *ep = NULL;
51             return 0;
52           }
53     }
54
55   size = 0;
56   for (ep = __environ; *ep != NULL; ++ep)
57     if (!strncmp(*ep, string, name_end - string) &&
58         (*ep)[name_end - string] == '=')
59       break;
60     else
61       ++size;
62
63   if (*ep == NULL)
64     {
65       static char **last_environ = NULL;
66       char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
67       if (new_environ == NULL)
68         return -1;
69       (void) memcpy((PTR) new_environ, (PTR) __environ, size * sizeof(char *));
70       new_environ[size] = (char *) string;
71       new_environ[size + 1] = NULL;
72       if (last_environ != NULL)
73         free((PTR) last_environ);
74       last_environ = new_environ;
75       __environ = new_environ;
76     }
77   else
78     *ep = (char *) string;
79
80   return 0;
81 }