Blame gnulib-tests/test-pipe.c

Packit 709fb3
/* Test of pipe.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software; you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3, or (at your option)
Packit 709fb3
   any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#include "signature.h"
Packit 709fb3
SIGNATURE_CHECK (pipe, int, (int[2]));
Packit 709fb3
Packit 709fb3
#include <fcntl.h>
Packit 709fb3
#include <stdbool.h>
Packit 709fb3
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
/* Get declarations of the native Windows API functions.  */
Packit 709fb3
# define WIN32_LEAN_AND_MEAN
Packit 709fb3
# include <windows.h>
Packit 709fb3
/* Get _get_osfhandle.  */
Packit 709fb3
# if GNULIB_MSVC_NOTHROW
Packit 709fb3
#  include "msvc-nothrow.h"
Packit 709fb3
# else
Packit 709fb3
#  include <io.h>
Packit 709fb3
# endif
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#include "binary-io.h"
Packit 709fb3
#include "macros.h"
Packit 709fb3
Packit 709fb3
/* Return true if FD is open.  */
Packit 709fb3
static bool
Packit 709fb3
is_open (int fd)
Packit 709fb3
{
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
  /* On native Windows, the initial state of unassigned standard file
Packit 709fb3
     descriptors is that they are open but point to an
Packit 709fb3
     INVALID_HANDLE_VALUE, and there is no fcntl.  */
Packit 709fb3
  return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
Packit 709fb3
#else
Packit 709fb3
# ifndef F_GETFL
Packit 709fb3
#  error Please port fcntl to your platform
Packit 709fb3
# endif
Packit 709fb3
  return 0 <= fcntl (fd, F_GETFL);
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Return true if FD is not inherited to child processes.  */
Packit 709fb3
static bool
Packit 709fb3
is_cloexec (int fd)
Packit 709fb3
{
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
  HANDLE h = (HANDLE) _get_osfhandle (fd);
Packit 709fb3
  DWORD flags;
Packit 709fb3
  ASSERT (GetHandleInformation (h, &flags));
Packit 709fb3
  return (flags & HANDLE_FLAG_INHERIT) == 0;
Packit 709fb3
#else
Packit 709fb3
  int flags;
Packit 709fb3
  ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
Packit 709fb3
  return (flags & FD_CLOEXEC) != 0;
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Return true if FD is in non-blocking mode.  */
Packit 709fb3
static bool
Packit 709fb3
is_nonblocking (int fd)
Packit 709fb3
{
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
  /* We don't use the non-blocking mode for sockets here.  */
Packit 709fb3
  return 0;
Packit 709fb3
#else
Packit 709fb3
  int flags;
Packit 709fb3
  ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
Packit 709fb3
  return (flags & O_NONBLOCK) != 0;
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
main ()
Packit 709fb3
{
Packit 709fb3
  int fd[2];
Packit 709fb3
Packit 709fb3
  fd[0] = -1;
Packit 709fb3
  fd[1] = -1;
Packit 709fb3
  ASSERT (pipe (fd) >= 0);
Packit 709fb3
  ASSERT (fd[0] >= 0);
Packit 709fb3
  ASSERT (fd[1] >= 0);
Packit 709fb3
  ASSERT (fd[0] != fd[1]);
Packit 709fb3
  ASSERT (is_open (fd[0]));
Packit 709fb3
  ASSERT (is_open (fd[1]));
Packit 709fb3
  ASSERT (!is_cloexec (fd[0]));
Packit 709fb3
  ASSERT (!is_cloexec (fd[1]));
Packit 709fb3
  ASSERT (!is_nonblocking (fd[0]));
Packit 709fb3
  ASSERT (!is_nonblocking (fd[1]));
Packit 709fb3
Packit 709fb3
  return 0;
Packit 709fb3
}