2 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 1991, 2001, 2006 Free Software Foundation, Inc.
5 * This file is part of the GNU C Library.
7 * The GNU C Library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * The GNU C Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with the GNU C Library; see the file COPYING.LIB. If
19 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 * Cambridge, MA 02139, USA.
23 /* Hacked slightly by jjc@jclark.com for groff. */
31 typedef size_t SIZE_T
;
33 extern char **environ
;
35 /* Put STRING, which is of the form `NAME=VALUE', in the environment. */
37 int putenv(const char *string
)
39 char *name_end
= strchr(string
, '=');
45 /* Remove the variable from the environment. */
46 size
= strlen(string
);
47 for (ep
= environ
; *ep
!= NULL
; ++ep
)
48 if (!strncmp(*ep
, string
, size
) && (*ep
)[size
] == '=')
61 for (ep
= environ
; *ep
!= NULL
; ++ep
)
62 if (!strncmp(*ep
, string
, name_end
- string
)
63 && (*ep
)[name_end
- string
] == '=')
70 static char **last_environ
= NULL
;
71 char **new_environ
= (char **) malloc((size
+ 2) * sizeof(char *));
72 if (new_environ
== NULL
)
74 (void) memcpy((PTR
) new_environ
, (PTR
) environ
, size
* sizeof(char *));
75 new_environ
[size
] = (char *) string
;
76 new_environ
[size
+ 1] = NULL
;
77 if (last_environ
!= NULL
)
78 free((PTR
) last_environ
);
79 last_environ
= new_environ
;
80 environ
= new_environ
;
83 *ep
= (char *) string
;