/***************************************************************************
 *   Copyright (C) 2005 by Brian Lauber   *
 *   bml8@case.edu   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "blError.h"

// This first message should always print
const char *const HD = "Half duplex\n";

// If the pipe is full-duplex, print this message too
const char *const FD = "Full duplex\n";

int main(int argc, char **argv)
{
  char buffer[64]; // Read into this buffer.
  int thePipe[2];

  Error(pipe(thePipe)); // Create an unnamed pipe

  if(Error(fork()) == 0) // Fork a child process
  {
    // First, the child process will access the pipe in the half-duplex format.
    // This means that it will WRITE to thePipe[1]
    Error(write(thePipe[1], HD, strlen(HD) + 1));

    // If the pipe is full-duplex, then the child should also be able to
    // read from thePipe[1]
    Error(read(thePipe[0], buffer, strlen(FD) + 1));
    printf("%s", buffer);
  }
  else // The parent process
  {
    Error(write(thePipe[1], FD, strlen(FD) + 1));
    Error(read(thePipe[0], buffer, strlen(HD) + 1));
    printf("%s", buffer);
  }

  return EXIT_SUCCESS;
}
