Blame gnulib-tests/unsetenv.c

Packit 709fb3
/* Copyright (C) 1992, 1995-2002, 2005-2017 Free Software Foundation, Inc.
Packit 709fb3
   This file is part of the GNU C Library.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any 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
/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
Packit 709fb3
   optimizes away the name == NULL test below.  */
Packit 709fb3
#define _GL_ARG_NONNULL(params)
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
/* Specification.  */
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
Packit 709fb3
#include <errno.h>
Packit 709fb3
#if !_LIBC
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 !_LIBC
Packit 709fb3
# define __environ      environ
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
/* In the GNU C library we must keep the namespace clean.  */
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
# define unsetenv __unsetenv
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if _LIBC || !HAVE_UNSETENV
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
unsetenv (const char *name)
Packit 709fb3
{
Packit 709fb3
  size_t len;
Packit 709fb3
  char **ep;
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
  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
}
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
# undef unsetenv
Packit 709fb3
weak_alias (__unsetenv, unsetenv)
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#else /* HAVE_UNSETENV */
Packit 709fb3
Packit 709fb3
# undef unsetenv
Packit 709fb3
# if !HAVE_DECL_UNSETENV
Packit 709fb3
#  if VOID_UNSETENV
Packit 709fb3
extern void unsetenv (const char *);
Packit 709fb3
#  else
Packit 709fb3
extern int unsetenv (const char *);
Packit 709fb3
#  endif
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
/* Call the underlying unsetenv, in case there is hidden bookkeeping
Packit 709fb3
   that needs updating beyond just modifying environ.  */
Packit 709fb3
int
Packit 709fb3
rpl_unsetenv (const char *name)
Packit 709fb3
{
Packit 709fb3
  int result = 0;
Packit 709fb3
  if (!name || !*name || strchr (name, '='))
Packit 709fb3
    {
Packit 709fb3
      errno = EINVAL;
Packit 709fb3
      return -1;
Packit 709fb3
    }
Packit 709fb3
  while (getenv (name))
Packit 709fb3
# if !VOID_UNSETENV
Packit 709fb3
    result =
Packit 709fb3
# endif
Packit 709fb3
      unsetenv (name);
Packit 709fb3
  return result;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#endif /* HAVE_UNSETENV */