root/maint/gnulib/tests/test-pipe.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. is_open
  2. is_cloexec
  3. is_nonblocking
  4. main

   1 /* Test of pipe.
   2    Copyright (C) 2009-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software; you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3, or (at your option)
   7    any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 #include <config.h>
  18 
  19 #include <unistd.h>
  20 
  21 #include "signature.h"
  22 SIGNATURE_CHECK (pipe, int, (int[2]));
  23 
  24 #include <fcntl.h>
  25 #include <stdbool.h>
  26 
  27 #if defined _WIN32 && ! defined __CYGWIN__
  28 /* Get declarations of the native Windows API functions.  */
  29 # define WIN32_LEAN_AND_MEAN
  30 # include <windows.h>
  31 /* Get _get_osfhandle.  */
  32 # if GNULIB_MSVC_NOTHROW
  33 #  include "msvc-nothrow.h"
  34 # else
  35 #  include <io.h>
  36 # endif
  37 #endif
  38 
  39 #include "binary-io.h"
  40 #include "macros.h"
  41 
  42 /* Return true if FD is open.  */
  43 static bool
  44 is_open (int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46 #if defined _WIN32 && ! defined __CYGWIN__
  47   /* On native Windows, the initial state of unassigned standard file
  48      descriptors is that they are open but point to an
  49      INVALID_HANDLE_VALUE, and there is no fcntl.  */
  50   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
  51 #else
  52 # ifndef F_GETFL
  53 #  error Please port fcntl to your platform
  54 # endif
  55   return 0 <= fcntl (fd, F_GETFL);
  56 #endif
  57 }
  58 
  59 /* Return true if FD is not inherited to child processes.  */
  60 static bool
  61 is_cloexec (int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63 #if defined _WIN32 && ! defined __CYGWIN__
  64   HANDLE h = (HANDLE) _get_osfhandle (fd);
  65   DWORD flags;
  66   ASSERT (GetHandleInformation (h, &flags));
  67   return (flags & HANDLE_FLAG_INHERIT) == 0;
  68 #else
  69   int flags;
  70   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
  71   return (flags & FD_CLOEXEC) != 0;
  72 #endif
  73 }
  74 
  75 /* Return true if FD is in non-blocking mode.  */
  76 static bool
  77 is_nonblocking (int fd)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79 #if defined _WIN32 && ! defined __CYGWIN__
  80   /* We don't use the non-blocking mode for sockets here.  */
  81   return 0;
  82 #else
  83   int flags;
  84   ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
  85   return (flags & O_NONBLOCK) != 0;
  86 #endif
  87 }
  88 
  89 int
  90 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92   int fd[2];
  93 
  94   fd[0] = -1;
  95   fd[1] = -1;
  96   ASSERT (pipe (fd) >= 0);
  97   ASSERT (fd[0] >= 0);
  98   ASSERT (fd[1] >= 0);
  99   ASSERT (fd[0] != fd[1]);
 100   ASSERT (is_open (fd[0]));
 101   ASSERT (is_open (fd[1]));
 102   ASSERT (!is_cloexec (fd[0]));
 103   ASSERT (!is_cloexec (fd[1]));
 104   ASSERT (!is_nonblocking (fd[0]));
 105   ASSERT (!is_nonblocking (fd[1]));
 106 
 107   return 0;
 108 }

/* [previous][next][first][last][top][bottom][index][help] */