[Lazarus] SetEnv on Linux 64bit

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Wed Apr 4 14:09:27 CEST 2012



On Wed, 4 Apr 2012, Honza wrote:

> 2012/4/4  <michael.vancanneyt at wisa.be>:
>> Not the current.
>>
>> The EXTERNAL variable environ, i.e. the one that the kernel passed on,
>> which cannot be modified. You can only modify your local copy.
>
> 14:00 myname at tux64:~/tmp/c$ ls
> a.c  b.c
> 14:00 myname at tux64:~/tmp/c$ cat a.c
> #include <stdlib.h>
> #include <stdio.h>
> #include <unistd.h>
>
> main() {
> 	char *e = getenv("MYVAR");
> 	printf("a1: %s\n", e);
> 	setenv("MYVAR", "MYVALUE", 1);
> 	e = getenv("MYVAR");
> 	printf("a2: %s\n", e);
> 	execv("./b.out", NULL);
> }
> 14:00 myname at tux64:~/tmp/c$ cat b.c
> #include <stdlib.h>
> #include <stdio.h>
>
> main() {
> 	char *e = getenv("MYVAR");
> 	printf("b: %s\n", e);
> }
>
> 14:00 myname at tux64:~/tmp/c$ gcc -o a.out a.c && gcc -o b.out b.c && ls
> a.c  a.out  b.c  b.out
> 14:00 myname at tux64:~/tmp/c$ ./a.out
> a1: (null)
> a2: MYVALUE
> b: MYVALUE
> 14:00 myname at tux64:~/tmp/c$

Output of strace:

brk(0)                                  = 0x21a5000 
brk(0x21c6000)                          = 0x21c6000 
write(1, "a2: MYVALUE\n", 12a2: MYVALUE 
)           = 12 
execve("./b.out", [0], [/* 57 vars */]) = 0 
brk(0)                                  = 0x189e000 
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or
directory)

As you can see, it uses execve() behind the scenes to be able to pass the modified
local environment.

This is libc-specific behaviour.

Michael.




More information about the Lazarus mailing list