Difference between revisions of "Interacting with the enviroment"
m |
m (Caretaker: categories) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:C]][[Category:Peerix]] | + | [[Category:C]][[Category:Peerd]][[Category:Peerix]] |
==Enviroment variables== | ==Enviroment variables== | ||
It's quite simple to read and write environment variables from C: | It's quite simple to read and write environment variables from C: | ||
Line 25: | Line 25: | ||
Executes a shell script (printenv) with ''system()'' | Executes a shell script (printenv) with ''system()'' | ||
+ | |||
+ | NOTE: Enviroment variables set by a child process do not affect the same variables in the parent's enviroment. | ||
<pre> | <pre> | ||
Line 38: | Line 40: | ||
=Pipes= | =Pipes= | ||
+ | This example shows how to remain connected to a forked child process, collecting any output it does until it finishes. We might use this kind of interface to enable the [[peerd]] program to utilise existing scripted automation or to simply call other programs with a set of parameters. | ||
+ | |||
+ | NOTE: Enviroment variables set by a child process do not affect the same variables in the parent's enviroment. | ||
*[[Pipe.c]] | *[[Pipe.c]] | ||
*[[hello.sh]] | *[[hello.sh]] | ||
+ | |||
+ | The output of this program is something like: | ||
+ | <pre> | ||
+ | Begin | ||
+ | pipe(): 3 4 | ||
+ | fork() pid 19260 | ||
+ | wait() returned with status 0 | ||
+ | read(): fetched 83 bytes | ||
+ | hello world! | ||
+ | FOO=foo was set by the child | ||
+ | Child changing foo | ||
+ | FOO=child changed foo | ||
+ | |||
+ | Parent: FOO=foo was set by the parent | ||
+ | </pre> | ||
+ | |||
=bash= | =bash= |
Latest revision as of 18:20, 21 March 2007
Enviroment variables
It's quite simple to read and write environment variables from C:
char *envvar; if(envvar = getenv("FOO")) printf( "FOO=%s\n", envvar ); putenv("FOO=bar");
When you call a child process a copy of the enviroment is passed to this process. However, if the process changes enviroment variables, these changes will be lost once the process terminates.
The parent C program:
putenv("FOO=bar"); system("sh printenv"); if(envvar = getenv("FOO")) printf( "FOO=%s\n", envvar );
Executes a shell script (printenv) with system()
NOTE: Enviroment variables set by a child process do not affect the same variables in the parent's enviroment.
echo "printenv: $FOO" FOO=bar2
The resulting output is:
printenv: bar FOO=bar
Pipes
This example shows how to remain connected to a forked child process, collecting any output it does until it finishes. We might use this kind of interface to enable the peerd program to utilise existing scripted automation or to simply call other programs with a set of parameters.
NOTE: Enviroment variables set by a child process do not affect the same variables in the parent's enviroment.
The output of this program is something like:
Begin pipe(): 3 4 fork() pid 19260 wait() returned with status 0 read(): fetched 83 bytes hello world! FOO=foo was set by the child Child changing foo FOO=child changed foo Parent: FOO=foo was set by the parent
bash
To connect to a bash shell you can simply do:
cat > bash
This will create an asynchronous connection to the bash process.