summary refs log tree commit diff
path: root/sysdeps/generic
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1995-12-06 00:14:32 +0000
committerRoland McGrath <roland@gnu.org>1995-12-06 00:14:32 +0000
commit196980f5117c8d38f10d64bf67eeb0924651675f (patch)
tree4e2a731a1f766ee63e1038d7f38bee3db8c78a2c /sysdeps/generic
parent77a58cad3fa0a286bd2581187a2463a762d711ba (diff)
downloadglibc-196980f5117c8d38f10d64bf67eeb0924651675f.tar.gz
glibc-196980f5117c8d38f10d64bf67eeb0924651675f.tar.xz
glibc-196980f5117c8d38f10d64bf67eeb0924651675f.zip
Updated from ../gpl2lgpl.sed /home/gd/gnu/lib/error.c
Diffstat (limited to 'sysdeps/generic')
-rw-r--r--sysdeps/generic/putenv.c80
-rw-r--r--sysdeps/generic/setenv.c77
2 files changed, 82 insertions, 75 deletions
diff --git a/sysdeps/generic/putenv.c b/sysdeps/generic/putenv.c
index 77a8393f43..06a195d06b 100644
--- a/sysdeps/generic/putenv.c
+++ b/sysdeps/generic/putenv.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1994 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1994, 1995 Free Software Foundation, Inc.
 
 This file is part of the GNU C Library.
 
@@ -17,32 +17,36 @@ License along with the GNU C Library; see the file COPYING.LIB.  If
 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 Cambridge, MA 02139, USA.  */
 
-#include <errno.h>
+#if defined (_AIX) && !defined (__GNUC__)
+ #pragma alloca
+#endif
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 
-#if defined (__GNU_LIBRARY__) || defined (HAVE_STDLIB_H)
+#if _LIBC || HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
-#if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
+#if _LIBC || HAVE_STRING_H
 #include <string.h>
 #endif
-#if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
-#include <unistd.h>
-#endif
 
-#if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
+#if !__GNU_LIBRARY__ && !HAVE_STRCHR
 #define strchr index
 #endif
-#if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
-#define memcpy(d,s,n) bcopy ((s), (d), (n))
-#endif
 
-#ifndef	HAVE_GNU_LD
-#define	__environ	environ
-#endif
+#ifndef _LIBC
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#else
+#ifdef __GNUC__
+#define alloca __builtin_alloca
+#else
+extern char *alloca ();
+#endif /* __GNUC__ */
+#endif /* HAVE_ALLOCA_H */
+#endif /* _LIBC */
 
 
 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
@@ -51,51 +55,15 @@ putenv (string)
      const char *string;
 {
   const char *const name_end = strchr (string, '=');
-  register size_t size;
-  register char **ep;
-
-  if (name_end == NULL)
-    {
-      /* Remove the variable from the environment.  */
-      size = strlen (string);
-      for (ep = __environ; *ep != NULL; ++ep)
-	if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
-	  {
-	    while (ep[1] != NULL)
-	      {
-		ep[0] = ep[1];
-		++ep;
-	      }
-	    *ep = NULL;
-	    return 0;
-	  }
-    }
-
-  size = 0;
-  for (ep = __environ; *ep != NULL; ++ep)
-    if (!strncmp (*ep, string, name_end - string) &&
-	(*ep)[name_end - string] == '=')
-      break;
-    else
-      ++size;
 
-  if (*ep == NULL)
+  if (name_end)
     {
-      static char **last_environ = NULL;
-      char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
-      if (new_environ == NULL)
-	return -1;
-      (void) memcpy ((void *) new_environ, (void *) __environ,
-		     size * sizeof (char *));
-      new_environ[size] = (char *) string;
-      new_environ[size + 1] = NULL;
-      if (last_environ != NULL)
-	free ((void *) last_environ);
-      last_environ = new_environ;
-      __environ = new_environ;
+      char *name = alloca (name_end - string + 1);
+      memcpy (name, string, name_end - string);
+      name[name_end - string] = '\0';
+      return setenv (name, string + 1, 1);
     }
-  else
-    *ep = (char *) string;
 
+  unsetenv (string);
   return 0;
 }
diff --git a/sysdeps/generic/setenv.c b/sysdeps/generic/setenv.c
index 1907a2b7e1..dc2e8b43f3 100644
--- a/sysdeps/generic/setenv.c
+++ b/sysdeps/generic/setenv.c
@@ -16,24 +16,36 @@ License along with the GNU C Library; see the file COPYING.LIB.  If
 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 Cambridge, MA 02139, USA.  */
 
-#include <ansidecl.h>
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+
+#if _LIBC || HAVE_STDLIB_H
 #include <stdlib.h>
+#endif
+#if _LIBC || HAVE_STRING_H
 #include <string.h>
+#endif
+#if _LIBC || HAVE_UNISTD_H
 #include <unistd.h>
-#include <errno.h>
+#endif
 
 #ifndef	HAVE_GNU_LD
 #define	__environ	environ
 #endif
 
 int
-DEFUN(setenv, (name, value, replace),
-      CONST char *name AND CONST char *value AND int replace)
+setenv (name, value, replace)
+     const char *name;
+     const char *value;
+     int replace;
 {
   register char **ep;
   register size_t size;
-  CONST size_t namelen = strlen (name);
-  CONST size_t vallen = strlen (value);
+  const size_t namelen = strlen (name);
+  const size_t vallen = strlen (value) + 1;
 
   size = 0;
   for (ep = __environ; *ep != NULL; ++ep)
@@ -41,38 +53,47 @@ DEFUN(setenv, (name, value, replace),
       break;
     else
       ++size;
-  
+
   if (*ep == NULL)
     {
-      static char **last_environ = NULL;
-      char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
+      static char **last_environ;
+      char **new_environ;
+      if (__environ == last_environ)
+	/* We allocated this space; we can extend it.  */
+	new_environ = (char **) realloc (last_environ,
+					 (size + 2) * sizeof (char *));
+      else
+	new_environ = (char **) malloc ((size + 2) * sizeof (char *));
+
       if (new_environ == NULL)
 	return -1;
-      (void) memcpy((PTR) new_environ, (PTR) __environ, size * sizeof(char *));
 
-      new_environ[size] = malloc (namelen + 1 + vallen + 1);
+      new_environ[size] = malloc (namelen + 1 + vallen);
       if (new_environ[size] == NULL)
 	{
-	  free (new_environ);
+	  free ((char *) new_environ);
 	  errno = ENOMEM;
 	  return -1;
 	}
+
+      if (__environ != last_environ)
+	memcpy ((char *) new_environ, (char *) __environ,
+		size * sizeof (char *));
+
       memcpy (new_environ[size], name, namelen);
       new_environ[size][namelen] = '=';
-      memcpy (&new_environ[size][namelen + 1], value, vallen + 1);
+      memcpy (&new_environ[size][namelen + 1], value, vallen);
 
       new_environ[size + 1] = NULL;
 
-      if (last_environ != NULL)
-	free ((PTR) last_environ);
-      last_environ = new_environ;
-      __environ = new_environ;
+      last_environ = __environ = new_environ;
     }
   else if (replace)
     {
       size_t len = strlen (*ep);
-      if (len < namelen + 1 + vallen)
+      if (len + 1 < namelen + 1 + vallen)
 	{
+	  /* The existing string is too short; malloc a new one.  */
 	  char *new = malloc (namelen + 1 + vallen);
 	  if (new == NULL)
 	    return -1;
@@ -80,8 +101,26 @@ DEFUN(setenv, (name, value, replace),
 	}
       memcpy (*ep, name, namelen);
       (*ep)[namelen] = '=';
-      memcpy (&(*ep)[namelen + 1], value, vallen + 1);
+      memcpy (&(*ep)[namelen + 1], value, vallen);
     }
 
   return 0;
 }
+
+void
+unsetenv (const char *name)
+{
+  const size_t len = strlen (name);
+  char **ep;
+
+  for (ep = __environ; *ep; ++ep)
+    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
+      {
+	/* Found it.  Remove this pointer by moving later ones back.  */
+	char **dp = ep;
+	do
+	  dp[0] = dp[1];
+	while (*dp++);
+	/* Continue the loop in case NAME appears again.  */
+      }
+}