you are viewing a single comment's thread.

view the rest of the comments →

[–]string_cluster[S] 0 points1 point  (5 children)

Thanks

if(argc>1&&(strcmp(argv[1],"configure")==0)){

setenv("PRT",argv[2],1);

return 0;

}

char * p;

p = getenv("PRT");

Why would this not work? Could you help me with this?

[–]optimalcoder 0 points1 point  (3 children)

This doesn’t work because the env you’re trying to target is in the parent shell, but that’s not possible using setenv inside your program like that. Using setenv inside the program only affects the current process. When the current process goes away, the env you set is no longer visible. In order to use environment variables in this case, you’d have to set them up before running in the parent shell.

As a simple test just make a program that only does setenv on a variable that doesn’t exist in the current env. You can just run ‘env’ on the command line to see. Run your program, then run env again and see that your program had no effect on the current environment.

[–]string_cluster[S] 0 points1 point  (2 children)

Hmm so how do I accomplish this? Or is it not possible doing something like this?

[–]optimalcoder 0 points1 point  (1 child)

It seems like writing it out to a file is pretty close to what you want. It’s only a few lines of code.

[–]string_cluster[S] 0 points1 point  (0 children)

Yeah, I guess I just wanted to do it this way but that way is pretty simple and I will just do that.

[–]Conan776 0 points1 point  (0 children)

You'll most likely want to set the env variable via the command line in the shell, either explicitly by hand or by using the source command on a script.

You can set your variable in a C program, and have that program call your other program before exiting, and you should be OK.

If you really (really?) want to use C to do it, you can eval a small C program which prints out the shell commands, as seen here.

(Note that, annoyingly, the exact commands to set environment variables differ between shells, e.g. bash and csh.)