1570af302Sopenharmony_ci#include "stdio_impl.h"
2570af302Sopenharmony_ci#include <string.h>
3570af302Sopenharmony_ci
4570af302Sopenharmony_cisize_t __fwritex(const unsigned char *restrict s, size_t l, FILE *restrict f)
5570af302Sopenharmony_ci{
6570af302Sopenharmony_ci	size_t i=0;
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci	if (!f->wend && __towrite(f)) return 0;
9570af302Sopenharmony_ci
10570af302Sopenharmony_ci	if (l > f->wend - f->wpos) return f->write(f, s, l);
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci	if (f->lbf >= 0) {
13570af302Sopenharmony_ci		/* Match /^(.*\n|)/ */
14570af302Sopenharmony_ci		for (i=l; i && s[i-1] != '\n'; i--);
15570af302Sopenharmony_ci		if (i) {
16570af302Sopenharmony_ci			size_t n = f->write(f, s, i);
17570af302Sopenharmony_ci			if (n < i) return n;
18570af302Sopenharmony_ci			s += i;
19570af302Sopenharmony_ci			l -= i;
20570af302Sopenharmony_ci		}
21570af302Sopenharmony_ci	}
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci	memcpy(f->wpos, s, l);
24570af302Sopenharmony_ci	f->wpos += l;
25570af302Sopenharmony_ci	return l+i;
26570af302Sopenharmony_ci}
27570af302Sopenharmony_ci
28570af302Sopenharmony_cisize_t fwrite(const void *restrict src, size_t size, size_t nmemb, FILE *restrict f)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci	size_t k, l = size*nmemb;
31570af302Sopenharmony_ci	if (!size) nmemb = 0;
32570af302Sopenharmony_ci	FLOCK(f);
33570af302Sopenharmony_ci	k = __fwritex(src, l, f);
34570af302Sopenharmony_ci	FUNLOCK(f);
35570af302Sopenharmony_ci	return k==l ? nmemb : k/size;
36570af302Sopenharmony_ci}
37570af302Sopenharmony_ci
38570af302Sopenharmony_ciweak_alias(fwrite, fwrite_unlocked);
39