source: trunk/libs/newlib/src/newlib/libc/stdio/fwrite.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.8 KB
Line 
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 * advertising materials, and 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/*
19FUNCTION
20<<fwrite>>, <<fwrite_unlocked>>---write array elements
21
22INDEX
23        fwrite
24INDEX
25        fwrite_unlocked
26INDEX
27        _fwrite_r
28INDEX
29        _fwrite_unlocked_r
30
31SYNOPSIS
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_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
43                      size_t <[count]>, FILE *restrict <[fp]>);
44
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]>);
48
49DESCRIPTION
50<<fwrite>> attempts to copy, starting from the memory location
51<[buf]>, <[count]> elements (each of size <[size]>) into the file or
52stream 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
60protected by flockfile() (or ftrylockfile()) and funlockfile().  This
61function may safely be used in a multi-threaded program if and only
62if they are called while the invoking thread owns the (FILE *)
63object, as is the case after a successful call to the flockfile() or
64ftrylockfile() 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
68above that take an additional reentrant structure argument: <[ptr]>.
69
70RETURNS
71If <<fwrite>> succeeds in writing all the elements you specify, the
72result is the same as the argument <[count]>.  In any event, the
73result is the number of complete elements that <<fwrite>> copied to
74the file.
75
76PORTABILITY
77ANSI C requires <<fwrite>>.
78
79<<fwrite_unlocked>> is a BSD extension also provided by GNU libc.
80
81Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
82<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
83*/
84
85#if defined(LIBC_SCCS) && !defined(lint)
86static char sccsid[] = "%W% (Berkeley) %G%";
87#endif /* LIBC_SCCS and not lint */
88
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
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
110size_t
111_fwrite_r (struct _reent * ptr,
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  uio.uio_resid = iov.iov_len = n = count * size;
124  uio.uio_iov = &iov;
125  uio.uio_iovcnt = 1;
126
127  /*
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.
131   */
132
133  CHECK_INIT(ptr, fp);
134
135  _newlib_flockfile_start (fp);
136  ORIENT (fp, -1);
137  if (__sfvwrite_r (ptr, fp, &uio) == 0)
138    {
139      _newlib_flockfile_exit (fp);
140      return count;
141    }
142  _newlib_flockfile_end (fp);
143  return (n - uio.uio_resid) / size;
144#else
145  size_t i = 0;
146  const char *p = buf;
147  n = count * size;
148  CHECK_INIT (ptr, fp);
149
150  _newlib_flockfile_start (fp);
151  ORIENT (fp, -1);
152  /* Make sure we can write.  */
153  if (cantwrite (ptr, fp))
154    goto ret;
155
156  while (i < n)
157    {
158      if (__sputc_r (ptr, p[i], fp) == EOF)
159        break;
160
161      i++;
162    }
163
164ret:
165  _newlib_flockfile_end (fp);
166  return i / size;
167#endif
168}
169
170#ifndef _REENT_ONLY
171size_t
172fwrite (const void *__restrict buf,
173       size_t size,
174       size_t count,
175       FILE * fp)
176{
177  return _fwrite_r (_REENT, buf, size, count, fp);
178}
179#endif
Note: See TracBrowser for help on using the repository browser.