Back to index

glibc  2.9
setenv.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,1995-2001,2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library 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 GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #if HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 #include <errno.h>
00024 #if !_LIBC
00025 # if !defined errno && !defined HAVE_ERRNO_DECL
00026 extern int errno;
00027 # endif
00028 # define __set_errno(ev) ((errno) = (ev))
00029 #endif
00030 
00031 #if _LIBC || HAVE_STDLIB_H
00032 # include <stdlib.h>
00033 #endif
00034 #if _LIBC || HAVE_STRING_H
00035 # include <string.h>
00036 #endif
00037 #if _LIBC || HAVE_UNISTD_H
00038 # include <unistd.h>
00039 #endif
00040 
00041 #if !_LIBC
00042 # define __environ   environ
00043 # ifndef HAVE_ENVIRON_DECL
00044 extern char **environ;
00045 # endif
00046 #endif
00047 
00048 #if _LIBC
00049 /* This lock protects against simultaneous modifications of `environ'.  */
00050 # include <bits/libc-lock.h>
00051 __libc_lock_define_initialized (static, envlock)
00052 # define LOCK __libc_lock_lock (envlock)
00053 # define UNLOCK      __libc_lock_unlock (envlock)
00054 #else
00055 # define LOCK
00056 # define UNLOCK
00057 #endif
00058 
00059 /* In the GNU C library we must keep the namespace clean.  */
00060 #ifdef _LIBC
00061 # define setenv __setenv
00062 # define unsetenv __unsetenv
00063 # define clearenv __clearenv
00064 # define tfind __tfind
00065 # define tsearch __tsearch
00066 #endif
00067 
00068 /* In the GNU C library implementation we try to be more clever and
00069    allow arbitrarily many changes of the environment given that the used
00070    values are from a small set.  Outside glibc this will eat up all
00071    memory after a while.  */
00072 #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
00073                     && defined __GNUC__)
00074 # define USE_TSEARCH 1
00075 # include <search.h>
00076 
00077 /* This is a pointer to the root of the search tree with the known
00078    values.  */
00079 static void *known_values;
00080 
00081 # define KNOWN_VALUE(Str) \
00082   ({                                                                 \
00083     void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp);       \
00084     value != NULL ? *(char **) value : NULL;                                \
00085   })
00086 # define STORE_VALUE(Str) \
00087   tsearch (Str, &known_values, (__compar_fn_t) strcmp)
00088 
00089 #else
00090 # undef USE_TSEARCH
00091 
00092 # define KNOWN_VALUE(Str) NULL
00093 # define STORE_VALUE(Str) do { } while (0)
00094 
00095 #endif
00096 
00097 
00098 /* If this variable is not a null pointer we allocated the current
00099    environment.  */
00100 static char **last_environ;
00101 
00102 
00103 /* This function is used by `setenv' and `putenv'.  The difference between
00104    the two functions is that for the former must create a new string which
00105    is then placed in the environment, while the argument of `putenv'
00106    must be used directly.  This is all complicated by the fact that we try
00107    to reuse values once generated for a `setenv' call since we can never
00108    free the strings.  */
00109 int
00110 __add_to_environ (name, value, combined, replace)
00111      const char *name;
00112      const char *value;
00113      const char *combined;
00114      int replace;
00115 {
00116   register char **ep;
00117   register size_t size;
00118   const size_t namelen = strlen (name);
00119   const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
00120 
00121   LOCK;
00122 
00123   /* We have to get the pointer now that we have the lock and not earlier
00124      since another thread might have created a new environment.  */
00125   ep = __environ;
00126 
00127   size = 0;
00128   if (ep != NULL)
00129     {
00130       for (; *ep != NULL; ++ep)
00131        if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
00132          break;
00133        else
00134          ++size;
00135     }
00136 
00137   if (ep == NULL || __builtin_expect (*ep == NULL, 1))
00138     {
00139       char **new_environ;
00140 
00141       /* We allocated this space; we can extend it.  */
00142       new_environ = (char **) realloc (last_environ,
00143                                    (size + 2) * sizeof (char *));
00144       if (new_environ == NULL)
00145        {
00146          UNLOCK;
00147          return -1;
00148        }
00149 
00150       /* If the whole entry is given add it.  */
00151       if (combined != NULL)
00152        /* We must not add the string to the search tree since it belongs
00153           to the user.  */
00154        new_environ[size] = (char *) combined;
00155       else
00156        {
00157          /* See whether the value is already known.  */
00158 #ifdef USE_TSEARCH
00159 # ifdef __GNUC__
00160          char new_value[namelen + 1 + vallen];
00161 # else
00162          char *new_value = (char *) alloca (namelen + 1 + vallen);
00163 # endif
00164 # ifdef _LIBC
00165          __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
00166                    value, vallen);
00167 # else
00168          memcpy (new_value, name, namelen);
00169          new_value[namelen] = '=';
00170          memcpy (&new_value[namelen + 1], value, vallen);
00171 # endif
00172 
00173          new_environ[size] = KNOWN_VALUE (new_value);
00174          if (__builtin_expect (new_environ[size] == NULL, 1))
00175 #endif
00176            {
00177              new_environ[size] = (char *) malloc (namelen + 1 + vallen);
00178              if (__builtin_expect (new_environ[size] == NULL, 0))
00179               {
00180                 __set_errno (ENOMEM);
00181                 UNLOCK;
00182                 return -1;
00183               }
00184 
00185 #ifdef USE_TSEARCH
00186              memcpy (new_environ[size], new_value, namelen + 1 + vallen);
00187 #else
00188              memcpy (new_environ[size], name, namelen);
00189              new_environ[size][namelen] = '=';
00190              memcpy (&new_environ[size][namelen + 1], value, vallen);
00191 #endif
00192              /* And save the value now.  We cannot do this when we remove
00193                the string since then we cannot decide whether it is a
00194                user string or not.  */
00195              STORE_VALUE (new_environ[size]);
00196            }
00197        }
00198 
00199       if (__environ != last_environ)
00200        memcpy ((char *) new_environ, (char *) __environ,
00201               size * sizeof (char *));
00202 
00203       new_environ[size + 1] = NULL;
00204 
00205       last_environ = __environ = new_environ;
00206     }
00207   else if (replace)
00208     {
00209       char *np;
00210 
00211       /* Use the user string if given.  */
00212       if (combined != NULL)
00213        np = (char *) combined;
00214       else
00215        {
00216 #ifdef USE_TSEARCH
00217 # ifdef __GNUC__
00218          char new_value[namelen + 1 + vallen];
00219 # else
00220          char *new_value = (char *) alloca (namelen + 1 + vallen);
00221 # endif
00222 # ifdef _LIBC
00223          __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
00224                    value, vallen);
00225 # else
00226          memcpy (new_value, name, namelen);
00227          new_value[namelen] = '=';
00228          memcpy (&new_value[namelen + 1], value, vallen);
00229 # endif
00230 
00231          np = KNOWN_VALUE (new_value);
00232          if (__builtin_expect (np == NULL, 1))
00233 #endif
00234            {
00235              np = malloc (namelen + 1 + vallen);
00236              if (__builtin_expect (np == NULL, 0))
00237               {
00238                 UNLOCK;
00239                 return -1;
00240               }
00241 
00242 #ifdef USE_TSEARCH
00243              memcpy (np, new_value, namelen + 1 + vallen);
00244 #else
00245              memcpy (np, name, namelen);
00246              np[namelen] = '=';
00247              memcpy (&np[namelen + 1], value, vallen);
00248 #endif
00249              /* And remember the value.  */
00250              STORE_VALUE (np);
00251            }
00252        }
00253 
00254       *ep = np;
00255     }
00256 
00257   UNLOCK;
00258 
00259   return 0;
00260 }
00261 
00262 int
00263 setenv (name, value, replace)
00264      const char *name;
00265      const char *value;
00266      int replace;
00267 {
00268   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
00269     {
00270       __set_errno (EINVAL);
00271       return -1;
00272     }
00273 
00274   return __add_to_environ (name, value, NULL, replace);
00275 }
00276 
00277 int
00278 unsetenv (name)
00279      const char *name;
00280 {
00281   size_t len;
00282   char **ep;
00283 
00284   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
00285     {
00286       __set_errno (EINVAL);
00287       return -1;
00288     }
00289 
00290   len = strlen (name);
00291 
00292   LOCK;
00293 
00294   ep = __environ;
00295   while (*ep != NULL)
00296     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
00297       {
00298        /* Found it.  Remove this pointer by moving later ones back.  */
00299        char **dp = ep;
00300 
00301        do
00302          dp[0] = dp[1];
00303        while (*dp++);
00304        /* Continue the loop in case NAME appears again.  */
00305       }
00306     else
00307       ++ep;
00308 
00309   UNLOCK;
00310 
00311   return 0;
00312 }
00313 
00314 /* The `clearenv' was planned to be added to POSIX.1 but probably
00315    never made it.  Nevertheless the POSIX.9 standard (POSIX bindings
00316    for Fortran 77) requires this function.  */
00317 int
00318 clearenv ()
00319 {
00320   LOCK;
00321 
00322   if (__environ == last_environ && __environ != NULL)
00323     {
00324       /* We allocated this environment so we can free it.  */
00325       free (__environ);
00326       last_environ = NULL;
00327     }
00328 
00329   /* Clear the environment pointer removes the whole environment.  */
00330   __environ = NULL;
00331 
00332   UNLOCK;
00333 
00334   return 0;
00335 }
00336 #ifdef _LIBC
00337 libc_freeres_fn (free_mem)
00338 {
00339   /* Remove all traces.  */
00340   clearenv ();
00341 
00342   /* Now remove the search tree.  */
00343   __tdestroy (known_values, free);
00344   known_values = NULL;
00345 }
00346 
00347 # undef setenv
00348 # undef unsetenv
00349 # undef clearenv
00350 weak_alias (__setenv, setenv)
00351 weak_alias (__unsetenv, unsetenv)
00352 weak_alias (__clearenv, clearenv)
00353 #endif