void __gnat_clearenv (void) { #if defined (VMS) /* not implemented */ return; #elif defined (__sun__) \ || (defined (__vxworks) && ! defined (__RTP__)) || defined (__Lynx__) \ || defined (__PikeOS__) /* On Solaris, VxWorks (not RTPs), and Lynx there is no system call to unset a variable or to clear the environment so set all the entries in the environ table to NULL (see comment in __gnat_unsetenv for more explanation). */ char **env = __gnat_environ (); int index = 0; while (env[index] != NULL) { env[index]=NULL; index++; } #elif defined (__MINGW32__) || defined (__FreeBSD__) || defined (__APPLE__) \ || (defined (__vxworks) && defined (__RTP__)) || defined (__CYGWIN__) \ || defined (__NetBSD__) || defined (__OpenBSD__) || defined (__rtems__) \ || defined (__DragonFly__) || defined (__DJGPP__) /* On Windows, FreeBSD and MacOS there is no function to clean all the environment but there is a "clean" way to unset a variable. So go through the environ table and call __gnat_unsetenv on all entries */ char **env = __gnat_environ (); size_t size; while (env[0] != NULL) { size = 0; while (env[0][size] != '=') size++; /* create a string that contains "name" */ size++; { char *expression; expression = (char *) xmalloc (size * sizeof (char)); strncpy (expression, env[0], size); expression[size - 1] = 0; __gnat_unsetenv (expression); free (expression); } } #else clearenv (); #endif }
void __gnat_unsetenv (char *name) { #if defined (VMS) /* Not implemented */ return; #elif defined (__hpux__) || defined (sun) \ || (defined (__mips) && defined (__sgi)) \ || (defined (__vxworks) && ! defined (__RTP__)) \ || defined (_AIX) || defined (__Lynx__) /* On Solaris, HP-UX and IRIX there is no function to clear an environment variable. So we look for the variable in the environ table and delete it by setting the entry to NULL. This can clearly cause some memory leaks but free cannot be used on this context as not all strings in the environ have been allocated using malloc. To avoid this memory leak another method can be used. It consists in forcing the reallocation of all the strings in the environ table using malloc on the first call on the functions related to environment variable management. The disadvantage is that if a program makes a direct call to getenv the return string may be deallocated at some point. */ /* Note that on AIX, unsetenv is not supported on 5.1 but it is on 5.3. As we are still supporting AIX 5.1 we cannot use unsetenv */ char **env = __gnat_environ (); int index = 0; size_t size = strlen (name); while (env[index] != NULL) { if (strlen (env[index]) > size) { if (strstr (env[index], name) == env[index] && env[index][size] == '=') { #if defined (__vxworks) && ! defined (__RTP__) /* on Vxworks we are sure that the string has been allocated using malloc */ free (env[index]); #endif while (env[index] != NULL) { env[index]=env[index + 1]; index++; } } else index++; } else index++; } #elif defined (__MINGW32__) /* On Windows platform putenv ("key=") is equivalent to unsetenv (a subsequent call to getenv ("key") will return NULL and not the "\0" string */ size_t size = strlen (name) + 2; char *expression; expression = (char *) xmalloc (size * sizeof (char)); sprintf (expression, "%s=", name); putenv (expression); free (expression); #else unsetenv (name); #endif }