summaryrefslogtreecommitdiff
path: root/libgo/runtime/go-unsetenv.c
blob: 409436a0d3f7dd913b704bb2e163d43322769f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* go-unsetenv.c -- unset an environment variable from Go.

   Copyright 2015 The Go Authors. All rights reserved.
   Use of this source code is governed by a BSD-style
   license that can be found in the LICENSE file.  */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>

#include "go-alloc.h"
#include "runtime.h"
#include "arch.h"
#include "malloc.h"

/* Unset an environment variable from Go.  This is called by
   syscall.Unsetenv.  */

void unsetenv_c (String) __asm__ (GOSYM_PREFIX "syscall.unsetenv_c");

void
unsetenv_c (String k)
{
  const byte *ks;
  unsigned char *kn;
  intgo len;

  ks = k.str;
  if (ks == NULL)
    ks = (const byte *) "";
  kn = NULL;

#ifdef HAVE_UNSETENV

  if (ks != NULL && ks[k.len] != 0)
    {
      // Objects that are explicitly freed must be at least 16 bytes in size,
      // so that they are not allocated using tiny alloc.
      len = k.len + 1;
      if (len < TinySize)
	len = TinySize;
      kn = __go_alloc (len);
      __builtin_memcpy (kn, ks, k.len);
      ks = kn;
    }

  unsetenv ((const char *) ks);

#endif /* !defined(HAVE_UNSETENV) */

  if (kn != NULL)
    __go_free (kn);
}