1 /* env.c -- SRFI-98 environment interface */
2 /* Copyright (c) 2009-2012 Alex Shinn. All rights reserved. */
3 /* BSD-style license: http://synthcode.com/license.txt */
4
5 #ifdef __APPLE__
6 #include <crt_externs.h>
7 #define environ (*_NSGetEnviron())
8 #elif !defined(_WIN32) && !defined(PLAN9)
9 extern char **environ;
10 #endif
11
12 #include <chibi/eval.h>
13
sexp_get_environment_variable(sexp ctx,sexp self,sexp_sint_t n,sexp str)14 sexp sexp_get_environment_variable (sexp ctx, sexp self, sexp_sint_t n, sexp str) {
15 char *cstr;
16 if (! sexp_stringp(str))
17 return sexp_type_exception(ctx, self, SEXP_STRING, str);
18 cstr = getenv(sexp_string_data(str));
19 return cstr ? sexp_c_string(ctx, cstr, -1) : SEXP_FALSE;
20 }
21
sexp_get_environment_variables(sexp ctx,sexp self,sexp_sint_t n)22 sexp sexp_get_environment_variables (sexp ctx, sexp self, sexp_sint_t n) {
23 int i;
24 char **env, *cname, *cval;
25 sexp_gc_var3(res, name, val);
26 sexp_gc_preserve3(ctx, res, name, val);
27 res = SEXP_NULL;
28 #ifndef PLAN9
29 env = environ;
30 for (i=0; env[i]; i++) {
31 cname = env[i];
32 cval = strchr(cname, '=');
33 if (cval) {
34 name = sexp_c_string(ctx, cname, cval-cname);
35 val = sexp_c_string(ctx, cval+1, -1);
36 val = sexp_cons(ctx, name, val);
37 res = sexp_cons(ctx, val, res);
38 }
39 }
40 #endif
41 sexp_gc_release3(ctx);
42 return res;
43 }
44
sexp_init_library(sexp ctx,sexp self,sexp_sint_t n,sexp env,const char * version,const sexp_abi_identifier_t abi)45 sexp sexp_init_library (sexp ctx, sexp self, sexp_sint_t n, sexp env, const char* version, const sexp_abi_identifier_t abi) {
46 if (!(sexp_version_compatible(ctx, version, sexp_version)
47 && sexp_abi_compatible(ctx, abi, SEXP_ABI_IDENTIFIER)))
48 return SEXP_ABI_ERROR;
49 sexp_define_foreign(ctx, env, "get-environment-variable", 1, sexp_get_environment_variable);
50 sexp_define_foreign(ctx, env, "get-environment-variables", 0, sexp_get_environment_variables);
51 return SEXP_VOID;
52 }
53
54