Blame gl/tests/putenv.c

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