1 #include "stdio_impl.h"
2 #include <string.h>
3
__fwritex(const unsigned char * restrict s,size_t l,FILE * restrict f)4 size_t __fwritex(const unsigned char* restrict s, size_t l, FILE* restrict f) {
5 size_t i = 0;
6
7 if (!f->wend && __towrite(f))
8 return 0;
9
10 if (l > f->wend - f->wpos)
11 return f->write(f, s, l);
12
13 if (f->lbf >= 0) {
14 /* Match /^(.*\n|)/ */
15 for (i = l; i && s[i - 1] != '\n'; i--)
16 ;
17 if (i) {
18 size_t n = f->write(f, s, i);
19 if (n < i)
20 return n;
21 s += i;
22 l -= i;
23 }
24 }
25
26 memcpy(f->wpos, s, l);
27 f->wpos += l;
28 return l + i;
29 }
30
fwrite(const void * restrict src,size_t size,size_t nmemb,FILE * restrict f)31 size_t fwrite(const void* restrict src, size_t size, size_t nmemb, FILE* restrict f) {
32 size_t k, l = size * nmemb;
33 if (!size)
34 nmemb = 0;
35 FLOCK(f);
36 k = __fwritex(src, l, f);
37 FUNLOCK(f);
38 return k == l ? nmemb : k / size;
39 }
40
41 weak_alias(fwrite, fwrite_unlocked);
42