Compile source C in a process
I want to compile a source C in a parent process and then , the executable created , I want to run it into a son process. Any ideas?
Thank you.
#include <stdio.h>
#include <stdlib.h>
#define execle("/bin/ls","bin/ls","-l",NULL);
int main(int argc , char **argv )
{
int i;
for(i=1 ; i<argc ; i++)
{
if(argv[1]==".c")
{
if(fork()==0)
开发者_Go百科{
execle("/usr/bin/gcc","/usr/bin/gcc",argv[1],NULL);
exit(1);
}
}
if(argv[2]==".out")
{
if(fork()==0)
{
execle("/bin/cat","bin/cat/",argv[2],NULL);
exit(1);
}
}
}
return 0;
}
Here is how the code looks like , but can you tell me why it gives me errors on the lines where execle(..) is ?
In your parent/driver program, use the system(3)
call or a combination of fork/exec/wait
to run your compiler. Then you can use the same calls to run the newly compiled executable.
Keep in mind the security aspects of this while implementing it. This kind of thing has a huge exploit potential.
#define execle("/bin/ls","bin/ls","-l",NULL);
What do you expect this line to do ?
These two lines
if(argv[1]==".c")
if(argv[2]==".out")
Don't do what you think
try
if( strcmp(".c" , argv[1]) == 0 )
if( strcmp(".out" , argv[2]) == 0 )
More : http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Also,
exec("/bin/cat","bin/cat/",argv[2],NULL);
Does not really exist, so change it to execle
and #include <stdint.h>
More : http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
You should end up with something like :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc , char **argv)
{
int i;
for(i=1 ; i<argc ; i++)
{
if(strcmp(".c" , argv[1]) == 0)
{
if(fork()==0)
{
execle("/usr/bin/gcc","/usr/bin/gcc",argv[1],NULL);
exit(1);
}
}
if(strcmp(".c" , argv[1]) == 0)
{
if(fork()==0)
{
execle("/bin/cat","bin/cat/",argv[2],NULL);
exit(1);
}
}
}
return 0;
}
精彩评论