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

main(int argc, char *argv[])
{
	int pid;
	int status;
	int pipefd[2];

	if (pipe(pipefd) < 0)
	{
		perror("pipe failed");
		exit(3);
	}

	printf("Starting ls\n");
	if ((pid = fork()) == 0)
	{
		dup2(pipefd[1], 1);
		close(pipefd[0]);
		close(pipefd[1]);

		if (execlp("ls", "ls", "-l", "ex10.c", 0) == -1)
		{
			perror("exec failed");
			exit(4);
		}
	}

	if ((pid = fork()) == 0)
	{
		dup2(pipefd[0], 0);
		close(pipefd[0]);
		close(pipefd[1]);

		if (execlp("wc", "wc", 0) == -1)
		{
			perror("exec failed");
			exit(5);
		}
	}

	close(pipefd[0]);
	close(pipefd[1]);
	waitpid(pid, &status, 0);

	printf("Finished process status = %d\n",WEXITSTATUS(status));

}
