pass enviornment variables to popen

pass enviornment variables to popen

Post by sam » Tue, 01 Feb 2005 00:46:09


Hi,

How can I pass enviornment variables to popen() in C?
I want to do something like this:
setenv("KEY_CONF", "/usr/local/etc/ssl/conf/openssl.conf", 1);
popen("openssl <some commandline args> $KEY_CONF", "w");

But the above enviornment variable get removed in calling popen().
Unless I rewrite popen(), I don't see I can pass the enviornment
variables to popen().


Sam.
 
 
 

pass enviornment variables to popen

Post by read_the_s » Tue, 01 Feb 2005 02:13:32


Use popen() to execute shell scripts where you set and export the environment
variables before executing openssl or abandon popen() for fork() and
execve() instead using setenv in the child process just prior to execve().
--
John J. Rushford
http://www.yqcomputer.com/

 
 
 

pass enviornment variables to popen

Post by wall » Tue, 01 Feb 2005 02:20:19


why not try something along the lines of:




setenv("KEY_CONF", "/usr/local/etc/ssl/conf/openssl.conf", 1);
...
char *key_conf=getenv("KEY_CONF");
char pipe_command[255];

//although you should probably restrict the size of that array to avoid out
//of bounds in case the KEY_CONF is set to something so big that it wont fit
//here so you can try something along the lines of:
//*pipe_command=malloc(sizeof("openssl <some commandline args> ") + sizeof
(&key_conf));

sprintf(pipe_command("openssl <some commandline args> %s", pipe_command);
pipe_fp=popen(pipe_command, "w");

--
WINDOWS: "Where do you want to go today?"
LINUX: "Where do you want to go tommorow?"
BSD: "Are you guys coming or what?"
 
 
 

pass enviornment variables to popen

Post by wall » Tue, 01 Feb 2005 02:22:52


oops sorry this line should say:
sprintf(pipe_command("openssl <some commandline args> %s", key_conf);

--
WINDOWS: "Where do you want to go today?"
LINUX: "Where do you want to go tommorow?"
BSD: "Are you guys coming or what?"
 
 
 

pass enviornment variables to popen

Post by ptjm » Tue, 01 Feb 2005 02:35:35


% How can I pass enviornment variables to popen() in C?
% I want to do something like this:
% setenv("KEY_CONF", "/usr/local/etc/ssl/conf/openssl.conf", 1);
% popen("openssl <some commandline args> $KEY_CONF", "w");

This ought to have worked. For instance, try

#include <stdio.h>
#include <stdlib.h>

main()
{
FILE * fp;
char buf[204];

setenv("MYVAR", "My value",1);
fp = popen("echo $MYVAR", "r");
fgets(buf, sizeof(buf), fp);

puts(buf);
}

If you only want the environment variable on the command-line, you
can build the command-line using sprintf.
--

Patrick TJ McPhee
North York Canada
XXXX@XXXXX.COM