github.com/golang/gofrontend@v0.0.0-20240429183944-60f985a78526/libgo/runtime/go-unsetenv.c (about)

     1  /* 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 "runtime.h"
    13  
    14  /* Unset an environment variable from Go.  This is called by
    15     syscall.Unsetenv.  */
    16  
    17  void unsetenv_c (String) __asm__ (GOSYM_PREFIX "syscall.unsetenv__c");
    18  
    19  void
    20  unsetenv_c (String k)
    21  {
    22    const byte *ks;
    23    unsigned char *kn;
    24  
    25    ks = k.str;
    26    if (ks == NULL)
    27      ks = (const byte *) "";
    28    kn = NULL;
    29  
    30  #ifdef HAVE_UNSETENV
    31  
    32    if (ks[k.len] != 0)
    33      {
    34        kn = malloc (k.len + 1);
    35        if (kn == NULL)
    36  	runtime_throw ("out of malloc memory");
    37        __builtin_memcpy (kn, ks, k.len);
    38        ks = kn;
    39      }
    40  
    41    unsetenv ((const char *) ks);
    42  
    43  #endif /* !defined(HAVE_UNSETENV) */
    44  
    45    if (kn != NULL)
    46      free (kn);
    47  }