#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h> // open
#include <stdio.h>
int main() {
close(1); // close standard out
open("log.txt", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
puts("Captain's log");
chdir("/usr/include");
execl("/bin/ls", "ls", ".", (char *)NULL);
perror("exec failed");
return 0;
}
When I check log.txt, I could not find "Captain's log". I supposed it runs before execl, thus it should be there!