1/* POSIX compatible write() function.
2   Copyright (C) 2008-2014 Free Software Foundation, Inc.
3   Written by Bruno Haible <bruno@clisp.org>, 2008.
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18#include <config.h>
19
20/* Specification.  */
21#include <unistd.h>
22
23/* On native Windows platforms, SIGPIPE does not exist.  When write() is
24   called on a pipe with no readers, WriteFile() fails with error
25   GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
26   error EINVAL.  */
27
28#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29
30# include <errno.h>
31# include <signal.h>
32# include <io.h>
33
34# define WIN32_LEAN_AND_MEAN  /* avoid including junk */
35# include <windows.h>
36
37# include "msvc-inval.h"
38# include "msvc-nothrow.h"
39
40# undef write
41
42# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
43static ssize_t
44write_nothrow (int fd, const void *buf, size_t count)
45{
46  ssize_t result;
47
48  TRY_MSVC_INVAL
49    {
50      result = write (fd, buf, count);
51    }
52  CATCH_MSVC_INVAL
53    {
54      result = -1;
55      errno = EBADF;
56    }
57  DONE_MSVC_INVAL;
58
59  return result;
60}
61# else
62#  define write_nothrow write
63# endif
64
65ssize_t
66rpl_write (int fd, const void *buf, size_t count)
67{
68  for (;;)
69    {
70      ssize_t ret = write_nothrow (fd, buf, count);
71
72      if (ret < 0)
73        {
74# if GNULIB_NONBLOCKING
75          if (errno == ENOSPC)
76            {
77              HANDLE h = (HANDLE) _get_osfhandle (fd);
78              if (GetFileType (h) == FILE_TYPE_PIPE)
79                {
80                  /* h is a pipe or socket.  */
81                  DWORD state;
82                  if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
83                                               NULL, 0)
84                      && (state & PIPE_NOWAIT) != 0)
85                    {
86                      /* h is a pipe in non-blocking mode.
87                         We can get here in four situations:
88                           1. When the pipe buffer is full.
89                           2. When count <= pipe_buf_size and the number of
90                              free bytes in the pipe buffer is < count.
91                           3. When count > pipe_buf_size and the number of free
92                              bytes in the pipe buffer is > 0, < pipe_buf_size.
93                           4. When count > pipe_buf_size and the pipe buffer is
94                              entirely empty.
95                         The cases 1 and 2 are POSIX compliant.  In cases 3 and
96                         4 POSIX specifies that write() must split the request
97                         and succeed with a partial write.  We fix case 4.
98                         We don't fix case 3 because it is not essential for
99                         programs.  */
100                      DWORD out_size; /* size of the buffer for outgoing data */
101                      DWORD in_size;  /* size of the buffer for incoming data */
102                      if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
103                        {
104                          size_t reduced_count = count;
105                          /* In theory we need only one of out_size, in_size.
106                             But I don't know which of the two.  The description
107                             is ambiguous.  */
108                          if (out_size != 0 && out_size < reduced_count)
109                            reduced_count = out_size;
110                          if (in_size != 0 && in_size < reduced_count)
111                            reduced_count = in_size;
112                          if (reduced_count < count)
113                            {
114                              /* Attempt to write only the first part.  */
115                              count = reduced_count;
116                              continue;
117                            }
118                        }
119                      /* Change errno from ENOSPC to EAGAIN.  */
120                      errno = EAGAIN;
121                    }
122                }
123            }
124          else
125# endif
126            {
127# if GNULIB_SIGPIPE
128              if (GetLastError () == ERROR_NO_DATA
129                  && GetFileType ((HANDLE) _get_osfhandle (fd))
130                     == FILE_TYPE_PIPE)
131                {
132                  /* Try to raise signal SIGPIPE.  */
133                  raise (SIGPIPE);
134                  /* If it is currently blocked or ignored, change errno from
135                     EINVAL to EPIPE.  */
136                  errno = EPIPE;
137                }
138# endif
139            }
140        }
141      return ret;
142    }
143}
144
145#endif
146