/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/* This program is designed for testing the getmsg(), getpmsg, putmsg(),
   putpmsg(), as well as isastream() generated from stropts.h
   Note: STDIN_FILENO and STDOUT_FILENO in HPUX is not STREAM, therefore,
   functions in this program will fail in HPUX
*/

#include <stropts.h>
#include <unistd.h>
#include <stdio.h>

#define	BUFFSIZE	8192

int main()
{
  int              errno,  band;
  char             ctlbuf[BUFFSIZE],  datbuf[BUFFSIZE];
  struct strbuf	   ctl,  dat;
  long             flag;

  ctl.buf = ctlbuf;
  ctl.maxlen = BUFFSIZE;
  dat.buf = datbuf;
  dat.maxlen = BUFFSIZE;

  for ( ; ; )
  {
    flag = 0;		/* return any message, put as a normal one */
    /* get all messages from STDIN and then put them onto STDOUT as a normal one.
       just input a return will stop this procedure
    */
    if ( (errno = getmsg(STDIN_FILENO, &ctl, &dat, &flag)) < 0) {
      perror("getmsg() fails");
      exit(1);
    }
    fprintf(stderr, "getmsg is successful -> flag = %d, ctl.len = %d, dat.len = %d\n",
            flag, ctl.len, dat.len);

    if (dat.len == 1)
      break;
     else if (dat.len > 1)
       if ( (errno = putmsg(STDOUT_FILENO, &ctl, &dat, flag)) < 0) {
         perror("putmsg() fails");
         exit(1);
       }
  }
}
