Back to index

nagios-plugins  1.4.16
unsetenv.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 1995-2002, 2005-2010 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    This program is free software: you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 #include <config.h>
00018 
00019 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
00020    optimizes away the name == NULL test below.  */
00021 #define _GL_ARG_NONNULL(params)
00022 
00023 /* Specification.  */
00024 #include <stdlib.h>
00025 
00026 #include <errno.h>
00027 #if !_LIBC
00028 # define __set_errno(ev) ((errno) = (ev))
00029 #endif
00030 
00031 #include <string.h>
00032 #include <unistd.h>
00033 
00034 #if !_LIBC
00035 # define __environ      environ
00036 #endif
00037 
00038 #if _LIBC
00039 /* This lock protects against simultaneous modifications of `environ'.  */
00040 # include <bits/libc-lock.h>
00041 __libc_lock_define_initialized (static, envlock)
00042 # define LOCK   __libc_lock_lock (envlock)
00043 # define UNLOCK __libc_lock_unlock (envlock)
00044 #else
00045 # define LOCK
00046 # define UNLOCK
00047 #endif
00048 
00049 /* In the GNU C library we must keep the namespace clean.  */
00050 #ifdef _LIBC
00051 # define unsetenv __unsetenv
00052 #endif
00053 
00054 #if _LIBC || !HAVE_UNSETENV
00055 
00056 int
00057 unsetenv (const char *name)
00058 {
00059   size_t len;
00060   char **ep;
00061 
00062   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
00063     {
00064       __set_errno (EINVAL);
00065       return -1;
00066     }
00067 
00068   len = strlen (name);
00069 
00070   LOCK;
00071 
00072   ep = __environ;
00073   while (*ep != NULL)
00074     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
00075       {
00076         /* Found it.  Remove this pointer by moving later ones back.  */
00077         char **dp = ep;
00078 
00079         do
00080           dp[0] = dp[1];
00081         while (*dp++);
00082         /* Continue the loop in case NAME appears again.  */
00083       }
00084     else
00085       ++ep;
00086 
00087   UNLOCK;
00088 
00089   return 0;
00090 }
00091 
00092 #ifdef _LIBC
00093 # undef unsetenv
00094 weak_alias (__unsetenv, unsetenv)
00095 #endif
00096 
00097 #else /* HAVE_UNSETENV */
00098 
00099 # undef unsetenv
00100 
00101 /* Call the underlying unsetenv, in case there is hidden bookkeeping
00102    that needs updating beyond just modifying environ.  */
00103 int
00104 rpl_unsetenv (const char *name)
00105 {
00106   int result = 0;
00107   if (!name || !*name || strchr (name, '='))
00108     {
00109       errno = EINVAL;
00110       return -1;
00111     }
00112   while (getenv (name))
00113 # if !VOID_UNSETENV
00114     result =
00115 # endif
00116       unsetenv (name);
00117   return result;
00118 }
00119 
00120 #endif /* HAVE_UNSETENV */