putenv.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
  2. This file based on putenv.c in the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /*
  16. @deftypefn Supplemental int putenv (const char *@var{string})
  17. Uses @code{setenv} or @code{unsetenv} to put @var{string} into
  18. the environment or remove it. If @var{string} is of the form
  19. @samp{name=value} the string is added; if no @samp{=} is present the
  20. name is unset/removed.
  21. @end deftypefn
  22. */
  23. #if defined (_AIX) && !defined (__GNUC__)
  24. #pragma alloca
  25. #endif
  26. #if HAVE_CONFIG_H
  27. # include <config.h>
  28. #endif
  29. #include "ansidecl.h"
  30. #define putenv libiberty_putenv
  31. #if HAVE_STDLIB_H
  32. # include <stdlib.h>
  33. #endif
  34. #if HAVE_STRING_H
  35. # include <string.h>
  36. #endif
  37. #ifdef HAVE_ALLOCA_H
  38. # include <alloca.h>
  39. #else
  40. # ifndef alloca
  41. # ifdef __GNUC__
  42. # define alloca __builtin_alloca
  43. # else
  44. extern char *alloca ();
  45. # endif /* __GNUC__ */
  46. # endif /* alloca */
  47. #endif /* HAVE_ALLOCA_H */
  48. #undef putenv
  49. /* Below this point, it's verbatim code from the glibc-2.0 implementation */
  50. /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
  51. int
  52. putenv (const char *string)
  53. {
  54. const char *const name_end = strchr (string, '=');
  55. if (name_end)
  56. {
  57. char *name = (char *) alloca (name_end - string + 1);
  58. memcpy (name, string, name_end - string);
  59. name[name_end - string] = '\0';
  60. return setenv (name, name_end + 1, 1);
  61. }
  62. unsetenv (string);
  63. return 0;
  64. }