1 /* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
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.
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
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.
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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 #if _LIBC || HAVE_STDLIB_H
30 #if _LIBC || HAVE_STRING_H
33 #if _LIBC || HAVE_UNISTD_H
38 #define __environ environ
42 setenv (name
, value
, replace
)
49 const size_t namelen
= strlen (name
);
50 const size_t vallen
= strlen (value
) + 1;
53 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
54 if (!strncmp (*ep
, name
, namelen
) && (*ep
)[namelen
] == '=')
61 static char **last_environ
;
63 if (__environ
== last_environ
)
64 /* We allocated this space; we can extend it. */
65 new_environ
= (char **) realloc (last_environ
,
66 (size
+ 2) * sizeof (char *));
68 new_environ
= (char **) malloc ((size
+ 2) * sizeof (char *));
70 if (new_environ
== NULL
)
73 new_environ
[size
] = malloc (namelen
+ 1 + vallen
);
74 if (new_environ
[size
] == NULL
)
76 free ((char *) new_environ
);
81 if (__environ
!= last_environ
)
82 memcpy ((char *) new_environ
, (char *) __environ
,
83 size
* sizeof (char *));
85 memcpy (new_environ
[size
], name
, namelen
);
86 new_environ
[size
][namelen
] = '=';
87 memcpy (&new_environ
[size
][namelen
+ 1], value
, vallen
);
89 new_environ
[size
+ 1] = NULL
;
91 last_environ
= __environ
= new_environ
;
95 size_t len
= strlen (*ep
);
96 if (len
+ 1 < namelen
+ 1 + vallen
)
98 /* The existing string is too short; malloc a new one. */
99 char *new = malloc (namelen
+ 1 + vallen
);
104 memcpy (*ep
, name
, namelen
);
105 (*ep
)[namelen
] = '=';
106 memcpy (&(*ep
)[namelen
+ 1], value
, vallen
);
116 const size_t len
= strlen (name
);
119 for (ep
= __environ
; *ep
; ++ep
)
120 if (!strncmp (*ep
, name
, len
) && (*ep
)[len
] == '=')
122 /* Found it. Remove this pointer by moving later ones back. */
127 /* Continue the loop in case NAME appears again. */