annotate lib/stdio-write.c @ 273:88bbffa404ea 2.5 2.5

Updated version for release
author lost
date Sun, 16 Aug 2009 17:18:11 +0000
parents d5392bb5da3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1 /* POSIX compatible FILE stream write function.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
2 Copyright (C) 2008 Free Software Foundation, Inc.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
4
d5392bb5da3c Added generated files
lost
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
d5392bb5da3c Added generated files
lost
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
d5392bb5da3c Added generated files
lost
parents:
diff changeset
8 (at your option) any later version.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
9
d5392bb5da3c Added generated files
lost
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
13 GNU General Public License for more details.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
14
d5392bb5da3c Added generated files
lost
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
d5392bb5da3c Added generated files
lost
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
17
d5392bb5da3c Added generated files
lost
parents:
diff changeset
18 #include <config.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
19
d5392bb5da3c Added generated files
lost
parents:
diff changeset
20 /* Specification. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
21 #include <stdio.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
22
d5392bb5da3c Added generated files
lost
parents:
diff changeset
23 /* Replace these functions only if module 'sigpipe' is requested. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
24 #if GNULIB_SIGPIPE
d5392bb5da3c Added generated files
lost
parents:
diff changeset
25
d5392bb5da3c Added generated files
lost
parents:
diff changeset
26 /* On native Windows platforms, SIGPIPE does not exist. When write() is
d5392bb5da3c Added generated files
lost
parents:
diff changeset
27 called on a pipe with no readers, WriteFile() fails with error
d5392bb5da3c Added generated files
lost
parents:
diff changeset
28 GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
d5392bb5da3c Added generated files
lost
parents:
diff changeset
29 error EINVAL. This write() function is at the basis of the function
d5392bb5da3c Added generated files
lost
parents:
diff changeset
30 which flushes the buffer of a FILE stream. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
31
d5392bb5da3c Added generated files
lost
parents:
diff changeset
32 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
d5392bb5da3c Added generated files
lost
parents:
diff changeset
33
d5392bb5da3c Added generated files
lost
parents:
diff changeset
34 # include <errno.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
35 # include <signal.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
36 # include <io.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
37
d5392bb5da3c Added generated files
lost
parents:
diff changeset
38 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
39 # include <windows.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
40
d5392bb5da3c Added generated files
lost
parents:
diff changeset
41 # define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
42 if (ferror (stream)) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
43 return (EXPRESSION); \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
44 else \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
45 { \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
46 RETTYPE ret; \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
47 SetLastError (0); \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
48 ret = (EXPRESSION); \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
49 if (FAILED && GetLastError () == ERROR_NO_DATA && ferror (stream)) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
50 { \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
51 int fd = fileno (stream); \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
52 if (fd >= 0 \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
53 && GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_PIPE)\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
54 { \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
55 /* Try to raise signal SIGPIPE. */ \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
56 raise (SIGPIPE); \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
57 /* If it is currently blocked or ignored, change errno from \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
58 EINVAL to EPIPE. */ \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
59 errno = EPIPE; \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
60 } \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
61 } \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
62 return ret; \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
63 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
64
d5392bb5da3c Added generated files
lost
parents:
diff changeset
65 # if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
66 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
67 printf (const char *format, ...)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
68 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
69 int retval;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
70 va_list args;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
71
d5392bb5da3c Added generated files
lost
parents:
diff changeset
72 va_start (args, format);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
73 retval = vfprintf (stdout, format, args);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
74 va_end (args);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
75
d5392bb5da3c Added generated files
lost
parents:
diff changeset
76 return retval;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
77 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
78 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
79
d5392bb5da3c Added generated files
lost
parents:
diff changeset
80 # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
81 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
82 fprintf (FILE *stream, const char *format, ...)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
83 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
84 int retval;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
85 va_list args;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
86
d5392bb5da3c Added generated files
lost
parents:
diff changeset
87 va_start (args, format);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
88 retval = vfprintf (stream, format, args);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
89 va_end (args);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
90
d5392bb5da3c Added generated files
lost
parents:
diff changeset
91 return retval;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
92 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
93 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
94
d5392bb5da3c Added generated files
lost
parents:
diff changeset
95 # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vprintf.c */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
96 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
97 vprintf (const char *format, va_list args)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
98 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
99 return vfprintf (stdout, format, args);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
100 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
101 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
102
d5392bb5da3c Added generated files
lost
parents:
diff changeset
103 # if !REPLACE_VPRINTF_POSIX /* avoid collision with vfprintf.c */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
104 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
105 vfprintf (FILE *stream, const char *format, va_list args)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
106 #undef vfprintf
d5392bb5da3c Added generated files
lost
parents:
diff changeset
107 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
108 CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
109 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
110 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
111
d5392bb5da3c Added generated files
lost
parents:
diff changeset
112 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
113 putchar (int c)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
114 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
115 return fputc (c, stdout);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
116 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
117
d5392bb5da3c Added generated files
lost
parents:
diff changeset
118 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
119 fputc (int c, FILE *stream)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
120 #undef fputc
d5392bb5da3c Added generated files
lost
parents:
diff changeset
121 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
122 CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
123 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
124
d5392bb5da3c Added generated files
lost
parents:
diff changeset
125 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
126 fputs (const char *string, FILE *stream)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
127 #undef fputs
d5392bb5da3c Added generated files
lost
parents:
diff changeset
128 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
129 CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
130 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
131
d5392bb5da3c Added generated files
lost
parents:
diff changeset
132 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
133 puts (const char *string)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
134 #undef puts
d5392bb5da3c Added generated files
lost
parents:
diff changeset
135 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
136 FILE *stream = stdout;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
137 CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
138 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
139
d5392bb5da3c Added generated files
lost
parents:
diff changeset
140 size_t
d5392bb5da3c Added generated files
lost
parents:
diff changeset
141 fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
142 #undef fwrite
d5392bb5da3c Added generated files
lost
parents:
diff changeset
143 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
144 CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
145 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
146
d5392bb5da3c Added generated files
lost
parents:
diff changeset
147 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
148 #endif