ls -l | less
is actually a shell command line that consists of two processes connected by a pipe. The execvp()
call can only spawn a single process.
If you want to do this from your program, you must invoke the shell explicitly - either by using the system()
call, or by changing the command line to sh -c 'ls -l | less'
. Your word
array should look like this:
word[0] = "sh"
word[1] = "-c"
word[2] = "ls -l | less"
word[3] = NULL
[EDIT] Alternatively, you could do what the shell is doing internally: spawn two processes and connect them with a pipe. This would involve using the fork()
, pipe()
, dup2()
and execve()
calls. However, invoking the shell is much less work, and since less
is an interactive program anyway, you don't need to worry about performance that much: anything that takes less than 100 ms is perceived as instantaneous.