blob: 409436a0d3f7dd913b704bb2e163d43322769f1f [file] [log] [blame]
Peter Collingbourne93c73eb2015-04-05 23:30:42 +00001/* go-unsetenv.c -- unset an environment variable from Go.
2
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. */
6
7#include "config.h"
8
9#include <stddef.h>
10#include <stdlib.h>
11
12#include "go-alloc.h"
13#include "runtime.h"
14#include "arch.h"
15#include "malloc.h"
16
17/* Unset an environment variable from Go. This is called by
18 syscall.Unsetenv. */
19
20void unsetenv_c (String) __asm__ (GOSYM_PREFIX "syscall.unsetenv_c");
21
22void
23unsetenv_c (String k)
24{
25 const byte *ks;
26 unsigned char *kn;
27 intgo len;
28
29 ks = k.str;
30 if (ks == NULL)
31 ks = (const byte *) "";
32 kn = NULL;
33
34#ifdef HAVE_UNSETENV
35
36 if (ks != NULL && ks[k.len] != 0)
37 {
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;
46 }
47
48 unsetenv ((const char *) ks);
49
50#endif /* !defined(HAVE_UNSETENV) */
51
52 if (kn != NULL)
53 __go_free (kn);
54}