Tuesday, October 19, 2010

Executing Wild Cards from C programs

We all know that we can execute some simple commands using system(...) call form c program.
we will use execl(...) to do that.

First we will understand the difference excel(..) and system(...) calls:

1) execl(...) it executes as it is by doing fork.. so wild cards will not be effected.
but system(...) call spawns shell and does fork and and execute the command provided as arguments.

2) System(...) is blocking call. It blocks the flow of execution. Where as execl(...) is not that it just spawns a fork and the parent process will not get blocked.

we can't use wildcards using execl(...) reason is execl() will not spawn any shell where in wildcards can be understood. so if we would like to use wildcards then how we should write is

execl("/bin/bash" ,"bash", "-c", "rm -f /home/cc/rr* ", (char*)0);

send the parameters as arguments to bash. then it works..

Same can be executed as
bash$ bash -p rm -f /home/cc/rr*

No comments:

Post a Comment