Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / unsetenv.c
1 /* Copyright (C) 1992,1995-1999,2000-2002,2005-2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
17
18 #include <config.h>
19
20 #include <errno.h>
21 #if !_LIBC
22 # define __set_errno(ev) ((errno) = (ev))
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #if !_LIBC
30 # define __environ      environ
31 # ifndef HAVE_ENVIRON_DECL
32 extern char **environ;
33 # endif
34 #endif
35
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
46
47 /* In the GNU C library we must keep the namespace clean.  */
48 #ifdef _LIBC
49 # define unsetenv __unsetenv
50 #endif
51
52
53 int
54 unsetenv (const char *name)
55 {
56   size_t len;
57   char **ep;
58
59   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
60     {
61       __set_errno (EINVAL);
62       return -1;
63     }
64
65   len = strlen (name);
66
67   LOCK;
68
69   ep = __environ;
70   while (*ep != NULL)
71     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
72       {
73         /* Found it.  Remove this pointer by moving later ones back.  */
74         char **dp = ep;
75
76         do
77           dp[0] = dp[1];
78         while (*dp++);
79         /* Continue the loop in case NAME appears again.  */
80       }
81     else
82       ++ep;
83
84   UNLOCK;
85
86   return 0;
87 }
88
89 #ifdef _LIBC
90 # undef unsetenv
91 weak_alias (__unsetenv, unsetenv)
92 #endif