#include <stdio.h>
#include <string.h>      // strlen
#include <stdlib.h>      // exit
#include <unistd.h>      // sleep, fork
#include <sys/types.h>   // fork, wait
#include <sys/wait.h>    // wait

int main(int argc, char *argv[]) 
{
  int fd[2];
  int n_read, m_written;
  char message[BUFSIZ];
  
  if (pipe(fd) == -1)   /* create a pipe */ 
    {
      perror("Pipe");   /* pipe creation failed */
      exit(1);
    }
  
  switch(fork())
    {     
    case -1:            /* process creation failed */
      perror("Fork");
      exit(2);
      
    case 0:             /* child process */
      
      close (fd[1]);
      
      if ((n_read = read(fd[0], message, sizeof(message))) > 0)
	{
	  message[n_read] = '\0';   	  //	  *(message+n_read) = '\0'; 
	  printf("Received %d bytes: %s\n", n_read, message);
	}
      else
	perror("Read");
      break;
      
    default:            /* parent process */
      
      close(fd[0]);
      
      if ((m_written = write(fd[1], argv[1], strlen(argv[1]))) > 0) 	
	printf("Sent %d bytes: %s\n", m_written, argv[1]);
      else 
	perror("Write");
    }
}
