/* cmp.c - Compare two files. * * Copyright 2012 Timothy Elliott * * See http://opengroup.org/onlinepubs/9699919799/utilities/cmp.html USE_CMP(NEWTOY(cmp, "<2>2ls(silent)(quiet)[!ls]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2))) config CMP bool "cmp" default y help usage: cmp [-l] [-s] FILE1 FILE2 Compare the contents of two files. -l Show all differing bytes -s Silent */ #define FOR_cmp #include "toys.h" GLOBALS( int fd; char *name; ) static void do_cmp(int fd, char *name) { int i, len1, len2, min_len, size = sizeof(toybuf)/2; long byte_no = 1, line_no = 1; char *buf2 = toybuf+size; // First time through, cache the data and return. if (!TT.fd) { TT.name = name; // On return the old filehandle is closed, and this assures that even // if we were called with stdin closed, the new filehandle != 0. TT.fd = dup(fd); return; } toys.exitval = 0; for (;;) { len1 = readall(TT.fd, toybuf, size); len2 = readall(fd, buf2, size); min_len = len1 < len2 ? len1 : len2; for (i=0; i