Search code examples
cenvironment-variablessetenvgetenv

Environment Variables in C


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?


Solution

  • 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.