Environment Variables in C
-
03-07-2021 - |
Pregunta
I know this type of question has been asked a lot but none of the answers seem to help. I set an environment variable through setenv()
function call in Ubuntu Linux. However, the program doesn't seem use this environment variables. If I use getenv()
it gets the correct value but the output to the program is wrong. However, when I use export BLOCKSIZE=512
in the shell, the output to the program is correct. I am not spawning different processes from the program. Below is only a code snippet of what I am doing, it is not my whole program.
Is there any reason for this?
Solución
The issue here is that Ubuntu Linux has a default BLOCKSIZE
of 1024
not 512
. Therefore, when counting the blocks in the stat
structure, namely the st_blocks
field, I received a different answer than the normal ls
because in stat
the blocks are only counted in 512 byte blocks. This means that my program would not have to take into account the size of the environment variable. The main issue here was assuming that Linux used a 512 byte block size as I was told in a textbook.
Otros consejos
There is nothing in the code shown that would be affected by the BLOCKSIZE environment variable.
No system call is affected by BLOCKSIZE. I can see nowhere where you use 512. Programs such as ls
get the data from the o/s using the same system calls and then adjust the values that they present to you based on the setting of the environment variable. But the key point is that it is a decision by ls
in user code, not by the kernel in kernel code.
Since your code is not invoking ls
or any other program, there's nothing to be affected by the environment variable.