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>
|
|
|
|
|
|
|
|
#include "go-alloc.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "runtime.h"
|
2014-06-07 00:37:27 +02:00
|
|
|
#include "arch.h"
|
|
|
|
#include "malloc.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;
|
2014-06-07 00:37:27 +02:00
|
|
|
intgo len;
|
2011-05-20 02:18:15 +02:00
|
|
|
|
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
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
if (ks != NULL && ks[k.len] != 0)
|
2011-05-20 02:18:15 +02:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
// Objects that are explicitly freed must be at least 16 bytes in size,
|
|
|
|
// so that they are not allocated using tiny alloc.
|
|
|
|
len = k.len + 1;
|
|
|
|
if (len < TinySize)
|
|
|
|
len = TinySize;
|
|
|
|
kn = __go_alloc (len);
|
2012-11-01 04:02:13 +01:00
|
|
|
__builtin_memcpy (kn, ks, k.len);
|
2011-05-20 02:18:15 +02:00
|
|
|
ks = kn;
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
if (vs != NULL && vs[v.len] != 0)
|
2011-05-20 02:18:15 +02:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
len = v.len + 1;
|
|
|
|
if (len < TinySize)
|
|
|
|
len = TinySize;
|
|
|
|
vn = __go_alloc (len);
|
2012-11-01 04:02:13 +01:00
|
|
|
__builtin_memcpy (vn, vs, v.len);
|
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;
|
|
|
|
if (len < TinySize)
|
|
|
|
len = TinySize;
|
|
|
|
kn = __go_alloc (len);
|
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);
|
|
|
|
|
|
|
|
#endif /* !defined(HAVE_SETENV) */
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
if (kn != NULL)
|
|
|
|
__go_free (kn);
|
|
|
|
if (vn != NULL)
|
|
|
|
__go_free (vn);
|
|
|
|
}
|