86e3786e4cb9319b45c62e47bd9442bcdbef85f1
[platform/upstream/groff.git] / src / libs / libgroff / putenv.c
1 /* Copyright (C) 1991-2014  Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Hacked slightly by jjc@jclark.com for groff. */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <string.h>
24
25 #ifdef __STDC__
26 #include <stddef.h>
27 typedef void *PTR;
28 typedef size_t SIZE_T;
29 #else /* not __STDC__ */
30 typedef char *PTR;
31 typedef int SIZE_T;
32 #endif /* not __STDC__ */
33
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #else /* not HAVE_STDLIB_H */
37 PTR malloc();
38 #endif /* not HAVE_STDLIB_H */
39
40 #ifndef NULL
41 #define NULL 0
42 #endif
43
44 extern char **environ;
45
46 /* Put STRING, which is of the form `NAME=VALUE', in the environment.  */
47
48 int putenv(const char *string)
49 {
50   char *name_end = strchr(string, '=');
51   SIZE_T size;
52   char **ep;
53
54   if (name_end == NULL)
55     {
56       /* Remove the variable from the environment.  */
57       size = strlen(string);
58       for (ep = environ; *ep != NULL; ++ep)
59         if (!strncmp(*ep, string, size) && (*ep)[size] == '=')
60           {
61             while (ep[1] != NULL)
62               {
63                 ep[0] = ep[1];
64                 ++ep;
65               }
66             *ep = NULL;
67             return 0;
68           }
69     }
70
71   size = 0;
72   for (ep = environ; *ep != NULL; ++ep)
73     if (!strncmp(*ep, string, name_end - string)
74         && (*ep)[name_end - string] == '=')
75       break;
76     else
77       ++size;
78
79   if (*ep == NULL)
80     {
81       static char **last_environ = NULL;
82       char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
83       if (new_environ == NULL)
84         return -1;
85       (void) memcpy((PTR) new_environ, (PTR) environ, size * sizeof(char *));
86       new_environ[size] = (char *) string;
87       new_environ[size + 1] = NULL;
88       if (last_environ != NULL)
89         free((PTR) last_environ);
90       last_environ = new_environ;
91       environ = new_environ;
92     }
93   else
94     *ep = (char *) string;
95
96   return 0;
97 }