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

71 lines
1.4 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"
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
2014-09-21 17:33:12 +00:00
if (ks != NULL && ks[k.len] != 0)
2012-03-27 23:13:14 +00:00
{
2014-09-21 17:33:12 +00:00
kn = __go_alloc (k.len + 1);
__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
{
2014-09-21 17:33:12 +00:00
vn = __go_alloc (v.len + 1);
__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) */
2014-09-21 17:33:12 +00:00
kn = __go_alloc (k.len + v.len + 2);
__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);
}