go-unsetenv.c 922 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* go-unsetenv.c -- unset an environment variable from Go.
  2. Copyright 2015 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include "config.h"
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include "runtime.h"
  9. /* Unset an environment variable from Go. This is called by
  10. syscall.Unsetenv. */
  11. void unsetenv_c (String) __asm__ (GOSYM_PREFIX "syscall.unsetenv__c");
  12. void
  13. unsetenv_c (String k)
  14. {
  15. const byte *ks;
  16. unsigned char *kn;
  17. ks = k.str;
  18. if (ks == NULL)
  19. ks = (const byte *) "";
  20. kn = NULL;
  21. #ifdef HAVE_UNSETENV
  22. if (ks[k.len] != 0)
  23. {
  24. kn = malloc (k.len + 1);
  25. if (kn == NULL)
  26. runtime_throw ("out of malloc memory");
  27. __builtin_memcpy (kn, ks, k.len);
  28. ks = kn;
  29. }
  30. unsetenv ((const char *) ks);
  31. #endif /* !defined(HAVE_UNSETENV) */
  32. if (kn != NULL)
  33. free (kn);
  34. }