Debian Codegraph @

lib/stdio-write.c

raw
1 /* POSIX compatible FILE stream write function.
2 Copyright (C) 2008-2026 Free Software Foundation, Inc.
3 Written by Bruno Haible <[email protected]>, 2008.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include <stdio.h>
22
23 /* Replace these functions 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. This write() function is at the basis of the function
31 which flushes the buffer of a FILE stream. */
32
33 # if defined _WIN32 && ! defined __CYGWIN__
34
35 # include <errno.h>
36 # include <signal.h>
37 # include <io.h>
38
39 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
40 # include <windows.h>
41
42 # if GNULIB_MSVC_NOTHROW
43 # include "msvc-nothrow.h"
44 # else
45 # include <io.h>
46 # endif
47
48 /* Don't assume that UNICODE is not defined. */
49 # undef GetNamedPipeHandleState
50 # define GetNamedPipeHandleState GetNamedPipeHandleStateA
51
52 # if GNULIB_NONBLOCKING
53 # define CLEAR_ERRNO \
54 errno = 0;
55 # define HANDLE_ENOSPC \
56 if (errno == ENOSPC && ferror (stream)) \
57 { \
58 int fd = fileno (stream); \
59 if (fd >= 0) \
60 { \
61 HANDLE h = (HANDLE) _get_osfhandle (fd); \
62 if (GetFileType (h) == FILE_TYPE_PIPE) \
63 { \
64 /* h is a pipe or socket. */ \
65 DWORD state; \
66 if (GetNamedPipeHandleState (h, &state, NULL, NULL, \
67 NULL, NULL, 0) \
68 && (state & PIPE_NOWAIT) != 0) \
69 /* h is a pipe in non-blocking mode. \
70 Change errno from ENOSPC to EAGAIN. */ \
71 errno = EAGAIN; \
72 } \
73 } \
74 } \
75 else
76 # else
77 # define CLEAR_ERRNO
78 # define HANDLE_ENOSPC
79 # endif
80
81 # if GNULIB_SIGPIPE
82 # define CLEAR_LastError \
83 SetLastError (0);
84 # define HANDLE_ERROR_NO_DATA \
85 if (GetLastError () == ERROR_NO_DATA && ferror (stream)) \
86 { \
87 int fd = fileno (stream); \
88 if (fd >= 0 \
89 && GetFileType ((HANDLE) _get_osfhandle (fd)) \
90 == FILE_TYPE_PIPE) \
91 { \
92 /* Try to raise signal SIGPIPE. */ \
93 raise (SIGPIPE); \
94 /* If it is currently blocked or ignored, change errno from \
95 EINVAL to EPIPE. */ \
96 errno = EPIPE; \
97 } \
98 } \
99 else
100 # else
101 # define CLEAR_LastError
102 # define HANDLE_ERROR_NO_DATA
103 # endif
104
105 # define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \
106 if (ferror (stream)) \
107 return (EXPRESSION); \
108 else \
109 { \
110 CLEAR_ERRNO \
111 CLEAR_LastError \
112 RETTYPE ret = (EXPRESSION); \
113 if (FAILED) \
114 { \
115 HANDLE_ENOSPC \
116 HANDLE_ERROR_NO_DATA \
117 ; \
118 } \
119 return ret; \
120 }
121
122 # if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */
123 int
124 printf (const char *format, ...)
125 {
126 va_list args;
127 va_start (args, format);
128 int retval = vfprintf (stdout, format, args);
129 va_end (args);
130
131 return retval;
132 }
133 # endif
134
135 # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
136 int
137 fprintf (FILE *stream, const char *format, ...)
138 {
139 va_list args;
140 va_start (args, format);
141 int retval = vfprintf (stream, format, args);
142 va_end (args);
143
144 return retval;
145 }
146 # endif
147
148 # if !REPLACE_VPRINTF_POSIX /* avoid collision with vprintf.c */
149 int
150 vprintf (const char *format, va_list args)
151 {
152 return vfprintf (stdout, format, args);
153 }
154 # endif
155
156 # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vfprintf.c */
157 int
158 vfprintf (FILE *stream, const char *format, va_list args)
159 #undef vfprintf
160 #if defined __MINGW32__ && !defined _UCRT && __USE_MINGW_ANSI_STDIO
161 # define vfprintf gl_consolesafe_vfprintf
162 #endif
163 {
164 CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF)
165 }
166 # endif
167
168 int
169 putchar (int c)
170 {
171 return fputc (c, stdout);
172 }
173
174 int
175 fputc (int c, FILE *stream)
176 #undef fputc
177 {
178 CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF)
179 }
180
181 int
182 fputs (const char *string, FILE *stream)
183 #undef fputs
184 {
185 CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF)
186 }
187
188 int
189 puts (const char *string)
190 #undef puts
191 {
192 FILE *stream = stdout;
193 CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF)
194 }
195
196 size_t
197 fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
198 #undef fwrite
199 #if (defined _WIN32 && !defined __CYGWIN__) && !defined _UCRT
200 # define fwrite gl_consolesafe_fwrite
201 #endif
202 {
203 CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n)
204 }
205
206 # endif
207 #endif