1169695Skan/* Copyright (C) 1991, 1994, 1995, 1996, 2002 Free Software Foundation, Inc.
2169695Skan   This file based on putenv.c in the GNU C Library.
3169695Skan
4169695Skan   The GNU C Library is free software; you can redistribute it and/or
5169695Skan   modify it under the terms of the GNU Library General Public License as
6169695Skan   published by the Free Software Foundation; either version 2 of the
7169695Skan   License, or (at your option) any later version.
8169695Skan
9169695Skan   The GNU C Library is distributed in the hope that it will be useful,
10169695Skan   but WITHOUT ANY WARRANTY; without even the implied warranty of
11169695Skan   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12169695Skan   Library General Public License for more details.
13169695Skan
14169695Skan   You should have received a copy of the GNU Library General Public
15169695Skan   License along with the GNU C Library; see the file COPYING.LIB.  If not,
16169695Skan   write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
17169695Skan   Boston, MA 02110-1301, USA.  */
18169695Skan
19169695Skan/*
20169695Skan
21169695Skan@deftypefn Supplemental int putenv (const char *@var{string})
22169695Skan
23169695SkanUses @code{setenv} or @code{unsetenv} to put @var{string} into
24169695Skanthe environment or remove it.  If @var{string} is of the form
25169695Skan@samp{name=value} the string is added; if no @samp{=} is present the
26169695Skanname is unset/removed.
27169695Skan
28169695Skan@end deftypefn
29169695Skan
30169695Skan*/
31169695Skan
32169695Skan#if defined (_AIX) && !defined (__GNUC__)
33169695Skan #pragma alloca
34169695Skan#endif
35169695Skan
36169695Skan#if HAVE_CONFIG_H
37169695Skan# include <config.h>
38169695Skan#endif
39169695Skan
40169695Skan#include "ansidecl.h"
41169695Skan
42169695Skan#define putenv libiberty_putenv
43169695Skan
44169695Skan#if HAVE_STDLIB_H
45169695Skan# include <stdlib.h>
46169695Skan#endif
47169695Skan#if HAVE_STRING_H
48169695Skan# include <string.h>
49169695Skan#endif
50169695Skan
51169695Skan#ifdef HAVE_ALLOCA_H
52169695Skan# include <alloca.h>
53169695Skan#else
54169695Skan# ifndef alloca
55169695Skan#  ifdef __GNUC__
56169695Skan#   define alloca __builtin_alloca
57169695Skan#  else
58169695Skanextern char *alloca ();
59169695Skan#  endif /* __GNUC__ */
60169695Skan# endif /* alloca */
61169695Skan#endif /* HAVE_ALLOCA_H */
62169695Skan
63169695Skan#undef putenv
64169695Skan
65169695Skan/* Below this point, it's verbatim code from the glibc-2.0 implementation */
66169695Skan
67169695Skan
68169695Skan/* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
69169695Skanint
70169695Skanputenv (const char *string)
71169695Skan{
72169695Skan  const char *const name_end = strchr (string, '=');
73169695Skan
74169695Skan  if (name_end)
75169695Skan    {
76169695Skan      char *name = (char *) alloca (name_end - string + 1);
77169695Skan      memcpy (name, string, name_end - string);
78169695Skan      name[name_end - string] = '\0';
79169695Skan      return setenv (name, name_end + 1, 1);
80169695Skan    }
81169695Skan
82169695Skan  unsetenv (string);
83169695Skan  return 0;
84169695Skan}
85