2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-setenv.c
blob78717f4705ab383281dde0a4f67568392fc5bc60
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 "go-string.h"
15 /* Set the C environment from Go. This is called by syscall.Setenv. */
17 void setenv_c (struct __go_string, struct __go_string)
18 __asm__ ("libgo_syscall.syscall.setenv_c");
20 void
21 setenv_c (struct __go_string k, struct __go_string v)
23 const unsigned char *ks;
24 unsigned char *kn;
25 const unsigned char *vs;
26 unsigned char *vn;
28 ks = k.__data;
29 kn = NULL;
30 vs = v.__data;
31 vn = NULL;
33 #ifdef HAVE_SETENV
35 if (ks[k.__length] != 0)
37 kn = __go_alloc (k.__length + 1);
38 __builtin_memcpy (kn, ks, k.__length);
39 ks = kn;
42 if (vs[v.__length] != 0)
44 vn = __go_alloc (v.__length + 1);
45 __builtin_memcpy (vn, vs, v.__length);
46 vs = vn;
49 setenv ((const char *) ks, (const char *) vs, 1);
51 #else /* !defined(HAVE_SETENV) */
53 kn = malloc (k.__length + v.__length + 2);
54 __builtin_memcpy (kn, ks, k.__length);
55 kn[k.__length] = '=';
56 __builtin_memcpy (kn + k.__length + 1, vs, v.__length);
57 kn[k.__length + v.__length + 1] = '\0';
58 putenv ((char *) kn);
60 #endif /* !defined(HAVE_SETENV) */
62 if (kn != NULL)
63 __go_free (kn);
64 if (vn != NULL)
65 __go_free (vn);