Retro68/gcc/libgo/runtime/go-setenv.c

85 lines
1.8 KiB
C
Raw Normal View History

2012-03-27 23:13:14 +00: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. */
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include "go-alloc.h"
2014-09-21 17:33:12 +00:00
#include "runtime.h"
2015-08-28 15:33:40 +00:00
#include "arch.h"
#include "malloc.h"
2012-03-27 23:13:14 +00:00
/* Set the C environment from Go. This is called by syscall.Setenv. */
2014-09-21 17:33:12 +00:00
void setenv_c (String, String) __asm__ (GOSYM_PREFIX "syscall.setenv_c");
2012-03-27 23:13:14 +00:00
void
2014-09-21 17:33:12 +00:00
setenv_c (String k, String v)
2012-03-27 23:13:14 +00:00
{
2014-09-21 17:33:12 +00:00
const byte *ks;
2012-03-27 23:13:14 +00:00
unsigned char *kn;
2014-09-21 17:33:12 +00:00
const byte *vs;
2012-03-27 23:13:14 +00:00
unsigned char *vn;
2015-08-28 15:33:40 +00:00
intgo len;
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
ks = k.str;
if (ks == NULL)
ks = (const byte *) "";
2012-03-27 23:13:14 +00:00
kn = NULL;
2014-09-21 17:33:12 +00:00
vs = v.str;
if (vs == NULL)
vs = (const byte *) "";
2012-03-27 23:13:14 +00:00
vn = NULL;
#ifdef HAVE_SETENV
2014-09-21 17:33:12 +00:00
if (ks != NULL && ks[k.len] != 0)
2012-03-27 23:13:14 +00:00
{
2015-08-28 15:33:40 +00: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);
2014-09-21 17:33:12 +00:00
__builtin_memcpy (kn, ks, k.len);
2012-03-27 23:13:14 +00:00
ks = kn;
}
2014-09-21 17:33:12 +00:00
if (vs != NULL && vs[v.len] != 0)
2012-03-27 23:13:14 +00:00
{
2015-08-28 15:33:40 +00:00
len = v.len + 1;
if (len < TinySize)
len = TinySize;
vn = __go_alloc (len);
2014-09-21 17:33:12 +00:00
__builtin_memcpy (vn, vs, v.len);
2012-03-27 23:13:14 +00:00
vs = vn;
}
setenv ((const char *) ks, (const char *) vs, 1);
#else /* !defined(HAVE_SETENV) */
2015-08-28 15:33:40 +00:00
len = k.len + v.len + 2;
if (len < TinySize)
len = TinySize;
kn = __go_alloc (len);
2014-09-21 17:33:12 +00: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';
2012-03-27 23:13:14 +00:00
putenv ((char *) kn);
#endif /* !defined(HAVE_SETENV) */
if (kn != NULL)
__go_free (kn);
if (vn != NULL)
__go_free (vn);
}