Back to index

glibc  2.9
putenv.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 94, 95, 96, 97, 98, 99 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 defined _AIX && !defined __GNUC__
00020  #pragma alloca
00021 #endif
00022 
00023 #if HAVE_CONFIG_H
00024 # include <config.h>
00025 #endif
00026 
00027 #if _LIBC || HAVE_STDLIB_H
00028 # include <stdlib.h>
00029 #endif
00030 #if _LIBC || HAVE_STRING_H
00031 # include <string.h>
00032 #endif
00033 
00034 #if !__GNU_LIBRARY__ && !HAVE_STRCHR
00035 # define strchr index
00036 #endif
00037 
00038 #ifndef _LIBC
00039 # ifdef HAVE_ALLOCA_H
00040 #  include <alloca.h>
00041 # else
00042 #  ifdef __GNUC__
00043 #   define alloca __builtin_alloca
00044 #  else
00045 extern char *alloca ();
00046 #  endif /* __GNUC__ */
00047 # endif /* HAVE_ALLOCA_H */
00048 #endif /* _LIBC */
00049 
00050 
00051 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
00052 int
00053 putenv (string)
00054      char *string;
00055 {
00056   const char *const name_end = strchr (string, '=');
00057 
00058   if (name_end != NULL)
00059     {
00060 #ifdef _LIBC
00061       char *name = strndupa (string, name_end - string);
00062 #else
00063       char *name = alloca (name_end - string + 1);
00064       memcpy (name, string, name_end - string);
00065       name[name_end - string] = '\0';
00066 #endif
00067       return __add_to_environ (name, NULL, string, 1);
00068     }
00069 
00070   __unsetenv (string);
00071   return 0;
00072 }