Importing Upstream version 4.8.2
[platform/upstream/gcc48.git] / libgo / runtime / go-setenv.c
1 /* go-setenv.c -- set the C environment from Go.
2
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.  */
6
7 #include "config.h"
8
9 #include <stddef.h>
10 #include <stdlib.h>
11
12 #include "go-alloc.h"
13 #include "runtime.h"
14
15 /* Set the C environment from Go.  This is called by syscall.Setenv.  */
16
17 void setenv_c (String, String) __asm__ (GOSYM_PREFIX "syscall.setenv_c");
18
19 void
20 setenv_c (String k, String v)
21 {
22   const byte *ks;
23   unsigned char *kn;
24   const byte *vs;
25   unsigned char *vn;
26
27   ks = k.str;
28   if (ks == NULL)
29     ks = (const byte *) "";
30   kn = NULL;
31
32   vs = v.str;
33   if (vs == NULL)
34     vs = (const byte *) "";
35   vn = NULL;
36
37 #ifdef HAVE_SETENV
38
39   if (ks != NULL && ks[k.len] != 0)
40     {
41       kn = __go_alloc (k.len + 1);
42       __builtin_memcpy (kn, ks, k.len);
43       ks = kn;
44     }
45
46   if (vs != NULL && vs[v.len] != 0)
47     {
48       vn = __go_alloc (v.len + 1);
49       __builtin_memcpy (vn, vs, v.len);
50       vs = vn;
51     }
52
53   setenv ((const char *) ks, (const char *) vs, 1);
54
55 #else /* !defined(HAVE_SETENV) */
56
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);
63
64 #endif /* !defined(HAVE_SETENV) */
65
66   if (kn != NULL)
67     __go_free (kn);
68   if (vn != NULL)
69     __go_free (vn);
70 }