On Sun, Apr 07, 2013 at 12:07:19PM +1000, Darren Oakey wrote:
> I am using a system with no c compiler, no include directories - ie
> they've made some effort to stop you writing c for some reason (even
> though other languages are freely available).   I found tiny cc - and
> particularly PTS-TCC - and thought my problems were solved - hello
> world and other programs worked perfectly.
>
> Then I ran into a snag.
>
> However I try - I can't read the environment :( -    getenv, while
> appearing to be there, always returns null.  I looked at the uclibc
> source and getenv uses __environ - which is null, as is environ, if I
> do a char ** environ.   If I extend main to have void main (int argv,
> char **args, char **envp) - envp appears to be just random numbers.
>
> please help - anyone have any idea how I get at environment variables?
> I'm trying to use this to build a faster cgi on my web host - so
> without environment variables, I'm toast.

It's pretty hacky, but does something like this work?
If not it may be a problem with pts-tcc/statically linking with uclibc.

Rob
#include <stdio.h>
#include <string.h>

char *getenv2(char *nam)
{
	static char buf_ret[256];
	char buf_cmd[256];

	snprintf(buf_cmd, sizeof(buf_cmd), "echo ${%s}", nam);
	FILE *f = popen(buf_cmd, "r");
	char *ret = fgets(buf_ret, sizeof(buf_ret), f);
	pclose(f);

	char *nl;
	if(ret && (nl = strrchr(ret, '\n')))
		*nl = '\0';

	return ret;
}

main()
{
	printf("%s\n", getenv2("HOME"));
}
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to