go-unsetenv.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "go-alloc.h"
  9. #include "runtime.h"
  10. #include "arch.h"
  11. #include "malloc.h"
  12. /* Unset an environment variable from Go. This is called by
  13. syscall.Unsetenv. */
  14. void unsetenv_c (String) __asm__ (GOSYM_PREFIX "syscall.unsetenv_c");
  15. void
  16. unsetenv_c (String k)
  17. {
  18. const byte *ks;
  19. unsigned char *kn;
  20. intgo len;
  21. ks = k.str;
  22. if (ks == NULL)
  23. ks = (const byte *) "";
  24. kn = NULL;
  25. #ifdef HAVE_UNSETENV
  26. if (ks != NULL && ks[k.len] != 0)
  27. {
  28. // Objects that are explicitly freed must be at least 16 bytes in size,
  29. // so that they are not allocated using tiny alloc.
  30. len = k.len + 1;
  31. if (len < TinySize)
  32. len = TinySize;
  33. kn = __go_alloc (len);
  34. __builtin_memcpy (kn, ks, k.len);
  35. ks = kn;
  36. }
  37. unsetenv ((const char *) ks);
  38. #endif /* !defined(HAVE_UNSETENV) */
  39. if (kn != NULL)
  40. __go_free (kn);
  41. }