/*BINFMTC:
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifndef O_NONBLOCK
#define O_NONBLOCK O_NDELAY
#endif
#ifndef S_IFIFO
#define S_IFIFO 0010000
#endif

char *fin = "/tmp/conftest$$";

main()
{
  struct stat stb;
  int wrong_fd;
#ifdef FD_SET
  fd_set f;
#else
  int f;
#endif

  //(void)alarm(5);
#ifdef POSIX
  if (mkfifo(fin, 0777))
#else
  if (mknod(fin, S_IFIFO|0777, 0))
#endif
    {
      perror("mknod");
      exit(1);
    }
  if (stat(fin, &stb) || (stb.st_mode & S_IFIFO) != S_IFIFO)
    exit(2);
  close(0);
#ifdef __386BSD__
  /*
   * The next test fails under 386BSD, but screen works using fifos.
   * Fifos in O_RDWR mode are only used for the BROKEN_PIPE case and for
   * the select() configuration test.
   */
  exit(0);
#endif
  if (wrong_fd=open(fin, O_RDONLY | O_NONBLOCK))
    {
      printf("wrong fd open: %i\n", wrong_fd);
      exit(3);
    }
  if (fork() == 0)
    {
      close(0);
      if (open(fin, O_WRONLY | O_NONBLOCK))
        exit(3);
      close(0);
      if (open(fin, O_WRONLY | O_NONBLOCK))
        exit(4);
      if (write(0, "TEST", 4) == -1)
        exit(5);
      exit(0);
    }
#ifdef FD_SET
  FD_SET(0, &f);
#else
  f = 1;
#endif
  if (select(1, &f, 0, 0, 0) == -1)
    exit(6);
  exit(0);
}
