commit 429a08d099c104950dfed8d7340cb1bc99831f6e
parent ac5431b14092dfa8dbe2431dbed813804ab3d577
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 7 Mar 2023 11:47:54 +0100
Fix comparison for files of different lengths
Diffstat:
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/hexdiff.c b/hexdiff.c
@@ -37,12 +37,12 @@ bool use_color;
bool has_color;
static inline void
-color_byte(uint8_t b1, uint8_t b2)
+color_byte(struct file *cur, struct file *other, int i)
{
- if (b1 != b2)
- printf("\x1b[38:5:%um", diff_gradient[b1]);
+ if (i >= other->buflen || cur->buf[i] != other->buf[i])
+ printf("\x1b[38:5:%um", diff_gradient[cur->buf[i]]);
else
- printf("\x1b[38:5:%um", equal_gradient[b1]);
+ printf("\x1b[38:5:%um", equal_gradient[cur->buf[i]]);
has_color = true;
}
@@ -97,15 +97,23 @@ main(int argc, const char **argv)
printf("%06lx: ", pos);
for (i = 0; i < 16; i++) {
- if (use_color) color_byte(f1.buf[i], f2.buf[i]);
- printf("%02x ", f1.buf[i]);
- if (has_color) color_clear();
+ if (i < f1.buflen) {
+ if (use_color) color_byte(&f1, &f2, i);
+ printf("%02x ", f1.buf[i]);
+ if (has_color) color_clear();
+ } else {
+ printf(" ");
+ }
}
printf("| ");
for (i = 0; i < 16; i++) {
- if (use_color) color_byte(f2.buf[i], f1.buf[i]);
- printf("%02x ", f2.buf[i]);
- if (has_color) color_clear();
+ if (i < f2.buflen) {
+ if (use_color) color_byte(&f2, &f1, i);
+ printf("%02x ", f2.buf[i]);
+ if (has_color) color_clear();
+ } else {
+ printf(" ");
+ }
}
printf("\n");