/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2022 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"

/**
 * @fileoverview scalable diff tool
 *
 * The normal `diff` command can take hours to diff text files that are
 * hundreds of megabytes in size. This tool is a useful replacement for
 * use cases like comparing a log of CPU registers.
 */

char line1[4096];
char line2[4096];

int main(int argc, char *argv[]) {
  int line;
  char *l1, *l2;
  FILE *f1, *f2;
  int differences;
  if (argc < 3) {
    fprintf(stderr, "usage: %s FILE1 FILE2\n", argv[0]);
    exit(1);
  }
  if (!(f1 = fopen(argv[1], "r"))) {
    perror(argv[1]);
    exit(1);
  }
  if (!(f2 = fopen(argv[2], "r"))) {
    perror(argv[2]);
    exit(1);
  }
  for (differences = 0, line = 1;; ++line) {
    l1 = fgets(line1, sizeof(line1), f1);
    l2 = fgets(line2, sizeof(line2), f2);
    if (!l1 && !l2) {
      exit(0);
    }
    if (l1) _chomp(l1);
    if (l2) _chomp(l2);
    if (!l1 || !l2) {
      printf("> %s\n", l1 ? l1 : "EOF");
      printf("< %s\n", l2 ? l2 : "EOF");
      exit(0);
    }
    if (!strcmp(l1, l2)) {
      printf("%s\n", l1);
    } else {
      printf("# line %d differed!\n", line);
      printf("> %s\n", l1);
      printf("< %s\n", l2);
    }
  }
}
