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

80 lines
1.6 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>
2014-09-21 17:33:12 +00:00
#include "runtime.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;
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
if (ks[k.len] != 0)
2012-03-27 23:13:14 +00:00
{
kn = malloc (k.len + 1);
if (kn == NULL)
runtime_throw ("out of malloc memory");
2014-09-21 17:33:12 +00:00
__builtin_memcpy (kn, ks, k.len);
kn[k.len] = '\0';
2012-03-27 23:13:14 +00:00
ks = kn;
}
if (vs[v.len] != 0)
2012-03-27 23:13:14 +00:00
{
vn = malloc (v.len + 1);
if (vn == NULL)
runtime_throw ("out of malloc memory");
2014-09-21 17:33:12 +00:00
__builtin_memcpy (vn, vs, v.len);
vn[v.len] = '\0';
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;
kn = malloc (len);
if (kn == NULL)
runtime_throw ("out of malloc memory");
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);
kn = NULL; /* putenv takes ownership of the string. */
2012-03-27 23:13:14 +00:00
#endif /* !defined(HAVE_SETENV) */
if (kn != NULL)
free (kn);
2012-03-27 23:13:14 +00:00
if (vn != NULL)
free (vn);
2012-03-27 23:13:14 +00:00
}