2013-01-12 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-setenv.c
blob41f14d4b7343c9acf2b5fbffe74e5342d3e70489
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__ ("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 kn = NULL;
29 vs = v.str;
30 vn = NULL;
32 #ifdef HAVE_SETENV
34 if (ks[k.len] != 0)
36 kn = __go_alloc (k.len + 1);
37 __builtin_memcpy (kn, ks, k.len);
38 ks = kn;
41 if (vs[v.len] != 0)
43 vn = __go_alloc (v.len + 1);
44 __builtin_memcpy (vn, vs, v.len);
45 vs = vn;
48 setenv ((const char *) ks, (const char *) vs, 1);
50 #else /* !defined(HAVE_SETENV) */
52 kn = __go_alloc (k.len + v.len + 2);
53 __builtin_memcpy (kn, ks, k.len);
54 kn[k.len] = '=';
55 __builtin_memcpy (kn + k.len + 1, vs, v.len);
56 kn[k.len + v.len + 1] = '\0';
57 putenv ((char *) kn);
59 #endif /* !defined(HAVE_SETENV) */
61 if (kn != NULL)
62 __go_free (kn);
63 if (vn != NULL)
64 __go_free (vn);