/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* zy+ 01/14/00
   This program is designed for testing the fattach(), fdetach() and isastream()
   generated from stropts.h
   Note: pipe() in HPUX is not STREAM, therefore, fattach() in this program
   will fail in HPUX */

#include  <sys/types.h>
#include  <sys/stat.h>
#include  <stropts.h>

int main()
{
  int fd[2], tempfd, errno;
  char *tmpname = tmpnam(NULL);

  /* create a temporary file and close it to get a mount point for fattach() */
  
  if ( (tempfd = creat(tmpname, S_IWRITE|S_IREAD)) < 0)
  {
    printf("creat() fails, errno = %d\n", tempfd);
    return(-1);
  }
  else printf("creat() is successful, errno = %d\n", tempfd);

  if ((errno = close(tempfd)) < 0)
  {
    printf("close() fails, errno = %d\n", errno);
    return(-1);
  }
  else printf("close() is successful, errno = %d\n", errno);
 
  
  /* open a pipe to be attached to the created temporary file */
 
  if ((errno = pipe(fd)) < 0)
  {
    printf("pipe() fails, errno = %d\n", errno);
    return(-1);
  }
  else printf("pipe() is successful, errno = %d\n", errno);

  /* only a stream pipe can be attached */
 
  if ((errno = isastream(fd[1])) == 1)
    printf("isastream() is successful and the pipe is a stream pipe, errno = %d\n", errno);
  else if ((errno = isastream(fd[1])) == 0)
    printf("isastream() is successful and the pipe is not a stream pipe, errno = %d\n", errno);
  else 
  {
    printf("isastream() fails, errno = %d\n", errno);
    return(-1);
  }

  /* attach the temporary file with a stream pipe.
     every operation acted on this file will be tranfered to the pipe
  */

  if ((errno = fattach(fd[1], tmpname)) < 0)
  {
    printf("fattach() fails, errno = %d\n", errno);
    return(-1);
  }
  else printf("fattach() is successful, errno = %d\n", errno);

  /* close the attachment */
  
  if ((errno = fdetach(tmpname)) < 0)
  {
    printf("fdetach() fails, errno = %d\n", errno);
    return(-1);
  }
  else printf("fdetach() is successful, errno = %d\n", errno);

  return(0); 
}
