Search code examples
shellunixpipeline

Unix - pipeline ls - la | less C executable giving double total file size vs shell


*Edit - Stephen has answered this question in the comments below *

so basically I have made two seperate child processes(using two seperate methods with their own fork) to execute the command ls -la | less using pipe.

The first one executes ls like this:

execl("/bin/ls", "ls", "-la", NULL);

The second child process executes less like this:

execlp("less", "less", NULL);

And the results come up fine.. apart from one little part:

Results using shell command:

total 15
drwxr-xr-x 2 daniel staff 4 2015-02-27 18:58 .
drwxr-xr-x 15 daniel staff 24 2015-02-27 18:58 ..
-rwxr-xr-x 1 daniel staff 9280 2015-02-27 18:58 pipes
-rw-r--r-- 1 daniel staff 1419 2015-02-27 18:58 pipes.c

Results using my executable:

total 30
drwxr-xr-x 2 daniel staff 4 Feb 27 18:58 .
drwxr-xr-x 15 daniel staff 24 Feb 27 18:58 ..
-rwxr-xr-x 1 daniel staff 9280 Feb 27 18:58 pipes
-rw-r--r-- 1 daniel staff 1419 Feb 27 18:58 pipes.c

Now the date being a different format I don't care about.. but the total size is twice as large with my executable(30 vs 15). Why is this happening?


Solution

  • Make sure that the ls you are running from the shell and the ls that is running in your program are the same program.

    Your program is specifying /bin/ls as the program to run; you can find out what is being run when you type the command at the shell prompt by using the shell command which ls (also see type ls).

    If these are different it could be due to the POSIX vs. GNU blocksize used in the total size computation.