13e4d44aa681d1b3e56cb355e3799d780ab74bb4
[debian/tar] / gnu / read.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* POSIX compatible read() function.
4    Copyright (C) 2008-2013 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2011.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <unistd.h>
24
25 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
26
27 # include <errno.h>
28 # include <io.h>
29
30 # define WIN32_LEAN_AND_MEAN  /* avoid including junk */
31 # include <windows.h>
32
33 # include "msvc-inval.h"
34 # include "msvc-nothrow.h"
35
36 # undef read
37
38 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
39 static ssize_t
40 read_nothrow (int fd, void *buf, size_t count)
41 {
42   ssize_t result;
43
44   TRY_MSVC_INVAL
45     {
46       result = read (fd, buf, count);
47     }
48   CATCH_MSVC_INVAL
49     {
50       result = -1;
51       errno = EBADF;
52     }
53   DONE_MSVC_INVAL;
54
55   return result;
56 }
57 # else
58 #  define read_nothrow read
59 # endif
60
61 ssize_t
62 rpl_read (int fd, void *buf, size_t count)
63 {
64   ssize_t ret = read_nothrow (fd, buf, count);
65
66 # if GNULIB_NONBLOCKING
67   if (ret < 0
68       && GetLastError () == ERROR_NO_DATA)
69     {
70       HANDLE h = (HANDLE) _get_osfhandle (fd);
71       if (GetFileType (h) == FILE_TYPE_PIPE)
72         {
73           /* h is a pipe or socket.  */
74           DWORD state;
75           if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL, NULL, 0)
76               && (state & PIPE_NOWAIT) != 0)
77             /* h is a pipe in non-blocking mode.
78                Change errno from EINVAL to EAGAIN.  */
79             errno = EAGAIN;
80         }
81     }
82 # endif
83
84   return ret;
85 }
86
87 #endif