toybox/toys/posix/cmp.c
<<
>>
Prefs
   1/* cmp.c - Compare two files.
   2 *
   3 * Copyright 2012 Timothy Elliott <tle@holymonkey.com>
   4 *
   5 * See http://opengroup.org/onlinepubs/9699919799/utilities/cmp.html
   6
   7USE_CMP(NEWTOY(cmp, "<1>4ls(silent)(quiet)n#<1[!ls]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
   8
   9config CMP
  10  bool "cmp"
  11  default y
  12  help
  13    usage: cmp [-ls] [-n LEN] FILE1 [FILE2 [SKIP1 [SKIP2]]]
  14
  15    Compare the contents of files (vs stdin if only one given), optionally
  16    skipping bytes at start.
  17
  18    -l  Show all differing bytes
  19    -n LEN      Compare at most LEN bytes
  20    -s  Silent
  21*/
  22
  23#define FOR_cmp
  24#include "toys.h"
  25
  26GLOBALS(
  27  long n;
  28
  29  int fd;
  30  char *name;
  31)
  32
  33// We hijack loopfiles() to open and understand the "-" filename for us.
  34static void do_cmp(int fd, char *name)
  35{
  36  int i, len1, len2, min_len, size = sizeof(toybuf)/2;
  37  long long byte_no = 1, line_no = 1;
  38  char *buf2 = toybuf+size;
  39
  40  if (toys.optc>(i = 2+!!TT.fd)) lskip(fd, atolx(toys.optargs[i]));
  41
  42  // First time through, cache the data and return.
  43  if (!TT.fd) {
  44    TT.name = name;
  45    // On return the old filehandle is closed, and this assures that even
  46    // if we were called with stdin closed, the new filehandle != 0.
  47    TT.fd = dup(fd);
  48    return;
  49  }
  50
  51  toys.exitval = 0;
  52
  53  for (;!FLAG(n) || TT.n;) {
  54    if (FLAG(n)) TT.n -= size = minof(size, TT.n);
  55    len1 = readall(TT.fd, toybuf, size);
  56    len2 = readall(fd, buf2, size);
  57    min_len = minof(len1, len2);
  58    for (i = 0; i<min_len; i++) {
  59      if (toybuf[i] != buf2[i]) {
  60        toys.exitval = 1;
  61        if (FLAG(l)) printf("%lld %o %o\n", byte_no, toybuf[i], buf2[i]);
  62        else {
  63          if (!FLAG(s)) printf("%s %s differ: char %lld, line %lld\n",
  64              TT.name, name, byte_no, line_no);
  65          goto out;
  66        }
  67      }
  68      byte_no++;
  69      if (toybuf[i] == '\n') line_no++;
  70    }
  71    if (len1 != len2) {
  72      if (!FLAG(s)) {
  73        strcpy(toybuf, "EOF on %s after byte %lld, line %lld");
  74        if (FLAG(l)) *strchr(toybuf, ',') = 0;
  75        error_msg(toybuf, len1 < len2 ? TT.name : name, byte_no-1, line_no-1);
  76      } else toys.exitval = 1;
  77      break;
  78    }
  79    if (len1 < 1) break;
  80  }
  81out:
  82  if (CFG_TOYBOX_FREE) close(TT.fd);
  83  xexit();
  84}
  85
  86void cmp_main(void)
  87{
  88  toys.exitval = 2;
  89  loopfiles_rw(toys.optargs, O_CLOEXEC|(WARN_ONLY*!FLAG(s)), 0, do_cmp);
  90  if (toys.optc == 1) do_cmp(0, "-");
  91}
  92