1 /* POSIX compatible FILE stream write function.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
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.
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.
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/>. */
23 /* Replace these functions only if module 'nonblocking' or module 'sigpipe' is
25 #if GNULIB_NONBLOCKING || GNULIB_SIGPIPE
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. */
33 # if defined _WIN32 && ! defined __CYGWIN__
39 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
42 # if GNULIB_MSVC_NOTHROW
43 # include "msvc-nothrow.h"
48 /* Don't assume that UNICODE is not defined. */
49 # undef GetNamedPipeHandleState
50 # define GetNamedPipeHandleState GetNamedPipeHandleStateA
52 # if GNULIB_NONBLOCKING
53 # define CLEAR_ERRNO \
55 # define HANDLE_ENOSPC \
56 if (errno == ENOSPC && ferror (stream)) \
58 int fd = fileno (stream); \
61 HANDLE h = (HANDLE) _get_osfhandle (fd); \
62 if (GetFileType (h) == FILE_TYPE_PIPE) \
64 /* h is a pipe or socket. */ \
66 if (GetNamedPipeHandleState (h, &state, NULL, NULL, \
68 && (state & PIPE_NOWAIT) != 0) \
69 /* h is a pipe in non-blocking mode. \
70 Change errno from ENOSPC to EAGAIN. */ \
78 # define HANDLE_ENOSPC
82 # define CLEAR_LastError \
84 # define HANDLE_ERROR_NO_DATA \
85 if (GetLastError () == ERROR_NO_DATA && ferror (stream)) \
87 int fd = fileno (stream); \
89 && GetFileType ((HANDLE) _get_osfhandle (fd)) \
92 /* Try to raise signal SIGPIPE. */ \
94 /* If it is currently blocked or ignored, change errno from \
101 # define CLEAR_LastError
102 # define HANDLE_ERROR_NO_DATA
105 # define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \
106 if (ferror (stream)) \
107 return (EXPRESSION); \
113 ret = (EXPRESSION); \
117 HANDLE_ERROR_NO_DATA \
123 # if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */
125 printf (const char *format
, ...)
130 va_start (args
, format
);
131 retval
= vfprintf (stdout
, format
, args
);
138 # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
140 fprintf (FILE *stream
, const char *format
, ...)
145 va_start (args
, format
);
146 retval
= vfprintf (stream
, format
, args
);
153 # if !REPLACE_VPRINTF_POSIX /* avoid collision with vprintf.c */
155 vprintf (const char *format
, va_list args
)
157 return vfprintf (stdout
, format
, args
);
161 # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vfprintf.c */
163 vfprintf (FILE *stream
, const char *format
, va_list args
)
166 CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream
, format
, args
), ret
== EOF
)
173 return fputc (c
, stdout
);
177 fputc (int c
, FILE *stream
)
180 CALL_WITH_SIGPIPE_EMULATION (int, fputc (c
, stream
), ret
== EOF
)
184 fputs (const char *string
, FILE *stream
)
187 CALL_WITH_SIGPIPE_EMULATION (int, fputs (string
, stream
), ret
== EOF
)
191 puts (const char *string
)
194 FILE *stream
= stdout
;
195 CALL_WITH_SIGPIPE_EMULATION (int, puts (string
), ret
== EOF
)
199 fwrite (const void *ptr
, size_t s
, size_t n
, FILE *stream
)
202 CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr
, s
, n
, stream
), ret
< n
)