2015-09-10 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-unsetenv.c
blob409436a0d3f7dd913b704bb2e163d43322769f1f
1 /* go-unsetenv.c -- unset an environment variable from Go.
3 Copyright 2015 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 #include "config.h"
9 #include <stddef.h>
10 #include <stdlib.h>
12 #include "go-alloc.h"
13 #include "runtime.h"
14 #include "arch.h"
15 #include "malloc.h"
17 /* Unset an environment variable from Go. This is called by
18 syscall.Unsetenv. */
20 void unsetenv_c (String) __asm__ (GOSYM_PREFIX "syscall.unsetenv_c");
22 void
23 unsetenv_c (String k)
25 const byte *ks;
26 unsigned char *kn;
27 intgo len;
29 ks = k.str;
30 if (ks == NULL)
31 ks = (const byte *) "";
32 kn = NULL;
34 #ifdef HAVE_UNSETENV
36 if (ks != NULL && ks[k.len] != 0)
38 // Objects that are explicitly freed must be at least 16 bytes in size,
39 // so that they are not allocated using tiny alloc.
40 len = k.len + 1;
41 if (len < TinySize)
42 len = TinySize;
43 kn = __go_alloc (len);
44 __builtin_memcpy (kn, ks, k.len);
45 ks = kn;
48 unsetenv ((const char *) ks);
50 #endif /* !defined(HAVE_UNSETENV) */
52 if (kn != NULL)
53 __go_free (kn);