Back to index

cell-binutils  2.17cvs20070401
putenv.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1994, 1995, 1996, 2002 Free Software Foundation, Inc.
00002    This file based on putenv.c in 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 Library General Public License as
00006    published by the Free Software Foundation; either version 2 of the
00007    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    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00016    write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00017    Boston, MA 02110-1301, USA.  */
00018 
00019 /*
00020 
00021 @deftypefn Supplemental int putenv (const char *@var{string})
00022 
00023 Uses @code{setenv} or @code{unsetenv} to put @var{string} into
00024 the environment or remove it.  If @var{string} is of the form
00025 @samp{name=value} the string is added; if no @samp{=} is present the
00026 name is unset/removed.
00027 
00028 @end deftypefn
00029 
00030 */
00031 
00032 #if defined (_AIX) && !defined (__GNUC__)
00033  #pragma alloca
00034 #endif
00035 
00036 #if HAVE_CONFIG_H
00037 # include <config.h>
00038 #endif
00039 
00040 #include "ansidecl.h"
00041 
00042 #define putenv libiberty_putenv
00043 
00044 #if HAVE_STDLIB_H
00045 # include <stdlib.h>
00046 #endif
00047 #if HAVE_STRING_H
00048 # include <string.h>
00049 #endif
00050 
00051 #ifdef HAVE_ALLOCA_H
00052 # include <alloca.h>
00053 #else
00054 # ifndef alloca
00055 #  ifdef __GNUC__
00056 #   define alloca __builtin_alloca
00057 #  else
00058 extern char *alloca ();
00059 #  endif /* __GNUC__ */
00060 # endif /* alloca */
00061 #endif /* HAVE_ALLOCA_H */
00062 
00063 #undef putenv
00064 
00065 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
00066 
00067 
00068 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
00069 int
00070 putenv (const char *string)
00071 {
00072   const char *const name_end = strchr (string, '=');
00073 
00074   if (name_end)
00075     {
00076       char *name = (char *) alloca (name_end - string + 1);
00077       memcpy (name, string, name_end - string);
00078       name[name_end - string] = '\0';
00079       return setenv (name, name_end + 1, 1);
00080     }
00081 
00082   unsetenv (string);
00083   return 0;
00084 }