Good I fixed that problem. A new one has risen. The following code outputs to the file, however any subsequent commands dont output to the stdout. For instance if after running in my shell, ls -l > foobar.txt, I run ls, the output for ls never appears on the stdout. Any ideas?
Code:
int orig_stdout;
//char *filename;
// Duplicate the stdout file handle and store it in orig_stdout.
orig_stdout = dup(fileno(stdout));
// This text appears on-screen.
// Reopen stdout and redirect it to the file.
freopen(args[3], "w", stdout);
// This text appears in the file.
pid = fork();
if (pid)
{
pid = wait(&ret_status);
}else
{
//format execlp input
strncat(args[1], "\0", 1);
if(execlp(args[0], args[1], NULL))
{
puts(strerror(errno));
exit(127);
}
}
// Close the redirected stdout.
fclose(stdout);
// Restore the original stdout and print to the screen again.
fdopen(orig_stdout, "w");
printf("I'm back writing to the original stdout.\n");