Blame gnulib-tests/test-pipe.c

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