-1

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?

tpar44
  • 1,431
  • 4
  • 22
  • 35
  • 2
    Are you sure your `flags[9]` is equal `1`? Your code produces the expected result on ideone ([link](http://ideone.com/3B47R)). – Sergey Kalinichenko Sep 24 '12 at 01:18
  • What is `flags[9]`? If I understand you correctly, you are running the program once with `flags[9]` set to 1, then again with it set to something else. If this is true, then that is the issue. `setenv()` can not modify the parent's environment variables. This is by design, for security reasons. – Foran Sep 24 '12 at 01:21
  • @dasblinkenlight yes I am sure that flags[9] == 1 – tpar44 Sep 24 '12 at 01:27
  • @Foran No, that is not what I am doing. I always run it with flags[9] = 1 but it does not produce the expected result. It will only produce the expected result when I use `export BLOCKSIZE=512` in the shell and then run the program with flags[9] = 1 – tpar44 Sep 24 '12 at 01:29
  • 1
    @tpar44 What is the return value of `setenv()` in this case? also I might suggest removing the conditional all together as it only clouds the issue. – Foran Sep 24 '12 at 01:46
  • @Foran I know the variable is set because when I use getenv, it returns 512 but when I do other calculations, like counting the number of 512 byte blocks a file uses, the answer is wrong but when I use `export BLOCKSIZE=512` it works fine. I dont have `source` or `export` anything in the program do I? – tpar44 Sep 24 '12 at 01:49
  • @tpar44 so you're saying that getenv() returns 512, but the OS isn't honoring the value? – Foran Sep 24 '12 at 01:52
  • @Foran yes, that was it seems like to me – tpar44 Sep 24 '12 at 01:53
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17040/discussion-between-foran-and-tpar44) – Foran Sep 24 '12 at 01:53
  • 1
    Talk about "the expected result" is not helpful, as expectations are often incorrect. You need to state clearly what you expect and what you get instead. – Jim Balter Sep 24 '12 at 01:54
  • 1
    'This is by design, for security reasons.' -- No, although that might be a fortuitous consequence of the design. An implementation in which a change to a child process's environment variables would change the parent process's environment variables would have been considerably more complex -- probably involving environment storage allocated to process groups (which didn't exist when environment variables were invented) -- and much less useful. – Jim Balter Sep 24 '12 at 02:09

2 Answers2

1

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.

tpar44
  • 1,431
  • 4
  • 22
  • 35
0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278