/* Copyright (c) 2001-2004 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(void)
{
  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;

  /* isastream() tests whether STDIN or STDOUT used further below is a stream */
  if ((errno = isastream(STDIN_FILENO)) == 1)
    printf("isastream() is successful and the STDIN is a stream, errno = %d\n", errno);
   else if ((errno = isastream(STDIN_FILENO)) == 0)
    printf("isastream() is successful and the STDIN is not a stream, errno = %d\n", errno);

  if ((errno = isastream(STDOUT_FILENO)) == 1)
    printf("isastream() is successful and the STDOUT is a stream, errno = %d\n", errno);
   else if ((errno = isastream(STDOUT_FILENO)) == 0)
    printf("isastream() is successful and the STDOUT is not a stream, errno = %d\n", errno);

  band = 1;
  for ( ; ; )
  {
    flag = MSG_ANY;            /* return any message */
    if ( (errno = getpmsg(STDIN_FILENO, &ctl, &dat, &band, &flag)) < 0) {
      perror("getpmsg() fails");
      exit(1);
    }
    fprintf(stderr, "getpmsg is successful -> flag = %d, ctl.len = %d, dat.len = %d\n",
            flag, ctl.len, dat.len);

    flag = MSG_BAND;            /* put the message with a priority specified by band */
    if (dat.len == 1)
      exit(0);
     else if (dat.len > 1)
      if ( (errno = putpmsg(STDOUT_FILENO, &ctl, &dat, band, flag)) < 0) {
        perror("putpmsg() fails");
        exit(1);
      }
  }
}
