1 /* Copyright (C) 1991, 92, 93, 94, 96, 97 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
24 /* Write NMEMB chunks of SIZE bytes each from PTR onto STREAM. */
26 fwrite (ptr
, size
, nmemb
, stream
)
30 register FILE *stream
;
32 register const unsigned char *p
= (const unsigned char *) ptr
;
33 register size_t to_write
= size
* nmemb
;
34 register size_t written
= 0;
39 if (!__validfp (stream
) || !stream
->__mode
.__write
)
47 if (p
== NULL
|| to_write
== 0)
50 if (!stream
->__seen
|| stream
->__put_limit
== stream
->__buffer
)
52 /* This stream has never been seen before.
53 Calling __flshfp will give it a buffer
54 and I/O functions if it needs them. */
55 if (__flshfp (stream
, *p
++) == EOF
)
64 = stream
->__room_funcs
.__output
== __default_room_functions
.__output
;
69 if (__stdio_check_offset (stream
) == EOF
&& errno
!= ESPIPE
)
78 if (stream
->__buffer
== NULL
&& default_func
&&
79 stream
->__offset
== stream
->__target
)
81 /* This is an unbuffered stream using the standard output
82 buffer-flushing function, so we just do a straight write. */
84 int count
= (stream
->__io_funcs
.__write
== NULL
? to_write
:
85 (*stream
->__io_funcs
.__write
) (stream
->__cookie
,
91 if (stream
->__offset
!= -1)
93 stream
->__offset
+= count
;
94 stream
->__target
= stream
->__offset
;
104 /* We ignore the end pointer here since we want to find out how much space
105 is really in the buffer, even for a line-buffered stream. */
106 buffer_space
= stream
->__bufsize
- (stream
->__bufp
- stream
->__buffer
);
108 newlinep
= (stream
->__linebuf
&&
109 memchr ((const void *) p
, '\n', to_write
) != NULL
);
111 if (newlinep
&& stream
->__bufp
== stream
->__buffer
&&
112 stream
->__offset
== stream
->__target
)
113 /* The buffer's empty, and we want to write our data
114 out soon anyway, so just write it straight out. */
117 if (stream
->__bufsize
== 0 && !default_func
)
119 /* No buffer, and a special function.
120 We can't do much better than putc. */
121 while (to_write
-- > 0)
123 if (__flshfp (stream
, *p
++) == EOF
)
129 else if (!default_func
|| buffer_space
>= to_write
)
131 /* There is enough room in the buffer for everything we want to write
132 or the user has specified his own output buffer-flushing/expanding
137 register size_t n
= to_write
;
139 if (n
> buffer_space
)
149 *stream
->__bufp
++ = *p
++;
152 memcpy ((void *) stream
->__bufp
, (void *) p
, n
);
160 else if (buffer_space
== 0)
162 /* We have filled the buffer, so flush it. */
163 if (fflush (stream
) == EOF
)
166 /* Reset our record of the space available in the buffer,
167 since we have just flushed it. */
169 buffer_space
= (stream
->__bufsize
-
170 (stream
->__bufp
- stream
->__buffer
));
171 if (buffer_space
== 0)
173 /* With a custom output-room function, flushing might
174 not create any buffer space. Try writing a single
175 character to create the space. */
176 if (__flshfp (stream
, *p
++) == EOF
)
185 /* We have written all the data into the buffer. If we are
186 line-buffered and just put a newline in the buffer, flush now to
187 make sure it gets out. */
193 /* It won't all fit in the buffer. */
195 if (stream
->__bufp
!= stream
->__buffer
)
197 /* There are characters in the buffer. Flush them. */
198 if (__flshfp (stream
, EOF
) == EOF
)
202 /* The buffer has been flushed.
203 Now either fill it or write directly. */
205 buffer_space
= stream
->__bufsize
- (stream
->__bufp
- stream
->__buffer
);
207 if (stream
->__offset
== stream
->__target
&&
208 (buffer_space
< to_write
|| newlinep
))
209 /* What we have to write is bigger than the buffer,
210 or it contains a newline and we're line-buffered,
214 /* It will fit in the buffer. */
219 return (size_t) written
/ size
;