summaryrefslogtreecommitdiff
path: root/src/stdio/fwrite.c
blob: ab2e0f04a35a20e8a279c6364e6a9014bc984a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include "_stdio.h"

/** write directly to a file stream **/

size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
{
	unsigned char *buf = (unsigned char *)ptr;
	size_t n = 0;

	SIGNAL_SAFE(0);
	ASSERT_NOOVERLAP(ptr, size * nmemb, stream, sizeof(*stream));

	while (nmemb) {
		size_t i;
		for (i = 0; i < size; i++) {
			int c = fputc(buf[i], stream);
			if (c == EOF) {
				/* an error */
			} else {
				n++;
			}
		}
		nmemb--;
		buf += size;
	}
	/*
	RETURN_SUCCESS(the number of elements written);
	*/
	return n;
}

/***
writes ARGUMENT(nmemb) elements of ARGUMENT(size) bytes each from
the array ARGUMENT(ptr) to ARGUMENT(stream).

The file position inidicate is advanced by the number of bytes successfully
written.
***/

/*
STDC(1)
*/