#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>

main(int argc, char *argv[])
{
	int i;
	int pid;
	int outfd;
	int status;

	outfd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
	printf("Starting ls\n");
	if ((pid = fork()) == 0)
	{
		dup2(outfd, 1);
		close(outfd);

		if (execlp("ls", "ls", "-l", "ex10.c", 0) == -1)
		{
			fprintf(stderr,"exec failed\n");
			exit(4);
		}
	}
	wait(&status);
	printf("Finished process status = %d\n",WEXITSTATUS(status));

}
