189857Sobrien/* Copyright (C) 1991, 1994, 1995, 1996, 2002 Free Software Foundation, Inc.
260484Sobrien   This file based on putenv.c in the GNU C Library.
360484Sobrien
460484Sobrien   The GNU C Library is free software; you can redistribute it and/or
560484Sobrien   modify it under the terms of the GNU Library General Public License as
660484Sobrien   published by the Free Software Foundation; either version 2 of the
760484Sobrien   License, or (at your option) any later version.
860484Sobrien
960484Sobrien   The GNU C Library is distributed in the hope that it will be useful,
1060484Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1160484Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1260484Sobrien   Library General Public License for more details.
1360484Sobrien
1460484Sobrien   You should have received a copy of the GNU Library General Public
1560484Sobrien   License along with the GNU C Library; see the file COPYING.LIB.  If not,
16218822Sdim   write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
17218822Sdim   Boston, MA 02110-1301, USA.  */
1860484Sobrien
1989857Sobrien/*
2089857Sobrien
2189857Sobrien@deftypefn Supplemental int putenv (const char *@var{string})
2289857Sobrien
2389857SobrienUses @code{setenv} or @code{unsetenv} to put @var{string} into
2489857Sobrienthe environment or remove it.  If @var{string} is of the form
2589857Sobrien@samp{name=value} the string is added; if no @samp{=} is present the
2689857Sobrienname is unset/removed.
2789857Sobrien
2889857Sobrien@end deftypefn
2989857Sobrien
3089857Sobrien*/
3189857Sobrien
3260484Sobrien#if defined (_AIX) && !defined (__GNUC__)
3360484Sobrien #pragma alloca
3460484Sobrien#endif
3560484Sobrien
3660484Sobrien#if HAVE_CONFIG_H
3760484Sobrien# include <config.h>
3860484Sobrien#endif
3960484Sobrien
4060484Sobrien#include "ansidecl.h"
4160484Sobrien
4289857Sobrien#define putenv libiberty_putenv
4389857Sobrien
4460484Sobrien#if HAVE_STDLIB_H
4560484Sobrien# include <stdlib.h>
4660484Sobrien#endif
4760484Sobrien#if HAVE_STRING_H
4860484Sobrien# include <string.h>
4960484Sobrien#endif
5060484Sobrien
5160484Sobrien#ifdef HAVE_ALLOCA_H
5260484Sobrien# include <alloca.h>
5360484Sobrien#else
5460484Sobrien# ifndef alloca
5560484Sobrien#  ifdef __GNUC__
5660484Sobrien#   define alloca __builtin_alloca
5760484Sobrien#  else
5860484Sobrienextern char *alloca ();
5960484Sobrien#  endif /* __GNUC__ */
6060484Sobrien# endif /* alloca */
6160484Sobrien#endif /* HAVE_ALLOCA_H */
6260484Sobrien
6389857Sobrien#undef putenv
6489857Sobrien
6560484Sobrien/* Below this point, it's verbatim code from the glibc-2.0 implementation */
6660484Sobrien
6760484Sobrien
6860484Sobrien/* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
6960484Sobrienint
70218822Sdimputenv (const char *string)
7160484Sobrien{
7260484Sobrien  const char *const name_end = strchr (string, '=');
7360484Sobrien
7460484Sobrien  if (name_end)
7560484Sobrien    {
7660484Sobrien      char *name = (char *) alloca (name_end - string + 1);
7760484Sobrien      memcpy (name, string, name_end - string);
7860484Sobrien      name[name_end - string] = '\0';
7960484Sobrien      return setenv (name, name_end + 1, 1);
8060484Sobrien    }
8160484Sobrien
8260484Sobrien  unsetenv (string);
8360484Sobrien  return 0;
8460484Sobrien}
85