*** empty log message ***
[shishi.git] / gl / unsetenv.c
blob0f837440db3fc2f339f979c5ec95b8d06e7e96fa
1 /* Copyright (C) 1992,1995-1999,2000-2002,2005-2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #include <config.h>
20 #include <errno.h>
21 #if !_LIBC
22 # define __set_errno(ev) ((errno) = (ev))
23 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #if !_LIBC
30 # define __environ environ
31 # ifndef HAVE_ENVIRON_DECL
32 extern char **environ;
33 # endif
34 #endif
36 #if _LIBC
37 /* This lock protects against simultaneous modifications of `environ'. */
38 # include <bits/libc-lock.h>
39 __libc_lock_define_initialized (static, envlock)
40 # define LOCK __libc_lock_lock (envlock)
41 # define UNLOCK __libc_lock_unlock (envlock)
42 #else
43 # define LOCK
44 # define UNLOCK
45 #endif
47 /* In the GNU C library we must keep the namespace clean. */
48 #ifdef _LIBC
49 # define unsetenv __unsetenv
50 #endif
53 int
54 unsetenv (const char *name)
56 size_t len;
57 char **ep;
59 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
61 __set_errno (EINVAL);
62 return -1;
65 len = strlen (name);
67 LOCK;
69 ep = __environ;
70 while (*ep != NULL)
71 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
73 /* Found it. Remove this pointer by moving later ones back. */
74 char **dp = ep;
77 dp[0] = dp[1];
78 while (*dp++);
79 /* Continue the loop in case NAME appears again. */
81 else
82 ++ep;
84 UNLOCK;
86 return 0;
89 #ifdef _LIBC
90 # undef unsetenv
91 weak_alias (__unsetenv, unsetenv)
92 #endif