/[inetutils]/inetutils/libinetutils/setenv.c
ViewVC logotype

Diff of /inetutils/libinetutils/setenv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by gray, Tue May 14 15:13:56 2002 UTC revision 1.7 by jbailey, Wed Dec 11 13:19:52 2002 UTC
# Line 1  Line 1 
1  /* Copyright (C) 1992, 1995, 1996, 1997, 2000 Free Software Foundation, Inc.  /* Copyright (C) 1992, 1995, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.  
2    
3     The GNU C Library is free software; you can redistribute it and/or  NOTE: The canonical source of this file is maintained with the GNU C Library.
4     modify it under the terms of the GNU Library General Public License as  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
    published by the Free Software Foundation; either version 2 of the  
    License, or (at your option) any later version.  
   
    The GNU C Library is distributed in the hope that it will be useful,  
    but WITHOUT ANY WARRANTY; without even the implied warranty of  
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
    Library General Public License for more details.  
   
    You should have received a copy of the GNU Library General Public  
    License along with the GNU C Library; see the file COPYING.LIB.  If not,  
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,  
    Boston, MA 02111-1307, USA.  */  
5    
6  #if HAVE_CONFIG_H  This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19    USA.  */
20    
21    #ifdef HAVE_CONFIG_H
22  # include <config.h>  # include <config.h>
23  #endif  #endif
24    
25  #include <errno.h>  #include <errno.h>
 #if !_LIBC  
 # if !defined(errno) && !HAVE_DECL_ERRNO  
 extern int errno;  
 # endif  
 # define __set_errno(ev) ((errno) = (ev))  
 #endif  
26    
27  #if _LIBC || HAVE_STDLIB_H  #if _LIBC || HAVE_STDLIB_H
28  # include <stdlib.h>  # include <stdlib.h>
# Line 38  extern int errno; Line 34  extern int errno;
34  # include <unistd.h>  # include <unistd.h>
35  #endif  #endif
36    
37  #if _LIBC - 0 == 0  #ifndef HAVE_GNU_LD
38  # define __environ      environ  # define __environ      environ
 # if !HAVE_DECL_ENVIRON  
 extern char **environ;  
 # endif  
 #endif  
   
 #if _LIBC - 0  
 /* This lock protects against simultaneous modifications of `environ'.  */  
 # include <libc-lock.h>  
 __libc_lock_define_initialized (static, envlock)  
 # define LOCK   __libc_lock_lock (envlock)  
 # define UNLOCK __libc_lock_unlock (envlock)  
 #else  
 # define LOCK  
 # define UNLOCK  
39  #endif  #endif
40    
 /* If this variable is not a null pointer we allocated the current  
    environment.  */  
 static char **last_environ;  
   
   
41  int  int
42  setenv (const char *name, const char *value, int replace)  setenv (name, value, replace)
43         const char *name;
44         const char *value;
45         int replace;
46  {  {
47    register char **ep;    register char **ep;
48    register size_t size;    register size_t size;
49    const size_t namelen = strlen (name);    const size_t namelen = strlen (name);
50    const size_t vallen = strlen (value) + 1;    const size_t vallen = strlen (value) + 1;
51    
   LOCK;  
   
52    size = 0;    size = 0;
53    if (__environ != NULL)    for (ep = __environ; *ep != NULL; ++ep)
54      for (ep = __environ; *ep != NULL; ++ep)      if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
55        if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')        break;
56          break;      else
57        else        ++size;
         ++size;  
58    
59    if (__environ == NULL || *ep == NULL)    if (*ep == NULL)
60      {      {
61          static char **last_environ;
62        char **new_environ;        char **new_environ;
63        if (__environ == last_environ && __environ != NULL)        if (__environ == last_environ)
64          /* We allocated this space; we can extend it.  */          /* We allocated this space; we can extend it.  */
65          new_environ = (char **) realloc (last_environ,          new_environ = (char **) realloc (last_environ,
66                                           (size + 2) * sizeof (char *));                                           (size + 2) * sizeof (char *));
# Line 90  setenv (const char *name, const char *va Line 68  setenv (const char *name, const char *va
68          new_environ = (char **) malloc ((size + 2) * sizeof (char *));          new_environ = (char **) malloc ((size + 2) * sizeof (char *));
69    
70        if (new_environ == NULL)        if (new_environ == NULL)
71          {          return -1;
           UNLOCK;  
           return -1;  
         }  
72    
73        new_environ[size] = malloc (namelen + 1 + vallen);        new_environ[size] = malloc (namelen + 1 + vallen);
74        if (new_environ[size] == NULL)        if (new_environ[size] == NULL)
75          {          {
76            free ((char *) new_environ);            free ((char *) new_environ);
77            __set_errno (ENOMEM);            errno = ENOMEM;
           UNLOCK;  
78            return -1;            return -1;
79          }          }
80    
# Line 124  setenv (const char *name, const char *va Line 98  setenv (const char *name, const char *va
98            /* The existing string is too short; malloc a new one.  */            /* The existing string is too short; malloc a new one.  */
99            char *new = malloc (namelen + 1 + vallen);            char *new = malloc (namelen + 1 + vallen);
100            if (new == NULL)            if (new == NULL)
101              {              return -1;
               UNLOCK;  
               return -1;  
             }  
102            *ep = new;            *ep = new;
103          }          }
104        memcpy (*ep, name, namelen);        memcpy (*ep, name, namelen);
# Line 135  setenv (const char *name, const char *va Line 106  setenv (const char *name, const char *va
106        memcpy (&(*ep)[namelen + 1], value, vallen);        memcpy (&(*ep)[namelen + 1], value, vallen);
107      }      }
108    
   UNLOCK;  
   
109    return 0;    return 0;
110  }  }
111    
112  void  void
113  unsetenv (const char *name)  unsetenv (name)
114         const char *name;
115  {  {
116    const size_t len = strlen (name);    const size_t len = strlen (name);
117    char **ep;    char **ep;
118    
   LOCK;  
   
119    for (ep = __environ; *ep; ++ep)    for (ep = __environ; *ep; ++ep)
120      if (!strncmp (*ep, name, len) && (*ep)[len] == '=')      if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
121        {        {
# Line 158  unsetenv (const char *name) Line 126  unsetenv (const char *name)
126          while (*dp++);          while (*dp++);
127          /* Continue the loop in case NAME appears again.  */          /* Continue the loop in case NAME appears again.  */
128        }        }
   
   UNLOCK;  
 }  
   
 /* The `clearenv' was planned to be added to POSIX.1 but probably  
    never made it.  Nevertheless the POSIX.9 standard (POSIX bindings  
    for Fortran 77) requires this function.  */  
 int  
 clearenv ()  
 {  
   LOCK;  
   
   if (__environ == last_environ && __environ != NULL)  
     {  
       /* We allocated this environment so we can free it.  */  
       free (__environ);  
       last_environ = NULL;  
     }  
   
   /* Clear the environment pointer removes the whole environment.  */  
   __environ = NULL;  
   
   UNLOCK;  
   
   return 0;  
129  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26