Login
7 branches 0 tags
Ben (X13/Arch) More detailed perf. report c2a2449 3 years ago 747 Commits
nujel / bin / operation / environment.c
/* Nujel - Copyright (C) 2020-2022 - Benjamin Vincent Schulenburg
 * This project uses the MIT license, a copy should be included under /LICENSE */
#include "io.h"
#include "../misc.h"

#include <unistd.h>

/* Add environment key/value pair to tree T */
static lTree *addVar(const char *e, lTree *t){
	int endOfKey, endOfString;
	for(endOfKey=0;e[endOfKey] != '=';endOfKey++){}
	for(endOfString=endOfKey+1;e[endOfString];endOfString++){}
	lSymbol *sym = lSymSL(e,endOfKey);
	lVal *v = lValStringNoCopy(&e[endOfKey+1], endOfString);
	return lTreeInsert(t, sym, v);
}

#if (defined(__MSYS__)) || (defined(__MINGW32__))

#include <windows.h>

/* Windows specific - add Environment args to `environment/variables` */
void initEnvironmentMap(lClosure *c){
	lTree *t = NULL;
	LPCH env = GetEnvironmentStrings();
	while(*env){
		t = addVar(env,t);
		while(*env++){}
	}
	lVal *et = lValTree(t);
	lDefineClosureSym(c,lSymS("System/Environment"),et);
}

#else
extern char **environ;

/* Add Environment args to `environment/variables` */
void initEnvironmentMap(lClosure *c){
	lTree *t = NULL;
	for(int i=0;environ[i];i++){
		t = addVar(environ[i],t);
	}
	lVal *env = lValTree(t);
	lDefineClosureSym(c,lSymS("System/Environment"),env);
}
#endif