Import upstream version 1.28
[debian/tar] / gnu / write.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* POSIX compatible write() function.
4    Copyright (C) 2008-2014 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2008.
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 /* On native Windows platforms, SIGPIPE does not exist.  When write() is
26    called on a pipe with no readers, WriteFile() fails with error
27    GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
28    error EINVAL.  */
29
30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31
32 # include <errno.h>
33 # include <signal.h>
34 # include <io.h>
35
36 # define WIN32_LEAN_AND_MEAN  /* avoid including junk */
37 # include <windows.h>
38
39 # include "msvc-inval.h"
40 # include "msvc-nothrow.h"
41
42 # undef write
43
44 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
45 static ssize_t
46 write_nothrow (int fd, const void *buf, size_t count)
47 {
48   ssize_t result;
49
50   TRY_MSVC_INVAL
51     {
52       result = write (fd, buf, count);
53     }
54   CATCH_MSVC_INVAL
55     {
56       result = -1;
57       errno = EBADF;
58     }
59   DONE_MSVC_INVAL;
60
61   return result;
62 }
63 # else
64 #  define write_nothrow write
65 # endif
66
67 ssize_t
68 rpl_write (int fd, const void *buf, size_t count)
69 {
70   for (;;)
71     {
72       ssize_t ret = write_nothrow (fd, buf, count);
73
74       if (ret < 0)
75         {
76 # if GNULIB_NONBLOCKING
77           if (errno == ENOSPC)
78             {
79               HANDLE h = (HANDLE) _get_osfhandle (fd);
80               if (GetFileType (h) == FILE_TYPE_PIPE)
81                 {
82                   /* h is a pipe or socket.  */
83                   DWORD state;
84                   if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
85                                                NULL, 0)
86                       && (state & PIPE_NOWAIT) != 0)
87                     {
88                       /* h is a pipe in non-blocking mode.
89                          We can get here in four situations:
90                            1. When the pipe buffer is full.
91                            2. When count <= pipe_buf_size and the number of
92                               free bytes in the pipe buffer is < count.
93                            3. When count > pipe_buf_size and the number of free
94                               bytes in the pipe buffer is > 0, < pipe_buf_size.
95                            4. When count > pipe_buf_size and the pipe buffer is
96                               entirely empty.
97                          The cases 1 and 2 are POSIX compliant.  In cases 3 and
98                          4 POSIX specifies that write() must split the request
99                          and succeed with a partial write.  We fix case 4.
100                          We don't fix case 3 because it is not essential for
101                          programs.  */
102                       DWORD out_size; /* size of the buffer for outgoing data */
103                       DWORD in_size;  /* size of the buffer for incoming data */
104                       if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
105                         {
106                           size_t reduced_count = count;
107                           /* In theory we need only one of out_size, in_size.
108                              But I don't know which of the two.  The description
109                              is ambiguous.  */
110                           if (out_size != 0 && out_size < reduced_count)
111                             reduced_count = out_size;
112                           if (in_size != 0 && in_size < reduced_count)
113                             reduced_count = in_size;
114                           if (reduced_count < count)
115                             {
116                               /* Attempt to write only the first part.  */
117                               count = reduced_count;
118                               continue;
119                             }
120                         }
121                       /* Change errno from ENOSPC to EAGAIN.  */
122                       errno = EAGAIN;
123                     }
124                 }
125             }
126           else
127 # endif
128             {
129 # if GNULIB_SIGPIPE
130               if (GetLastError () == ERROR_NO_DATA
131                   && GetFileType ((HANDLE) _get_osfhandle (fd))
132                      == FILE_TYPE_PIPE)
133                 {
134                   /* Try to raise signal SIGPIPE.  */
135                   raise (SIGPIPE);
136                   /* If it is currently blocked or ignored, change errno from
137                      EINVAL to EPIPE.  */
138                   errno = EPIPE;
139                 }
140 # endif
141             }
142         }
143       return ret;
144     }
145 }
146
147 #endif