putenv.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 1991, 1994, 1995, 1996, 2002 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. }