2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 <<fwrite>>, <<fwrite_unlocked>>---write array elements
33 size_t fwrite(const void *restrict <[buf]>, size_t <[size]>,
34 size_t <[count]>, FILE *restrict <[fp]>);
38 size_t fwrite_unlocked(const void *restrict <[buf]>, size_t <[size]>,
39 size_t <[count]>, FILE *restrict <[fp]>);
42 size_t _fwrite_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
43 size_t <[count]>, FILE *restrict <[fp]>);
46 size_t _fwrite_unlocked_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
47 size_t <[count]>, FILE *restrict <[fp]>);
50 <<fwrite>> attempts to copy, starting from the memory location
51 <[buf]>, <[count]> elements (each of size <[size]>) into the file or
52 stream identified by <[fp]>. <<fwrite>> may copy fewer elements than
53 <[count]> if an error intervenes.
55 <<fwrite>> also advances the file position indicator (if any) for
56 <[fp]> by the number of @emph{characters} actually written.
58 <<fwrite_unlocked>> is a non-thread-safe version of <<fwrite>>.
59 <<fwrite_unlocked>> may only safely be used within a scope
60 protected by flockfile() (or ftrylockfile()) and funlockfile(). This
61 function may safely be used in a multi-threaded program if and only
62 if they are called while the invoking thread owns the (FILE *)
63 object, as is the case after a successful call to the flockfile() or
64 ftrylockfile() functions. If threads are disabled, then
65 <<fwrite_unlocked>> is equivalent to <<fwrite>>.
67 <<_fwrite_r>> and <<_fwrite_unlocked_r>> are simply reentrant versions of the
68 above that take an additional reentrant structure argument: <[ptr]>.
71 If <<fwrite>> succeeds in writing all the elements you specify, the
72 result is the same as the argument <[count]>. In any event, the
73 result is the number of complete elements that <<fwrite>> copied to
77 ANSI C requires <<fwrite>>.
79 <<fwrite_unlocked>> is a BSD extension also provided by GNU libc.
81 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
82 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
85 #if defined(LIBC_SCCS) && !defined(lint)
86 static char sccsid
[] = "%W% (Berkeley) %G%";
87 #endif /* LIBC_SCCS and not lint */
100 #ifdef __IMPL_UNLOCKED__
101 #define _fwrite_r _fwrite_unlocked_r
102 #define fwrite fwrite_unlocked
106 * Write `count' objects (each size `size') from memory to the given file.
107 * Return the number of whole objects written.
111 _fwrite_r (struct _reent
* ptr
,
112 const void *__restrict buf
,
115 FILE * __restrict fp
)
118 #ifdef _FVWRITE_IN_STREAMIO
123 uio
.uio_resid
= iov
.iov_len
= n
= count
* size
;
128 * The usual case is success (__sfvwrite_r returns 0);
129 * skip the divide if this happens, since divides are
130 * generally slow and since this occurs whenever size==0.
135 _newlib_flockfile_start (fp
);
136 if (ORIENT (fp
, -1) != -1)
138 _newlib_flockfile_exit (fp
);
141 if (__sfvwrite_r (ptr
, fp
, &uio
) == 0)
143 _newlib_flockfile_exit (fp
);
146 _newlib_flockfile_end (fp
);
147 return (n
- uio
.uio_resid
) / size
;
152 CHECK_INIT (ptr
, fp
);
154 _newlib_flockfile_start (fp
);
155 /* Make sure we can write. */
156 if (cantwrite (ptr
, fp
))
161 if (__sputc_r (ptr
, p
[i
], fp
) == EOF
)
168 _newlib_flockfile_end (fp
);
175 fwrite (const void *__restrict buf
,
180 return _fwrite_r (_REENT
, buf
, size
, count
, fp
);