Loading...
stdlib/FreeBSD/putenv.c Libc-825.26 Libc-1669.40.2
--- Libc/Libc-825.26/stdlib/FreeBSD/putenv.c
+++ Libc/Libc-1669.40.2/stdlib/FreeBSD/putenv.c
@@ -38,7 +38,9 @@
 #include <sys/types.h>
 #include <db.h>
 #include <crt_externs.h>
-#include <errno.h> 
+#include <errno.h>
+
+#include "libc_private.h"
 
 extern struct owned_ptr *__env_owned;
 
@@ -46,8 +48,8 @@
 extern char **_saved_environ;
 #endif /* LEGACY_CRT1_ENVIRON */
 
-__private_extern__ int __init__env_owned(int);
-__private_extern__ int __setenv(const char *, const char *, int, int, char ***, struct owned_ptr *);
+__private_extern__ int __init__env_owned_locked(int);
+__private_extern__ int __setenv_locked(const char *, const char *, int, int, char ***, struct owned_ptr *);
 
 #ifndef BUILDING_VARIANT
 /*
@@ -58,18 +60,23 @@
 int
 _putenvp(char *str, char ***envp, void *state)
 {
-	if (__init__env_owned(1)) return (-1);
-	return (__setenv(str, NULL, 1, 0, envp, (state ? (struct owned_ptr *)state : __env_owned)));
+	environ_lock_np();
+	if (__init__env_owned_locked(1)) {
+		environ_unlock_np();
+		return (-1);
+	}
+	int ret = __setenv_locked(str, NULL, 1, 0, envp,
+			(state ? (struct owned_ptr *)state : __env_owned));
+	environ_unlock_np();
+	return ret;
 }
 #endif /* BUILDING_VARIANT */
 
 int
-putenv(str)
-	char *str;
+putenv(char *str)
 {
-#ifdef LEGACY_CRT1_ENVIRON
 	int ret;
-#endif /* LEGACY_CRT1_ENVIRON */
+	int copy;
 
 #if __DARWIN_UNIX03
 	if (str == NULL || *str == 0 || index(str, '=') == NULL) {
@@ -80,21 +87,22 @@
 	if (index(str, '=') == NULL)
 		return (-1);
 #endif /* __DARWIN_UNIX03 */
-	if (__init__env_owned(1)) return (-1);
-#ifdef LEGACY_CRT1_ENVIRON
-	ret =
-#else /* !LEGACY_CRT1_ENVIRON */
-	return
-#endif /* !LEGACY_CRT1_ENVIRON */
-	    __setenv(str, NULL, 1,
+
 #if __DARWIN_UNIX03
-		0,
+	copy = 0;
 #else /* !__DARWIN_UNIX03 */
-		-1,
+	copy = -1;
 #endif /* __DARWIN_UNIX03 */
-		_NSGetEnviron(), __env_owned);
+
+	environ_lock_np();
+	if (__init__env_owned_locked(1)) {
+		environ_unlock_np();
+		return (-1);
+	}
+	ret = __setenv_locked(str, NULL, 1, copy, _NSGetEnviron(), __env_owned);
 #ifdef LEGACY_CRT1_ENVIRON
 	_saved_environ = *_NSGetEnviron();
+#endif /* LEGACY_CRT1_ENVIRON */
+	environ_unlock_np();
 	return ret;
-#endif /* LEGACY_CRT1_ENVIRON */
 }