Blame lib/unsetenv.c

Packit 8f70b4
/* Copyright (C) 1992, 1995-2002, 2005-2018 Free Software Foundation, Inc.
Packit 8f70b4
   This file is part of the GNU C Library.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
Packit 8f70b4
   optimizes away the name == NULL test below.  */
Packit 8f70b4
#define _GL_ARG_NONNULL(params)
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <stdlib.h>
Packit 8f70b4
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
#if !_LIBC
Packit 8f70b4
# define __set_errno(ev) ((errno) = (ev))
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#include <string.h>
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
Packit 8f70b4
#if !_LIBC
Packit 8f70b4
# define __environ      environ
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if _LIBC
Packit 8f70b4
/* This lock protects against simultaneous modifications of 'environ'.  */
Packit 8f70b4
# include <bits/libc-lock.h>
Packit 8f70b4
__libc_lock_define_initialized (static, envlock)
Packit 8f70b4
# define LOCK   __libc_lock_lock (envlock)
Packit 8f70b4
# define UNLOCK __libc_lock_unlock (envlock)
Packit 8f70b4
#else
Packit 8f70b4
# define LOCK
Packit 8f70b4
# define UNLOCK
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* In the GNU C library we must keep the namespace clean.  */
Packit 8f70b4
#ifdef _LIBC
Packit 8f70b4
# define unsetenv __unsetenv
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if _LIBC || !HAVE_UNSETENV
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
unsetenv (const char *name)
Packit 8f70b4
{
Packit 8f70b4
  size_t len;
Packit 8f70b4
  char **ep;
Packit 8f70b4
Packit 8f70b4
  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
Packit 8f70b4
    {
Packit 8f70b4
      __set_errno (EINVAL);
Packit 8f70b4
      return -1;
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  len = strlen (name);
Packit 8f70b4
Packit 8f70b4
  LOCK;
Packit 8f70b4
Packit 8f70b4
  ep = __environ;
Packit 8f70b4
  while (*ep != NULL)
Packit 8f70b4
    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
Packit 8f70b4
      {
Packit 8f70b4
        /* Found it.  Remove this pointer by moving later ones back.  */
Packit 8f70b4
        char **dp = ep;
Packit 8f70b4
Packit 8f70b4
        do
Packit 8f70b4
          dp[0] = dp[1];
Packit 8f70b4
        while (*dp++);
Packit 8f70b4
        /* Continue the loop in case NAME appears again.  */
Packit 8f70b4
      }
Packit 8f70b4
    else
Packit 8f70b4
      ++ep;
Packit 8f70b4
Packit 8f70b4
  UNLOCK;
Packit 8f70b4
Packit 8f70b4
  return 0;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#ifdef _LIBC
Packit 8f70b4
# undef unsetenv
Packit 8f70b4
weak_alias (__unsetenv, unsetenv)
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#else /* HAVE_UNSETENV */
Packit 8f70b4
Packit 8f70b4
# undef unsetenv
Packit 8f70b4
# if !HAVE_DECL_UNSETENV
Packit 8f70b4
#  if VOID_UNSETENV
Packit 8f70b4
extern void unsetenv (const char *);
Packit 8f70b4
#  else
Packit 8f70b4
extern int unsetenv (const char *);
Packit 8f70b4
#  endif
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
/* Call the underlying unsetenv, in case there is hidden bookkeeping
Packit 8f70b4
   that needs updating beyond just modifying environ.  */
Packit 8f70b4
int
Packit 8f70b4
rpl_unsetenv (const char *name)
Packit 8f70b4
{
Packit 8f70b4
  int result = 0;
Packit 8f70b4
  if (!name || !*name || strchr (name, '='))
Packit 8f70b4
    {
Packit 8f70b4
      errno = EINVAL;
Packit 8f70b4
      return -1;
Packit 8f70b4
    }
Packit 8f70b4
  while (getenv (name))
Packit 8f70b4
# if !VOID_UNSETENV
Packit 8f70b4
    result =
Packit 8f70b4
# endif
Packit 8f70b4
      unsetenv (name);
Packit 8f70b4
  return result;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#endif /* HAVE_UNSETENV */