1/* Copyright (C) 1992,1995-1999,2000-2002 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
4   The GNU C Library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Library General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   The GNU C Library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Library General Public License for more details.
13
14   You should have received a copy of the GNU Library General Public
15   License along with the GNU C Library; see the file COPYING.LIB.  If not,
16   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17   Boston, MA 02111-1307, USA.  */
18
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include <errno.h>
24#if !_LIBC
25# if !defined errno && !defined HAVE_ERRNO_DECL
26extern int errno;
27# endif
28# define __set_errno(ev) ((errno) = (ev))
29#endif
30
31#include <stdlib.h>
32#include <string.h>
33#if _LIBC || HAVE_UNISTD_H
34# include <unistd.h>
35#endif
36
37#if !_LIBC
38# define __environ	environ
39# ifndef HAVE_ENVIRON_DECL
40extern char **environ;
41# endif
42#endif
43
44#if _LIBC
45/* This lock protects against simultaneous modifications of `environ'.  */
46# include <bits/libc-lock.h>
47__libc_lock_define_initialized (static, envlock)
48# define LOCK	__libc_lock_lock (envlock)
49# define UNLOCK	__libc_lock_unlock (envlock)
50#else
51# define LOCK
52# define UNLOCK
53#endif
54
55/* In the GNU C library we must keep the namespace clean.  */
56#ifdef _LIBC
57# define unsetenv __unsetenv
58#endif
59
60
61int
62unsetenv (const char *name)
63{
64  size_t len;
65  char **ep;
66
67  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
68    {
69      __set_errno (EINVAL);
70      return -1;
71    }
72
73  len = strlen (name);
74
75  LOCK;
76
77  ep = __environ;
78  while (*ep != NULL)
79    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
80      {
81	/* Found it.  Remove this pointer by moving later ones back.  */
82	char **dp = ep;
83
84	do
85	  dp[0] = dp[1];
86	while (*dp++);
87	/* Continue the loop in case NAME appears again.  */
88      }
89    else
90      ++ep;
91
92  UNLOCK;
93
94  return 0;
95}
96
97#ifdef _LIBC
98# undef unsetenv
99weak_alias (__unsetenv, unsetenv)
100#endif
101