/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* David Korn
* Glenn Fowler
* AT&T Bell Laboratories
*
* cmp
*/
static const char usage[] =
"[-?\n@(#)$Id: cmp (AT&T Research) 2010-04-11 $\n]"
"[+NAME?cmp - compare two files]"
"[+DESCRIPTION?\bcmp\b compares two files \afile1\a and \afile2\a. "
"\bcmp\b writes no output if the files are the same. By default, if the "
"files differ, the byte and line number at which the first difference "
"occurred are written to standard output. Bytes and lines are numbered "
"beginning with 1.]"
"[+?If \askip1\a or \askip2\a are specified, or the \b-i\b option is "
"specified, initial bytes of the corresponding file are skipped before "
"beginning the compare. The skip values are in bytes or can have a "
"suffix of \bk\b for kilobytes or \bm\b for megabytes.]"
"[+?If either \afile1\a or \afiles2\a is \b-\b, \bcmp\b uses standard "
"input starting at the current location.]"
"[b:print-bytes?Print differing bytes as 3 digit octal values.]"
"[c:print-chars?Print differing bytes as follows: non-space printable "
"characters as themselves; space and control characters as \b^\b "
"followed by a letter of the alphabet; and characters with the high bit "
"set as the lower 7 bit character prefixed by \bM^\b for 7 bit space and "
"non-printable characters and \bM-\b for all other characters. If the 7 "
"bit character encoding is not ASCII then the characters are converted "
"to ASCII to determine \ahigh bit set\a, and if set it is cleared and "
"converted back to the native encoding. Multibyte characters in the "
"current locale are treated as printable characters.]"
"[d:differences?Print at most \adifferences\a differences using "
"\b--verbose\b output format. \b--differences=0\b is equivalent to "
"\b--silent\b.]#[differences]"
"[i:ignore-initial|skip?Skip the the first \askip1\a bytes in \afile1\a "
"and the first \askip2\a bytes in \afile2\a. If \askip2\a is omitted "
"then \askip1\a is used.]:[skip1[::skip2]]:=0::0]"
"[l:verbose?Write the decimal byte number and the differing bytes (in "
"octal) for each difference.]"
"[n:count|bytes?Compare at most \acount\a bytes.]#[count]"
"[s:quiet|silent?Write nothing for differing files; return non-zero exit "
"status only.]"
"\n"
"\nfile1 file2 [skip1 [skip2]]\n"
"\n"
"[+EXIT STATUS?]"
"{"
"[+0?The files or portions compared are identical.]"
"[+1?The files are different.]"
"[+>1?An error occurred.]"
"}"
"[+SEE ALSO?\bcomm\b(1), \bdiff\b(1), \bcat\b(1)]"
;
#include <cmd.h>
#include <ls.h>
#include <ctype.h>
#include <ccode.h>
static void
{
int c;
int m;
char* s;
s = buf;
{
*s++ = ' ';
*s++ = ' ';
*s++ = '0' + ((o >> 6) & 07);
*s++ = '0' + ((o >> 3) & 07);
*s++ = '0' + (o & 07);
}
{
*s++ = ' ';
if (c & 0x80)
{
m = 1;
*s++ = 'M';
c &= 0x7f;
}
else
m = 0;
{
if (!m)
*s++ = ' ';
*s++ = '^';
c ^= 0x40;
}
else if (m)
*s++ = '-';
else
{
*s++ = ' ';
*s++ = ' ';
}
*s++ = o;
}
*s = 0;
}
/*
* compare two files
*/
static int
cmp(const char* file1, Sfio_t* f1, const char* file2, Sfio_t* f2, int flags, Sfoff_t count, Sfoff_t differences)
{
register int c1;
register int c2;
register unsigned char* p1 = 0;
register unsigned char* p2 = 0;
register unsigned char* e1 = 0;
register unsigned char* e2 = 0;
int n1 = 0;
int ret = 0;
unsigned char* last;
for (;;)
{
{
return ret;
{
{
ret = 1;
if (!(flags & CMP_SILENT))
}
return ret;
}
}
{
{
if (!(flags & CMP_SILENT))
return 1;
}
}
if (flags & CMP_SILENT)
{
return 1;
}
else
{
{
{
if (differences >= 0)
{
if (!differences)
return 1;
differences--;
}
#if 0
if (!flags)
sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u\n", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines);
else
{
}
#else
if (flags & CMP_VERBOSE)
else
sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines);
{
}
else
#endif
return 1;
ret = 1;
}
if (c1 == '\n')
lines++;
}
}
}
}
int
{
char* s;
char* e;
char* file1;
char* file2;
int n;
int flags = 0;
for (;;)
{
{
case 'b':
continue;
case 'c':
continue;
case 'd':
flags |= CMP_VERBOSE;
continue;
case 'i':
if (*e == ':')
else
if (*e)
{
break;
}
continue;
case 'l':
flags |= CMP_VERBOSE;
continue;
case 'n':
continue;
case 's':
flags |= CMP_SILENT;
continue;
case ':':
break;
case '?':
break;
}
break;
}
n = 2;
{
if (!(flags & CMP_SILENT))
goto done;
}
{
if (!(flags & CMP_SILENT))
goto done;
}
if (s = *argv++)
{
if (*e)
{
goto done;
}
if (s = *argv++)
{
if (*e)
{
goto done;
}
}
if (*argv)
{
goto done;
}
}
{
if (!(flags & CMP_SILENT))
n = 1;
goto done;
}
{
if (!(flags & CMP_SILENT))
n = 1;
goto done;
}
n = 0;
else
n = ((flags & CMP_SILENT) && S_ISREG(s1.st_mode) && S_ISREG(s2.st_mode) && (s1.st_size - o1) != (s2.st_size - o2)) ? 1 : cmp(file1, f1, file2, f2, flags, count, differences);
done:
return n;
}