/cp
[official-gcc.git] / libgo / runtime / go-setenv.c
blob6c7378c9ecf3477c0bff152999533660285d3b00
1 /* go-setenv.c -- set the C environment from Go.
3 Copyright 2011 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"
15 /* Set the C environment from Go. This is called by syscall.Setenv. */
17 void setenv_c (String, String) __asm__ (GOSYM_PREFIX "syscall.setenv_c");
19 void
20 setenv_c (String k, String v)
22 const byte *ks;
23 unsigned char *kn;
24 const byte *vs;
25 unsigned char *vn;
27 ks = k.str;
28 if (ks == NULL)
29 ks = (const byte *) "";
30 kn = NULL;
32 vs = v.str;
33 if (vs == NULL)
34 vs = (const byte *) "";
35 vn = NULL;
37 #ifdef HAVE_SETENV
39 if (ks != NULL && ks[k.len] != 0)
41 kn = __go_alloc (k.len + 1);
42 __builtin_memcpy (kn, ks, k.len);
43 ks = kn;
46 if (vs != NULL && vs[v.len] != 0)
48 vn = __go_alloc (v.len + 1);
49 __builtin_memcpy (vn, vs, v.len);
50 vs = vn;
53 setenv ((const char *) ks, (const char *) vs, 1);
55 #else /* !defined(HAVE_SETENV) */
57 kn = __go_alloc (k.len + v.len + 2);
58 __builtin_memcpy (kn, ks, k.len);
59 kn[k.len] = '=';
60 __builtin_memcpy (kn + k.len + 1, vs, v.len);
61 kn[k.len + v.len + 1] = '\0';
62 putenv ((char *) kn);
64 #endif /* !defined(HAVE_SETENV) */
66 if (kn != NULL)
67 __go_free (kn);
68 if (vn != NULL)
69 __go_free (vn);