Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / fwrite.c
blob9241ea2cd9973aaa3cc02ac6e5d546c88e2d15bd
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
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.
19 FUNCTION
20 <<fwrite>>, <<fwrite_unlocked>>---write array elements
22 INDEX
23 fwrite
24 INDEX
25 fwrite_unlocked
26 INDEX
27 _fwrite_r
28 INDEX
29 _fwrite_unlocked_r
31 SYNOPSIS
32 #include <stdio.h>
33 size_t fwrite(const void *restrict <[buf]>, size_t <[size]>,
34 size_t <[count]>, FILE *restrict <[fp]>);
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]>);
41 #include <stdio.h>
42 size_t _fwrite_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
43 size_t <[count]>, FILE *restrict <[fp]>);
45 #include <stdio.h>
46 size_t _fwrite_unlocked_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
47 size_t <[count]>, FILE *restrict <[fp]>);
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.
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]>.
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.
76 PORTABILITY
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 */
89 #include <_ansi.h>
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
100 #ifdef __IMPL_UNLOCKED__
101 #define _fwrite_r _fwrite_unlocked_r
102 #define fwrite fwrite_unlocked
103 #endif
106 * Write `count' objects (each size `size') from memory to the given file.
107 * Return the number of whole objects written.
110 size_t
111 _fwrite_r (struct _reent * ptr,
112 const void *__restrict buf,
113 size_t size,
114 size_t count,
115 FILE * __restrict fp)
117 size_t n;
118 #ifdef _FVWRITE_IN_STREAMIO
119 struct __suio uio;
120 struct __siov iov;
122 iov.iov_base = buf;
123 uio.uio_resid = iov.iov_len = n = count * size;
124 uio.uio_iov = &iov;
125 uio.uio_iovcnt = 1;
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.
133 CHECK_INIT(ptr, fp);
135 _newlib_flockfile_start (fp);
136 if (ORIENT (fp, -1) != -1)
138 _newlib_flockfile_exit (fp);
139 return 0;
141 if (__sfvwrite_r (ptr, fp, &uio) == 0)
143 _newlib_flockfile_exit (fp);
144 return count;
146 _newlib_flockfile_end (fp);
147 return (n - uio.uio_resid) / size;
148 #else
149 size_t i = 0;
150 const char *p = buf;
151 n = count * size;
152 CHECK_INIT (ptr, fp);
154 _newlib_flockfile_start (fp);
155 /* Make sure we can write. */
156 if (cantwrite (ptr, fp))
157 goto ret;
159 while (i < n)
161 if (__sputc_r (ptr, p[i], fp) == EOF)
162 break;
164 i++;
167 ret:
168 _newlib_flockfile_end (fp);
169 return i / size;
170 #endif
173 #ifndef _REENT_ONLY
174 size_t
175 fwrite (const void *__restrict buf,
176 size_t size,
177 size_t count,
178 FILE * fp)
180 return _fwrite_r (_REENT, buf, size, count, fp);
182 #endif