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 <stdio.h>
91 #include <string.h>
92 #if 0
93 #include <sys/stdc.h>
94 #endif
95 #include "local.h"
96 #if 1
97 #include "fvwrite.h"
98 #endif
99 
100 #ifdef __IMPL_UNLOCKED__
101 #define _fwrite_r _fwrite_unlocked_r
102 #define fwrite fwrite_unlocked
103 #endif
104 
105 /*
106  * Write `count' objects (each size `size') from memory to the given file.
107  * Return the number of whole objects written.
108  */
109 
110 size_t
fwrite(const void * __restrict buf,size_t size,size_t count,FILE * __restrict fp)111 fwrite (
112        const void *__restrict buf,
113        size_t size,
114        size_t count,
115        FILE * __restrict fp)
116 {
117   size_t n;
118 #ifdef _FVWRITE_IN_STREAMIO
119   struct __suio uio;
120   struct __siov iov;
121 
122   iov.iov_base = buf;
123   if (!(uio.uio_resid = iov.iov_len = n = count * size))
124       return 0;
125   uio.uio_iov = &iov;
126   uio.uio_iovcnt = 1;
127 
128   /*
129    * The usual case is success (__sfvwrite_r returns 0);
130    * skip the divide if this happens, since divides are
131    * generally slow and since this occurs whenever size==0.
132    */
133 
134   CHECK_INIT(ptr, fp);
135 
136   _newlib_flockfile_start (fp);
137   if (ORIENT (fp, -1) != -1)
138     {
139       _newlib_flockfile_exit (fp);
140       return 0;
141     }
142   if (_sfvwrite (fp, &uio) == 0)
143     {
144       _newlib_flockfile_exit (fp);
145       return count;
146     }
147   _newlib_flockfile_end (fp);
148   return (n - uio.uio_resid) / size;
149 #else
150   size_t i = 0;
151   const char *p = buf;
152   if (!(n = count * size))
153       return 0;
154   CHECK_INIT (ptr, fp);
155 
156   _newlib_flockfile_start (fp);
157   /* Make sure we can write.  */
158   if (cantwrite (ptr, fp))
159     goto ret;
160 
161   while (i < n)
162     {
163       if (_sputc ( p[i], fp) == EOF)
164 	break;
165 
166       i++;
167     }
168 
169 ret:
170   _newlib_flockfile_end (fp);
171   return i / size;
172 #endif
173 }
174