1 /* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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 2 of the
7 License, or (at your option) any later version.
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.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
25 #if _LIBC || HAVE_STDLIB_H
28 #if _LIBC || HAVE_STRING_H
31 #if _LIBC || HAVE_UNISTD_H
36 #define __environ environ
40 setenv (name
, value
, replace
)
47 const size_t namelen
= strlen (name
);
48 const size_t vallen
= strlen (value
) + 1;
51 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
52 if (!strncmp (*ep
, name
, namelen
) && (*ep
)[namelen
] == '=')
59 static char **last_environ
;
61 if (__environ
== last_environ
)
62 /* We allocated this space; we can extend it. */
63 new_environ
= (char **) realloc (last_environ
,
64 (size
+ 2) * sizeof (char *));
66 new_environ
= (char **) malloc ((size
+ 2) * sizeof (char *));
68 if (new_environ
== NULL
)
71 new_environ
[size
] = malloc (namelen
+ 1 + vallen
);
72 if (new_environ
[size
] == NULL
)
74 free ((char *) new_environ
);
79 if (__environ
!= last_environ
)
80 memcpy ((char *) new_environ
, (char *) __environ
,
81 size
* sizeof (char *));
83 memcpy (new_environ
[size
], name
, namelen
);
84 new_environ
[size
][namelen
] = '=';
85 memcpy (&new_environ
[size
][namelen
+ 1], value
, vallen
);
87 new_environ
[size
+ 1] = NULL
;
89 last_environ
= __environ
= new_environ
;
93 size_t len
= strlen (*ep
);
94 if (len
+ 1 < namelen
+ 1 + vallen
)
96 /* The existing string is too short; malloc a new one. */
97 char *new = malloc (namelen
+ 1 + vallen
);
102 memcpy (*ep
, name
, namelen
);
103 (*ep
)[namelen
] = '=';
104 memcpy (&(*ep
)[namelen
+ 1], value
, vallen
);
111 unsetenv (const char *name
)
113 const size_t len
= strlen (name
);
116 for (ep
= __environ
; *ep
; ++ep
)
117 if (!strncmp (*ep
, name
, len
) && (*ep
)[len
] == '=')
119 /* Found it. Remove this pointer by moving later ones back. */
124 /* Continue the loop in case NAME appears again. */