Blame gnulib-tests/putenv.c

Packit 709fb3
/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2017 Free Software
Packit 709fb3
   Foundation, Inc.
Packit 709fb3
Packit 709fb3
   NOTE: The canonical source of this file is maintained with the GNU C
Packit 709fb3
   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify it
Packit 709fb3
   under the terms of the GNU General Public License as published by the
Packit 709fb3
   Free Software Foundation; either version 3 of the License, or any
Packit 709fb3
   later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
/* Specification.  */
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
Packit 709fb3
#include <stddef.h>
Packit 709fb3
Packit 709fb3
/* Include errno.h *after* sys/types.h to work around header problems
Packit 709fb3
   on AIX 3.2.5.  */
Packit 709fb3
#include <errno.h>
Packit 709fb3
#ifndef __set_errno
Packit 709fb3
# define __set_errno(ev) ((errno) = (ev))
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#include <string.h>
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
# define WIN32_LEAN_AND_MEAN
Packit 709fb3
# include <windows.h>
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if _LIBC
Packit 709fb3
# if HAVE_GNU_LD
Packit 709fb3
#  define environ __environ
Packit 709fb3
# else
Packit 709fb3
extern char **environ;
Packit 709fb3
# endif
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if _LIBC
Packit 709fb3
/* This lock protects against simultaneous modifications of 'environ'.  */
Packit 709fb3
# include <bits/libc-lock.h>
Packit 709fb3
__libc_lock_define_initialized (static, envlock)
Packit 709fb3
# define LOCK   __libc_lock_lock (envlock)
Packit 709fb3
# define UNLOCK __libc_lock_unlock (envlock)
Packit 709fb3
#else
Packit 709fb3
# define LOCK
Packit 709fb3
# define UNLOCK
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
static int
Packit 709fb3
_unsetenv (const char *name)
Packit 709fb3
{
Packit 709fb3
  size_t len;
Packit 709fb3
#if !HAVE_DECL__PUTENV
Packit 709fb3
  char **ep;
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
Packit 709fb3
    {
Packit 709fb3
      __set_errno (EINVAL);
Packit 709fb3
      return -1;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  len = strlen (name);
Packit 709fb3
Packit 709fb3
#if HAVE_DECL__PUTENV
Packit 709fb3
  {
Packit 709fb3
    int putenv_result, putenv_errno;
Packit 709fb3
    char *name_ = malloc (len + 2);
Packit 709fb3
    memcpy (name_, name, len);
Packit 709fb3
    name_[len] = '=';
Packit 709fb3
    name_[len + 1] = 0;
Packit 709fb3
    putenv_result = _putenv (name_);
Packit 709fb3
    putenv_errno = errno;
Packit 709fb3
    free (name_);
Packit 709fb3
    __set_errno (putenv_errno);
Packit 709fb3
    return putenv_result;
Packit 709fb3
  }
Packit 709fb3
#else
Packit 709fb3
Packit 709fb3
  LOCK;
Packit 709fb3
Packit 709fb3
  ep = environ;
Packit 709fb3
  while (*ep != NULL)
Packit 709fb3
    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
Packit 709fb3
      {
Packit 709fb3
        /* Found it.  Remove this pointer by moving later ones back.  */
Packit 709fb3
        char **dp = ep;
Packit 709fb3
Packit 709fb3
        do
Packit 709fb3
          dp[0] = dp[1];
Packit 709fb3
        while (*dp++);
Packit 709fb3
        /* Continue the loop in case NAME appears again.  */
Packit 709fb3
      }
Packit 709fb3
    else
Packit 709fb3
      ++ep;
Packit 709fb3
Packit 709fb3
  UNLOCK;
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
Packit 709fb3
/* Put STRING, which is of the form "NAME=VALUE", in the environment.
Packit 709fb3
   If STRING contains no '=', then remove STRING from the environment.  */
Packit 709fb3
int
Packit 709fb3
putenv (char *string)
Packit 709fb3
{
Packit 709fb3
  const char *name_end = strchr (string, '=');
Packit 709fb3
  char **ep;
Packit 709fb3
Packit 709fb3
  if (name_end == NULL)
Packit 709fb3
    {
Packit 709fb3
      /* Remove the variable from the environment.  */
Packit 709fb3
      return _unsetenv (string);
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
#if HAVE_DECL__PUTENV
Packit 709fb3
  /* Rely on _putenv to allocate the new environment.  If other
Packit 709fb3
     parts of the application use _putenv, the !HAVE_DECL__PUTENV code
Packit 709fb3
     would fight over who owns the environ vector, causing a crash.  */
Packit 709fb3
  if (name_end[1])
Packit 709fb3
    return _putenv (string);
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
Packit 709fb3
         to allocate the environ vector and then replace the new
Packit 709fb3
         entry with "NAME=".  */
Packit 709fb3
      int putenv_result, putenv_errno;
Packit 709fb3
      char *name_x = malloc (name_end - string + sizeof "= ");
Packit 709fb3
      if (!name_x)
Packit 709fb3
        return -1;
Packit 709fb3
      memcpy (name_x, string, name_end - string + 1);
Packit 709fb3
      name_x[name_end - string + 1] = ' ';
Packit 709fb3
      name_x[name_end - string + 2] = 0;
Packit 709fb3
      putenv_result = _putenv (name_x);
Packit 709fb3
      putenv_errno = errno;
Packit 709fb3
      for (ep = environ; *ep; ep++)
Packit 709fb3
        if (strcmp (*ep, name_x) == 0)
Packit 709fb3
          {
Packit 709fb3
            *ep = string;
Packit 709fb3
            break;
Packit 709fb3
          }
Packit 709fb3
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
      if (putenv_result == 0)
Packit 709fb3
        {
Packit 709fb3
          /* _putenv propagated "NAME= " into the subprocess environment;
Packit 709fb3
             fix that by calling SetEnvironmentVariable directly.  */
Packit 709fb3
          name_x[name_end - string] = 0;
Packit 709fb3
          putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
Packit 709fb3
          putenv_errno = ENOMEM; /* ENOMEM is the only way to fail.  */
Packit 709fb3
        }
Packit 709fb3
# endif
Packit 709fb3
      free (name_x);
Packit 709fb3
      __set_errno (putenv_errno);
Packit 709fb3
      return putenv_result;
Packit 709fb3
    }
Packit 709fb3
#else
Packit 709fb3
  for (ep = environ; *ep; ep++)
Packit 709fb3
    if (strncmp (*ep, string, name_end - string) == 0
Packit 709fb3
        && (*ep)[name_end - string] == '=')
Packit 709fb3
      break;
Packit 709fb3
Packit 709fb3
  if (*ep)
Packit 709fb3
    *ep = string;
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      static char **last_environ = NULL;
Packit 709fb3
      size_t size = ep - environ;
Packit 709fb3
      char **new_environ = malloc ((size + 2) * sizeof *new_environ);
Packit 709fb3
      if (! new_environ)
Packit 709fb3
        return -1;
Packit 709fb3
      new_environ[0] = string;
Packit 709fb3
      memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
Packit 709fb3
      free (last_environ);
Packit 709fb3
      last_environ = new_environ;
Packit 709fb3
      environ = new_environ;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
#endif
Packit 709fb3
}