Blame gnulib-tests/test-pipe.c

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