2011-05-20 02:18:15 +02:00
|
|
|
/* go-setenv.c -- set the C environment from Go.
|
|
|
|
|
|
|
|
Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
2011-05-25 00:21:34 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "runtime.h"
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
/* Set the C environment from Go. This is called by syscall.Setenv. */
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2013-01-24 20:44:23 +01:00
|
|
|
void setenv_c (String, String) __asm__ (GOSYM_PREFIX "syscall.setenv_c");
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
void
|
2012-11-01 04:02:13 +01:00
|
|
|
setenv_c (String k, String v)
|
2011-05-20 02:18:15 +02:00
|
|
|
{
|
2012-11-01 04:02:13 +01:00
|
|
|
const byte *ks;
|
2011-05-20 02:18:15 +02:00
|
|
|
unsigned char *kn;
|
2012-11-01 04:02:13 +01:00
|
|
|
const byte *vs;
|
2011-05-20 02:18:15 +02:00
|
|
|
unsigned char *vn;
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
ks = k.str;
|
2013-01-29 21:52:43 +01:00
|
|
|
if (ks == NULL)
|
|
|
|
ks = (const byte *) "";
|
2011-05-20 02:18:15 +02:00
|
|
|
kn = NULL;
|
2013-01-29 21:52:43 +01:00
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
vs = v.str;
|
2013-01-29 21:52:43 +01:00
|
|
|
if (vs == NULL)
|
|
|
|
vs = (const byte *) "";
|
2011-05-25 00:21:34 +02:00
|
|
|
vn = NULL;
|
|
|
|
|
|
|
|
#ifdef HAVE_SETENV
|
|
|
|
|
2016-11-18 17:03:13 +01:00
|
|
|
if (ks[k.len] != 0)
|
2011-05-20 02:18:15 +02:00
|
|
|
{
|
2016-11-18 17:03:13 +01:00
|
|
|
kn = malloc (k.len + 1);
|
|
|
|
if (kn == NULL)
|
|
|
|
runtime_throw ("out of malloc memory");
|
2012-11-01 04:02:13 +01:00
|
|
|
__builtin_memcpy (kn, ks, k.len);
|
2016-11-18 17:03:13 +01:00
|
|
|
kn[k.len] = '\0';
|
2011-05-20 02:18:15 +02:00
|
|
|
ks = kn;
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:03:13 +01:00
|
|
|
if (vs[v.len] != 0)
|
2011-05-20 02:18:15 +02:00
|
|
|
{
|
2016-11-18 17:03:13 +01:00
|
|
|
vn = malloc (v.len + 1);
|
|
|
|
if (vn == NULL)
|
|
|
|
runtime_throw ("out of malloc memory");
|
2012-11-01 04:02:13 +01:00
|
|
|
__builtin_memcpy (vn, vs, v.len);
|
2016-11-18 17:03:13 +01:00
|
|
|
vn[v.len] = '\0';
|
2011-05-20 02:18:15 +02:00
|
|
|
vs = vn;
|
|
|
|
}
|
|
|
|
|
|
|
|
setenv ((const char *) ks, (const char *) vs, 1);
|
|
|
|
|
2011-05-25 00:21:34 +02:00
|
|
|
#else /* !defined(HAVE_SETENV) */
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
len = k.len + v.len + 2;
|
2016-11-18 17:03:13 +01:00
|
|
|
kn = malloc (len);
|
|
|
|
if (kn == NULL)
|
|
|
|
runtime_throw ("out of malloc memory");
|
2012-11-01 04:02:13 +01:00
|
|
|
__builtin_memcpy (kn, ks, k.len);
|
|
|
|
kn[k.len] = '=';
|
|
|
|
__builtin_memcpy (kn + k.len + 1, vs, v.len);
|
|
|
|
kn[k.len + v.len + 1] = '\0';
|
2011-05-25 00:21:34 +02:00
|
|
|
putenv ((char *) kn);
|
2016-11-18 17:03:13 +01:00
|
|
|
kn = NULL; /* putenv takes ownership of the string. */
|
2011-05-25 00:21:34 +02:00
|
|
|
|
|
|
|
#endif /* !defined(HAVE_SETENV) */
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
if (kn != NULL)
|
2016-11-18 17:03:13 +01:00
|
|
|
free (kn);
|
2011-05-20 02:18:15 +02:00
|
|
|
if (vn != NULL)
|
2016-11-18 17:03:13 +01:00
|
|
|
free (vn);
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|