sync files from diff.git 9879b82a581a245e365fb159488c4294c318d8b3
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
diff --git a/lib/diff_output.c b/lib/diff_output.c
index daf8b8c..7038352 100644
--- a/lib/diff_output.c
+++ b/lib/diff_output.c
@@ -55,6 +55,8 @@ get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
return 0;
}
+#define DIFF_OUTPUT_BUF_SIZE 512
+
int
diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
const char *prefix, struct diff_atom *start_atom,
@@ -71,12 +73,16 @@ diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
foreach_diff_atom(atom, start_atom, count) {
off_t outlen = 0;
- int i, ch;
+ int i, ch, nbuf = 0;
unsigned int len = atom->len;
- rc = fprintf(dest, "%s", prefix);
- if (rc < 0)
- return errno;
- outlen += rc;
+ unsigned char buf[DIFF_OUTPUT_BUF_SIZE + 1 /* '\n' */];
+ size_t n;
+
+ n = strlcpy(buf, prefix, sizeof(buf));
+ if (n >= DIFF_OUTPUT_BUF_SIZE) /* leave room for '\n' */
+ return ENOBUFS;
+ nbuf += n;
+
if (len) {
rc = get_atom_byte(&ch, atom, len - 1);
if (rc)
@@ -96,13 +102,18 @@ diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
rc = get_atom_byte(&ch, atom, i);
if (rc)
return rc;
- rc = fprintf(dest, "%c", (unsigned char)ch);
- if (rc < 0)
- return errno;
- outlen += rc;
+ if (nbuf >= DIFF_OUTPUT_BUF_SIZE) {
+ rc = fwrite(buf, 1, nbuf, dest);
+ if (rc != nbuf)
+ return errno;
+ outlen += rc;
+ nbuf = 0;
+ }
+ buf[nbuf++] = ch;
}
- rc = fprintf(dest, "\n");
- if (rc < 0)
+ buf[nbuf++] = '\n';
+ rc = fwrite(buf, 1, nbuf, dest);
+ if (rc != nbuf)
return errno;
outlen += rc;
if (outinfo) {