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