how does exec() do it? on successful call of exec() family of functions, they never return...
how to i emulate that.
assume the standard example,
execl("/bin/ls","ls",NULL);
now this would never return.
i m trying to emulate exec()'s never to return feature...
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
void myexec(const char* path,const char* argv)
{
chdir(path);
system(argv);
exit(100);
}
int main(int argc,char *argv[])
{
if(argc!=3)
{
printf("USAGE:%s <path> <command>\n",argv[0]);
exit(1);
}
if( fork() == 0)
{
myexec(argv[1],argv[2]);
}
else
{
exit(0);
}
printf("this should never be executed");
}
i m not trying anything fancy...only to emulate the "no return" feature once a command is executed.
so when i run this program
(trial 1)
[c_d@localhost cfiles]$ gcc 1.c
[c_d@localhost cfiles]$ ./a.out /bin/ls ls
1.c a.out fcopy.c~ frevread.c~ msg~ prgm1.c~ prgm2.c~ prgm3.c~ prgm4.c~ prgm5.c~ prgm7.c sth~
1.c~ cfiles.tar.7z file1~ key.c prgm1 prgm2 prgm3 prgm4 prgm5 prgm6.c prgm7.c~
alpha.c~ f1~ fread.c~ key.c~ prgm1.c prgm2.c prgm3.c prgm4.c prgm5.c prgm6.c~ printip.c~
[c_d@localhost cfiles]$
(trial 2)
[c_d@localhost cfiles]$ ./a.out /home/c_d/workspace/unix/shellscripts/ ./leapyear_prgm07.sh
2009 is not a leap year
[c_d@localhost cfiles]$
i get what i want...the line "this should never be executed" never gets printed
please help me grasp the internals of exec() call... and explain to me how it works
also please explain to me, why in execl*() a set of arguments is preferred to be passed as in
execl ("/bin/ls", "ls", "-l", (char *)0);
when it could have been implemented as
execl ("/bin/ls", "ls -l", (char *)0);
instead
i have same complaint with execv*() style exec()...
thank you.