Subversion Repositories Kolibri OS

Rev

Rev 4921 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4349 Serge 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
/*
19
FUNCTION
6099 serge 20
<>, <>---write array elements
4349 Serge 21
 
22
INDEX
23
	fwrite
24
INDEX
6099 serge 25
	fwrite_unlocked
26
INDEX
4349 Serge 27
	_fwrite_r
6099 serge 28
INDEX
29
	_fwrite_unlocked_r
4349 Serge 30
 
31
ANSI_SYNOPSIS
32
	#include 
4921 Serge 33
	size_t fwrite(const void *restrict <[buf]>, size_t <[size]>,
34
		      size_t <[count]>, FILE *restrict <[fp]>);
4349 Serge 35
 
6099 serge 36
	#define _BSD_SOURCE
4349 Serge 37
	#include 
6099 serge 38
	size_t fwrite_unlocked(const void *restrict <[buf]>, size_t <[size]>,
39
		      size_t <[count]>, FILE *restrict <[fp]>);
40
 
41
	#include 
4921 Serge 42
	size_t _fwrite_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
43
		      size_t <[count]>, FILE *restrict <[fp]>);
4349 Serge 44
 
6099 serge 45
	#include 
46
	size_t _fwrite_unlocked_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
47
		      size_t <[count]>, FILE *restrict <[fp]>);
48
 
4349 Serge 49
TRAD_SYNOPSIS
50
	#include 
51
	size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>)
52
	char *<[buf]>;
53
	size_t <[size]>;
54
	size_t <[count]>;
55
	FILE *<[fp]>;
56
 
6099 serge 57
	#define _BSD_SOURCE
4349 Serge 58
	#include 
6099 serge 59
	size_t fwrite_unlocked(<[buf]>, <[size]>, <[count]>, <[fp]>)
60
	char *<[buf]>;
61
	size_t <[size]>;
62
	size_t <[count]>;
63
	FILE *<[fp]>;
64
 
65
	#include 
4349 Serge 66
	size_t _fwrite_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
67
	struct _reent *<[ptr]>;
68
	char *<[buf]>;
69
	size_t <[size]>;
70
	size_t <[count]>;
71
	FILE *<[fp]>;
72
 
6099 serge 73
	#include 
74
	size_t _fwrite_unlocked_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
75
	struct _reent *<[ptr]>;
76
	char *<[buf]>;
77
	size_t <[size]>;
78
	size_t <[count]>;
79
	FILE *<[fp]>;
80
 
4349 Serge 81
DESCRIPTION
82
<> attempts to copy, starting from the memory location
83
<[buf]>, <[count]> elements (each of size <[size]>) into the file or
84
stream identified by <[fp]>.  <> may copy fewer elements than
85
<[count]> if an error intervenes.
86
 
87
<> also advances the file position indicator (if any) for
88
<[fp]> by the number of @emph{characters} actually written.
89
 
6099 serge 90
<> is a non-thread-safe version of <>.
91
<> may only safely be used within a scope
92
protected by flockfile() (or ftrylockfile()) and funlockfile().  This
93
function may safely be used in a multi-threaded program if and only
94
if they are called while the invoking thread owns the (FILE *)
95
object, as is the case after a successful call to the flockfile() or
96
ftrylockfile() functions.  If threads are disabled, then
97
<> is equivalent to <>.
4349 Serge 98
 
6099 serge 99
<<_fwrite_r>> and <<_fwrite_unlocked_r>> are simply reentrant versions of the
100
above that take an additional reentrant structure argument: <[ptr]>.
101
 
4349 Serge 102
RETURNS
103
If <> succeeds in writing all the elements you specify, the
104
result is the same as the argument <[count]>.  In any event, the
105
result is the number of complete elements that <> copied to
106
the file.
107
 
108
PORTABILITY
109
ANSI C requires <>.
110
 
6099 serge 111
<> is a BSD extension also provided by GNU libc.
112
 
4349 Serge 113
Supporting OS subroutines required: <>, <>, <>,
114
<>, <>, <>, <>.
115
*/
116
 
117
#if defined(LIBC_SCCS) && !defined(lint)
118
static char sccsid[] = "%W% (Berkeley) %G%";
119
#endif /* LIBC_SCCS and not lint */
120
 
121
#include <_ansi.h>
122
#include 
123
#include 
124
#if 0
125
#include 
126
#endif
127
#include "local.h"
128
#if 1
129
#include "fvwrite.h"
130
#endif
131
 
6099 serge 132
#ifdef __IMPL_UNLOCKED__
133
#define _fwrite_r _fwrite_unlocked_r
134
#define fwrite fwrite_unlocked
135
#endif
136
 
4349 Serge 137
/*
138
 * Write `count' objects (each size `size') from memory to the given file.
139
 * Return the number of whole objects written.
140
 */
141
 
142
size_t
143
_DEFUN(_fwrite_r, (ptr, buf, size, count, fp),
144
       struct _reent * ptr _AND
4921 Serge 145
       _CONST _PTR __restrict buf _AND
4349 Serge 146
       size_t size     _AND
147
       size_t count    _AND
4921 Serge 148
       FILE * __restrict fp)
4349 Serge 149
{
150
  size_t n;
4921 Serge 151
#ifdef _FVWRITE_IN_STREAMIO
4349 Serge 152
  struct __suio uio;
153
  struct __siov iov;
154
 
155
  iov.iov_base = buf;
156
  uio.uio_resid = iov.iov_len = n = count * size;
157
  uio.uio_iov = &iov;
158
  uio.uio_iovcnt = 1;
159
 
160
  /*
161
   * The usual case is success (__sfvwrite_r returns 0);
162
   * skip the divide if this happens, since divides are
163
   * generally slow and since this occurs whenever size==0.
164
   */
165
 
166
  CHECK_INIT(ptr, fp);
167
 
4921 Serge 168
  _newlib_flockfile_start (fp);
4349 Serge 169
  ORIENT (fp, -1);
170
  if (__sfvwrite_r (ptr, fp, &uio) == 0)
171
    {
4921 Serge 172
      _newlib_flockfile_exit (fp);
4349 Serge 173
      return count;
174
    }
4921 Serge 175
  _newlib_flockfile_end (fp);
4349 Serge 176
  return (n - uio.uio_resid) / size;
4921 Serge 177
#else
178
  size_t i = 0;
179
  _CONST char *p = buf;
180
  n = count * size;
181
  CHECK_INIT (ptr, fp);
182
 
183
  _newlib_flockfile_start (fp);
184
  ORIENT (fp, -1);
185
  /* Make sure we can write.  */
186
  if (cantwrite (ptr, fp))
187
    goto ret;
188
 
189
  while (i < n)
190
    {
191
      if (__sputc_r (ptr, p[i], fp) == EOF)
192
	break;
193
 
194
      i++;
195
    }
196
 
197
ret:
198
  _newlib_flockfile_end (fp);
199
  return i / size;
200
#endif
4349 Serge 201
}
202
 
203
#ifndef _REENT_ONLY
204
size_t
205
_DEFUN(fwrite, (buf, size, count, fp),
4921 Serge 206
       _CONST _PTR __restrict buf _AND
4349 Serge 207
       size_t size     _AND
208
       size_t count    _AND
209
       FILE * fp)
210
{
211
  return _fwrite_r (_REENT, buf, size, count, fp);
212
}
213
#endif