Loading...
stdlib/FreeBSD/setenv.c.patch Libc-320 Libc-391
--- Libc/Libc-320/stdlib/FreeBSD/setenv.c.patch
+++ Libc/Libc-391/stdlib/FreeBSD/setenv.c.patch
@@ -1,23 +1,51 @@
---- setenv.c.orig	Mon Apr 28 16:37:26 2003
-+++ setenv.c	Tue May  6 16:55:50 2003
-@@ -40,6 +40,7 @@
+--- setenv.c.orig	2003-05-20 15:23:25.000000000 -0700
++++ setenv.c	2004-11-05 17:15:11.000000000 -0800
+@@ -40,81 +40,137 @@
  #include <stddef.h>
  #include <stdlib.h>
  #include <string.h>
 +#include <crt_externs.h>
++#include <errno.h>
  
  char *__findenv(const char *, int *);
++__private_extern__ int __setenv(const char *, const char *, int, int);
  
-@@ -54,7 +55,7 @@
+-/*
+- * setenv --
+- *	Set the value of the environmental variable "name" to be
+- *	"value".  If rewrite is set, replace any current value.
+- */
+-int
+-setenv(name, value, rewrite)
++#ifndef BUILDING_VARIANT
++__private_extern__ int
++__setenv(name, value, rewrite, copy)
+ 	const char *name;
  	const char *value;
- 	int rewrite;
+-	int rewrite;
++	int rewrite, copy;
  {
 -	extern char **environ;
 +	char ***environp = _NSGetEnviron();
  	static char **alloced;			/* if allocated space before */
  	char *c;
- 	int l_value, offset;
-@@ -73,30 +74,30 @@
+-	int l_value, offset;
++	int offset;
+ 
+-	if (*value == '=')			/* no `=' in value */
+-		++value;
+-	l_value = strlen(value);
+ 	if ((c = __findenv(name, &offset))) {	/* find if already exists */
+ 		if (!rewrite)
+ 			return (0);
+-		if (strlen(c) >= l_value) {	/* old larger; copy over */
+-			while ( (*c++ = *value++) );
+-			return (0);
+-		}
++		/* In UNIX03, we can't overwrite even if the string is long
++		 * enough, because the putenv() string is owned by the user
++		 * (ie, always malloc() a new string) */
+ 	} else {					/* create new slot */
  		int cnt;
  		char **p;
  
@@ -47,17 +75,67 @@
 +		(*environp)[cnt + 1] = NULL;
  		offset = cnt;
  	}
- 	for (c = (char *)name; *c && *c != '='; ++c);	/* no `=' in name */
+-	for (c = (char *)name; *c && *c != '='; ++c);	/* no `=' in name */
 -	if (!(environ[offset] =			/* name + `=' + value */
-+	if (!((*environp)[offset] =			/* name + `=' + value */
- 	    malloc((size_t)((int)(c - name) + l_value + 2))))
- 		return (-1);
+-	    malloc((size_t)((int)(c - name) + l_value + 2))))
+-		return (-1);
 -	for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
-+	for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
- 	for (*c++ = '='; (*c++ = *value++); );
+-	for (*c++ = '='; (*c++ = *value++); );
++	/* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
++	 * strings.  For Unix03 putenv(), we put the string directly in
++	 * the environment. */
++	if (copy) {
++		for (c = (char *)name; *c && *c != '='; ++c);	/* no `=' in name */
++		if (!((*environp)[offset] =			/* name + `=' + value */
++		    malloc((size_t)((int)(c - name) + strlen(value) + 2))))
++			return (-1);
++		for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
++		for (*c++ = '='; (*c++ = *value++); );
++	} else
++		(*environp)[offset] = name;
  	return (0);
  }
-@@ -109,7 +110,7 @@
++#endif /* !BUILD_VARIANT */
++
++/*
++ * setenv --
++ *	Set the value of the environmental variable "name" to be
++ *	"value".  If rewrite is set, replace any current value.
++ */
++int
++setenv(name, value, rewrite)
++	const char *name;
++	const char *value;
++	int rewrite;
++{
++	/* no null ptr or empty str */
++	if(name == NULL || *name == 0) {
++		errno = EINVAL;
++		return (-1);
++	}
++
++#if __DARWIN_UNIX03
++	/* no '=' in name */
++	if (strchr(name, '=')) {
++		errno = EINVAL;
++		return (-1);
++	}
++#endif /* __DARWIN_UNIX03 */
++
++	if (*value == '=')			/* no `=' in value */
++		++value;
++	return (__setenv(name, value, rewrite, 1));
++}
+ 
+ /*
+  * unsetenv(name) --
+  *	Delete environmental variable "name".
+  */
++#if __DARWIN_UNIX03
++int
++#else /* !__DARWIN_UNIX03 */
+ void
++#endif /* __DARWIN_UNIX03 */
  unsetenv(name)
  	const char *name;
  {
@@ -66,3 +144,28 @@
  	char **p;
  	int offset;
  
++#if __DARWIN_UNIX03
++	/* no null ptr or empty str */
++	if(name == NULL || *name == 0) {
++		errno = EINVAL;
++		return (-1);
++	}
++
++	/* no '=' in name */
++	if (strchr(name, '=')) {
++		errno = EINVAL;
++		return (-1);
++	}
++#else /* !__DARWIN_UNIX03 */
++	/* no null ptr or empty str */
++	if(name == NULL || *name == 0)
++		return;
++#endif /* __DARWIN_UNIX03 */
+ 	while (__findenv(name, &offset))	/* if set multiple times */
+ 		for (p = &environ[offset];; ++p)
+ 			if (!(*p = *(p + 1)))
+ 				break;
++#if __DARWIN_UNIX03
++	return 0;
++#endif /* __DARWIN_UNIX03 */
+ }