xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/putenv.c (revision 7e120ff03ede3fe64e2c8620c01465d528502ddb)
1*7e120ff0Schristos /* Copyright (C) 1991-2024 Free Software Foundation, Inc.
298b9484cSchristos    This file based on putenv.c in the GNU C Library.
398b9484cSchristos 
498b9484cSchristos    The GNU C Library is free software; you can redistribute it and/or
598b9484cSchristos    modify it under the terms of the GNU Library General Public License as
698b9484cSchristos    published by the Free Software Foundation; either version 2 of the
798b9484cSchristos    License, or (at your option) any later version.
898b9484cSchristos 
998b9484cSchristos    The GNU C Library is distributed in the hope that it will be useful,
1098b9484cSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1198b9484cSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1298b9484cSchristos    Library General Public License for more details.
1398b9484cSchristos 
1498b9484cSchristos    You should have received a copy of the GNU Library General Public
1598b9484cSchristos    License along with the GNU C Library; see the file COPYING.LIB.  If not,
1698b9484cSchristos    write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1798b9484cSchristos    Boston, MA 02110-1301, USA.  */
1898b9484cSchristos 
1998b9484cSchristos /*
2098b9484cSchristos 
2198b9484cSchristos @deftypefn Supplemental int putenv (const char *@var{string})
2298b9484cSchristos 
2398b9484cSchristos Uses @code{setenv} or @code{unsetenv} to put @var{string} into
2498b9484cSchristos the environment or remove it.  If @var{string} is of the form
2598b9484cSchristos @samp{name=value} the string is added; if no @samp{=} is present the
2698b9484cSchristos name is unset/removed.
2798b9484cSchristos 
2898b9484cSchristos @end deftypefn
2998b9484cSchristos 
3098b9484cSchristos */
3198b9484cSchristos 
3298b9484cSchristos #if defined (_AIX) && !defined (__GNUC__)
3398b9484cSchristos  #pragma alloca
3498b9484cSchristos #endif
3598b9484cSchristos 
3698b9484cSchristos #if HAVE_CONFIG_H
3798b9484cSchristos # include <config.h>
3898b9484cSchristos #endif
3998b9484cSchristos 
4098b9484cSchristos #include "ansidecl.h"
4198b9484cSchristos 
4298b9484cSchristos #define putenv libiberty_putenv
4398b9484cSchristos 
4498b9484cSchristos #if HAVE_STDLIB_H
4598b9484cSchristos # include <stdlib.h>
4698b9484cSchristos #endif
4798b9484cSchristos #if HAVE_STRING_H
4898b9484cSchristos # include <string.h>
4998b9484cSchristos #endif
5098b9484cSchristos 
5198b9484cSchristos #ifdef HAVE_ALLOCA_H
5298b9484cSchristos # include <alloca.h>
5398b9484cSchristos #else
5498b9484cSchristos # ifndef alloca
5598b9484cSchristos #  ifdef __GNUC__
5698b9484cSchristos #   define alloca __builtin_alloca
5798b9484cSchristos #  else
5898b9484cSchristos extern char *alloca ();
5998b9484cSchristos #  endif /* __GNUC__ */
6098b9484cSchristos # endif /* alloca */
6198b9484cSchristos #endif /* HAVE_ALLOCA_H */
6298b9484cSchristos 
6398b9484cSchristos #undef putenv
6498b9484cSchristos 
6598b9484cSchristos /* Below this point, it's verbatim code from the glibc-2.0 implementation */
6698b9484cSchristos 
6798b9484cSchristos 
6898b9484cSchristos /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
6998b9484cSchristos int
7098b9484cSchristos putenv (const char *string)
7198b9484cSchristos {
7298b9484cSchristos   const char *const name_end = strchr (string, '=');
7398b9484cSchristos 
7498b9484cSchristos   if (name_end)
7598b9484cSchristos     {
7698b9484cSchristos       char *name = (char *) alloca (name_end - string + 1);
7798b9484cSchristos       memcpy (name, string, name_end - string);
7898b9484cSchristos       name[name_end - string] = '\0';
7998b9484cSchristos       return setenv (name, name_end + 1, 1);
8098b9484cSchristos     }
8198b9484cSchristos 
8298b9484cSchristos   unsetenv (string);
8398b9484cSchristos   return 0;
8498b9484cSchristos }
85