I am creating a Quine in C and where i need to create a new c file and then compile it and execute it.
I made a simple snippet to understand why it's not working.
My guess is that execv
start the command before fprintf is done writing but i put a sleep and it's wasn't working too.
(All my apologize for this ugliest code but it's not the goal)
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char *cmd[100]= {"sh", "-c", "\"gcc tmp.c && ./a.out\""};
fprintf(fopen("tmp.c", "w"), "#include <stdio.h>\nint main(){puts(\"Hello World\");}");
execv("/bin/sh", cmd);
return (0);
}
Output
sh: gcc tmp.c && ./a.out: No such file or directory
Any idea ?