64bc5352ebe02ebda03dbe678004975b510bff68
[gnulib.git] / lib / write.c
1 /* POSIX compatible write() function.
2    Copyright (C) 2008-2011 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 /* Replace this function only if module 'nonblocking' or module 'sigpipe' is
24    requested.  */
25 #if GNULIB_NONBLOCKING || GNULIB_SIGPIPE
26
27 /* On native Windows platforms, SIGPIPE does not exist.  When write() is
28    called on a pipe with no readers, WriteFile() fails with error
29    GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
30    error EINVAL.  */
31
32 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33
34 #  include <errno.h>
35 #  include <signal.h>
36 #  include <io.h>
37
38 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
39 #  include <windows.h>
40
41 #  include "msvc-nothrow.h"
42
43 ssize_t
44 rpl_write (int fd, const void *buf, size_t count)
45 #undef write
46 {
47   for (;;)
48     {
49       ssize_t ret = write (fd, buf, count);
50
51       if (ret < 0)
52         {
53 #  if GNULIB_NONBLOCKING
54           if (errno == ENOSPC)
55             {
56               HANDLE h = (HANDLE) _get_osfhandle (fd);
57               if (GetFileType (h) == FILE_TYPE_PIPE)
58                 {
59                   /* h is a pipe or socket.  */
60                   DWORD state;
61                   if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
62                                                NULL, 0)
63                       && (state & PIPE_NOWAIT) != 0)
64                     {
65                       /* h is a pipe in non-blocking mode.
66                          We can get here in four situations:
67                            1. When the pipe buffer is full.
68                            2. When count <= pipe_buf_size and the number of
69                               free bytes in the pipe buffer is < count.
70                            3. When count > pipe_buf_size and the number of free
71                               bytes in the pipe buffer is > 0, < pipe_buf_size.
72                            4. When count > pipe_buf_size and the pipe buffer is
73                               entirely empty.
74                          The cases 1 and 2 are POSIX compliant.  In cases 3 and
75                          4 POSIX specifies that write() must split the request
76                          and succeed with a partial write.  We fix case 4.
77                          We don't fix case 3 because it is not essential for
78                          programs.  */
79                       DWORD out_size; /* size of the buffer for outgoing data */
80                       DWORD in_size;  /* size of the buffer for incoming data */
81                       if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
82                         {
83                           size_t reduced_count = count;
84                           /* In theory we need only one of out_size, in_size.
85                              But I don't know which of the two.  The description
86                              is ambiguous.  */
87                           if (out_size != 0 && out_size < reduced_count)
88                             reduced_count = out_size;
89                           if (in_size != 0 && in_size < reduced_count)
90                             reduced_count = in_size;
91                           if (reduced_count < count)
92                             {
93                               /* Attempt to write only the first part.  */
94                               count = reduced_count;
95                               continue;
96                             }
97                         }
98                       /* Change errno from ENOSPC to EAGAIN.  */
99                       errno = EAGAIN;
100                     }
101                 }
102             }
103           else
104 #  endif
105             {
106 #  if GNULIB_SIGPIPE
107               if (GetLastError () == ERROR_NO_DATA
108                   && GetFileType ((HANDLE) _get_osfhandle (fd))
109                      == FILE_TYPE_PIPE)
110                 {
111                   /* Try to raise signal SIGPIPE.  */
112                   raise (SIGPIPE);
113                   /* If it is currently blocked or ignored, change errno from
114                      EINVAL to EPIPE.  */
115                   errno = EPIPE;
116                 }
117 #  endif
118             }
119         }
120       return ret;
121     }
122 }
123
124 # endif
125 #endif