1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
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.
16 */
17
18 /*
19 FUNCTION
20 <<fwrite>>, <<fwrite_unlocked>>---write array elements
21
22 INDEX
23 fwrite
24 INDEX
25 fwrite_unlocked
26 INDEX
27 _fwrite_r
28 INDEX
29 _fwrite_unlocked_r
30
31 SYNOPSIS
32 #include <stdio.h>
33 size_t fwrite(const void *restrict <[buf]>, size_t <[size]>,
34 size_t <[count]>, FILE *restrict <[fp]>);
35
36 #define _BSD_SOURCE
37 #include <stdio.h>
38 size_t fwrite_unlocked(const void *restrict <[buf]>, size_t <[size]>,
39 size_t <[count]>, FILE *restrict <[fp]>);
40
41 #include <stdio.h>
42 size_t fwrite( const void *restrict <[buf]>, size_t <[size]>,
43 size_t <[count]>, FILE *restrict <[fp]>);
44
45 #include <stdio.h>
46 size_t fwrite_unlocked( const void *restrict <[buf]>, size_t <[size]>,
47 size_t <[count]>, FILE *restrict <[fp]>);
48
49 DESCRIPTION
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.
54
55 <<fwrite>> also advances the file position indicator (if any) for
56 <[fp]> by the number of @emph{characters} actually written.
57
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>>.
66
67 <<_fwrite_r>> and <<_fwrite_unlocked_r>> are simply reentrant versions of the
68 above that take an additional reentrant structure argument: <[ptr]>.
69
70 RETURNS
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
74 the file.
75
76 PORTABILITY
77 ANSI C requires <<fwrite>>.
78
79 <<fwrite_unlocked>> is a BSD extension also provided by GNU libc.
80
81 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
82 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
83 */
84
85 #if defined(LIBC_SCCS) && !defined(lint)
86 static char sccsid[] = "%W% (Berkeley) %G%";
87 #endif /* LIBC_SCCS and not lint */
88
89 #define _DEFAULT_SOURCE
90 #include <_ansi.h>
91 #include <stdio.h>
92 #include <string.h>
93 #if 0
94 #include <sys/stdc.h>
95 #endif
96 #include "local.h"
97 #if 1
98 #include "fvwrite.h"
99 #endif
100
101 #ifdef __IMPL_UNLOCKED__
102 #define _fwrite_r _fwrite_unlocked_r
103 #define fwrite fwrite_unlocked
104 #endif
105
106 /*
107 * Write `count' objects (each size `size') from memory to the given file.
108 * Return the number of whole objects written.
109 */
110
111 size_t
fwrite(const void * __restrict buf,size_t size,size_t count,FILE * __restrict fp)112 fwrite (
113 const void *__restrict buf,
114 size_t size,
115 size_t count,
116 FILE * __restrict fp)
117 {
118 size_t n;
119 #ifdef _FVWRITE_IN_STREAMIO
120 struct __suio uio;
121 struct __siov iov;
122
123 iov.iov_base = buf;
124 if (!(uio.uio_resid = iov.iov_len = n = count * size))
125 return 0;
126 uio.uio_iov = &iov;
127 uio.uio_iovcnt = 1;
128
129 /*
130 * The usual case is success (__sfvwrite_r returns 0);
131 * skip the divide if this happens, since divides are
132 * generally slow and since this occurs whenever size==0.
133 */
134
135 CHECK_INIT(ptr, fp);
136
137 _newlib_flockfile_start (fp);
138 ORIENT (fp, -1);
139 if (_sfvwrite ( fp, &uio) == 0)
140 {
141 _newlib_flockfile_exit (fp);
142 return count;
143 }
144 _newlib_flockfile_end (fp);
145 return (n - uio.uio_resid) / size;
146 #else
147 size_t i = 0;
148 const char *p = buf;
149 if (!(n = count * size))
150 return 0;
151 CHECK_INIT (ptr, fp);
152
153 _newlib_flockfile_start (fp);
154 ORIENT (fp, -1);
155 /* Make sure we can write. */
156 if (cantwrite (ptr, fp))
157 goto ret;
158
159 while (i < n)
160 {
161 if (_sputc ( p[i], fp) == EOF)
162 break;
163
164 i++;
165 }
166
167 ret:
168 _newlib_flockfile_end (fp);
169 return i / size;
170 #endif
171 }
172