Blame lib/read.c

Packit 709fb3
/* POSIX compatible read() function.
Packit 709fb3
   Copyright (C) 2008-2017 Free Software Foundation, Inc.
Packit 709fb3
   Written by Bruno Haible <bruno@clisp.org>, 2011.
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 of the License, or
Packit 709fb3
   (at your option) 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
/* Specification.  */
Packit 709fb3
#include <unistd.h>
Packit 709fb3
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
Packit 709fb3
# include <errno.h>
Packit 709fb3
# include <io.h>
Packit 709fb3
Packit 709fb3
# define WIN32_LEAN_AND_MEAN  /* avoid including junk */
Packit 709fb3
# include <windows.h>
Packit 709fb3
Packit 709fb3
# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 709fb3
#  include "msvc-inval.h"
Packit 709fb3
# endif
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
Packit 709fb3
# undef read
Packit 709fb3
Packit 709fb3
# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 709fb3
static ssize_t
Packit 709fb3
read_nothrow (int fd, void *buf, size_t count)
Packit 709fb3
{
Packit 709fb3
  ssize_t result;
Packit 709fb3
Packit 709fb3
  TRY_MSVC_INVAL
Packit 709fb3
    {
Packit 709fb3
      result = read (fd, buf, count);
Packit 709fb3
    }
Packit 709fb3
  CATCH_MSVC_INVAL
Packit 709fb3
    {
Packit 709fb3
      result = -1;
Packit 709fb3
      errno = EBADF;
Packit 709fb3
    }
Packit 709fb3
  DONE_MSVC_INVAL;
Packit 709fb3
Packit 709fb3
  return result;
Packit 709fb3
}
Packit 709fb3
# else
Packit 709fb3
#  define read_nothrow read
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
ssize_t
Packit 709fb3
rpl_read (int fd, void *buf, size_t count)
Packit 709fb3
{
Packit 709fb3
  ssize_t ret = read_nothrow (fd, buf, count);
Packit 709fb3
Packit 709fb3
# if GNULIB_NONBLOCKING
Packit 709fb3
  if (ret < 0
Packit 709fb3
      && GetLastError () == ERROR_NO_DATA)
Packit 709fb3
    {
Packit 709fb3
      HANDLE h = (HANDLE) _get_osfhandle (fd);
Packit 709fb3
      if (GetFileType (h) == FILE_TYPE_PIPE)
Packit 709fb3
        {
Packit 709fb3
          /* h is a pipe or socket.  */
Packit 709fb3
          DWORD state;
Packit 709fb3
          if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL, NULL, 0)
Packit 709fb3
              && (state & PIPE_NOWAIT) != 0)
Packit 709fb3
            /* h is a pipe in non-blocking mode.
Packit 709fb3
               Change errno from EINVAL to EAGAIN.  */
Packit 709fb3
            errno = EAGAIN;
Packit 709fb3
        }
Packit 709fb3
    }
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
  return ret;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#endif