1*4887Schin /*********************************************************************** 2*4887Schin * * 3*4887Schin * This software is part of the ast package * 4*4887Schin * Copyright (c) 1992-2007 AT&T Knowledge Ventures * 5*4887Schin * and is licensed under the * 6*4887Schin * Common Public License, Version 1.0 * 7*4887Schin * by AT&T Knowledge Ventures * 8*4887Schin * * 9*4887Schin * A copy of the License is available at * 10*4887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 11*4887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12*4887Schin * * 13*4887Schin * Information and Software Systems Research * 14*4887Schin * AT&T Research * 15*4887Schin * Florham Park NJ * 16*4887Schin * * 17*4887Schin * Glenn Fowler <gsf@research.att.com> * 18*4887Schin * David Korn <dgk@research.att.com> * 19*4887Schin * * 20*4887Schin ***********************************************************************/ 21*4887Schin #pragma prototyped 22*4887Schin /* 23*4887Schin * David Korn 24*4887Schin * Glenn Fowler 25*4887Schin * AT&T Bell Laboratories 26*4887Schin * 27*4887Schin * cmp 28*4887Schin */ 29*4887Schin 30*4887Schin static const char usage[] = 31*4887Schin "[-?\n@(#)$Id: cmp (AT&T Research) 2004-12-01 $\n]" 32*4887Schin USAGE_LICENSE 33*4887Schin "[+NAME?cmp - compare two files]" 34*4887Schin "[+DESCRIPTION?\bcmp\b compares two files \afile1\a and \afile2\a. " 35*4887Schin "\bcmp\b writes no output if the files are the same. By default, " 36*4887Schin "if the files differ, the byte and line number at which the " 37*4887Schin "first difference occurred are written to standard output. Bytes " 38*4887Schin "and lines are numbered beginning with 1.]" 39*4887Schin "[+?If \askip1\a or \askip2\a are specified, or the \b-i\b option is " 40*4887Schin "specified, initial bytes of the corresponding file are skipped " 41*4887Schin "before beginning the compare. The skip values are in bytes or " 42*4887Schin "can have a suffix of \bk\b for kilobytes or \bm\b for megabytes.]" 43*4887Schin "[+?If either \afile1\a or \afiles2\a is \b-\b, \bcmp\b " 44*4887Schin "uses standard input starting at the current location.]" 45*4887Schin "[c:print-chars?Writes control characters as a \b^\b followed by a letter of " 46*4887Schin "the alphabet and precede characters that have the high bit set with " 47*4887Schin "\bM-\b as with \bcat\b(1).]" 48*4887Schin "[i:ignore-initial]#[skip:=0?Sets default skip values for the operands " 49*4887Schin "\askip1\a and \askip2\a to \askip\a.]" 50*4887Schin "[l:verbose?Write the decimal byte number and the differing bytes (in octal) " 51*4887Schin "for each difference.]" 52*4887Schin "[s:quiet|silent?Write nothing for differing files; return non-zero " 53*4887Schin "exit status only.] ]" 54*4887Schin "\n" 55*4887Schin "\nfile1 file2 [skip1 [skip2]]\n" 56*4887Schin "\n" 57*4887Schin "[+EXIT STATUS?]{" 58*4887Schin "[+0?The files or portions compared are identical.]" 59*4887Schin "[+1?The files are different.]" 60*4887Schin "[+>1?An error occurred.]" 61*4887Schin "}" 62*4887Schin "[+SEE ALSO?\bcomm\b(1), \bdiff\b(1), \bcat\b(1)]" 63*4887Schin ; 64*4887Schin 65*4887Schin 66*4887Schin #include <cmd.h> 67*4887Schin #include <ls.h> 68*4887Schin #include <ctype.h> 69*4887Schin 70*4887Schin #define CMP_VERBOSE 1 71*4887Schin #define CMP_SILENT 2 72*4887Schin #define CMP_CHARS 4 73*4887Schin 74*4887Schin #define cntl(x) (x&037) 75*4887Schin #define printchar(c) ((c) ^ ('A'-cntl('A'))) 76*4887Schin 77*4887Schin static void outchar(Sfio_t *out, register int c, int delim) 78*4887Schin { 79*4887Schin if(c&0200) 80*4887Schin { 81*4887Schin sfputc(out,'M'); 82*4887Schin sfputc(out,'-'); 83*4887Schin c &= ~0200; 84*4887Schin } 85*4887Schin else if(!isprint(c)) 86*4887Schin { 87*4887Schin sfputc(out,'^'); 88*4887Schin c = printchar(c); 89*4887Schin } 90*4887Schin sfputc(out,c); 91*4887Schin sfputc(out,delim); 92*4887Schin } 93*4887Schin 94*4887Schin /* 95*4887Schin * compare two files 96*4887Schin */ 97*4887Schin 98*4887Schin static int 99*4887Schin cmp(const char* file1, Sfio_t* f1, const char* file2, Sfio_t* f2, int flags) 100*4887Schin { 101*4887Schin register int c1; 102*4887Schin register int c2; 103*4887Schin register unsigned char* p1 = 0; 104*4887Schin register unsigned char* p2 = 0; 105*4887Schin register Sfoff_t lines = 1; 106*4887Schin register unsigned char* e1 = 0; 107*4887Schin register unsigned char* e2 = 0; 108*4887Schin Sfoff_t pos = 0; 109*4887Schin int ret = 0; 110*4887Schin unsigned char* last; 111*4887Schin 112*4887Schin for (;;) 113*4887Schin { 114*4887Schin if ((c1 = e1 - p1) <= 0) 115*4887Schin { 116*4887Schin if (!(p1 = (unsigned char*)sfreserve(f1, SF_UNBOUND, 0)) || (c1 = sfvalue(f1)) <= 0) 117*4887Schin { 118*4887Schin if ((e2 - p2) > 0 || sfreserve(f2, SF_UNBOUND, 0) && sfvalue(f2) > 0) 119*4887Schin { 120*4887Schin ret = 1; 121*4887Schin if (!(flags & CMP_SILENT)) 122*4887Schin error(ERROR_exit(1), "%s: EOF", file1); 123*4887Schin } 124*4887Schin return(ret); 125*4887Schin } 126*4887Schin e1 = p1 + c1; 127*4887Schin } 128*4887Schin if ((c2 = e2 - p2) <= 0) 129*4887Schin { 130*4887Schin if (!(p2 = (unsigned char*)sfreserve(f2, SF_UNBOUND, 0)) || (c2 = sfvalue(f2)) <= 0) 131*4887Schin { 132*4887Schin if (!(flags & CMP_SILENT)) 133*4887Schin error(ERROR_exit(1), "%s: EOF", file2); 134*4887Schin return(1); 135*4887Schin } 136*4887Schin e2 = p2 + c2; 137*4887Schin } 138*4887Schin if (c1 > c2) 139*4887Schin c1 = c2; 140*4887Schin pos += c1; 141*4887Schin if (flags & CMP_SILENT) 142*4887Schin { 143*4887Schin if (memcmp(p1, p2, c1)) 144*4887Schin return(1); 145*4887Schin p1 += c1; 146*4887Schin p2 += c1; 147*4887Schin } 148*4887Schin else 149*4887Schin { 150*4887Schin last = p1 + c1; 151*4887Schin while (p1 < last) 152*4887Schin { 153*4887Schin if ((c1 = *p1++) != *p2++) 154*4887Schin { 155*4887Schin if (flags) 156*4887Schin { 157*4887Schin ret = 1; 158*4887Schin if(flags&CMP_CHARS) 159*4887Schin { 160*4887Schin sfprintf(sfstdout, "%6I*d ", sizeof(pos), pos - (last - p1)); 161*4887Schin outchar(sfstdout,c1,' '); 162*4887Schin outchar(sfstdout,*(p2-1),'\n'); 163*4887Schin } 164*4887Schin else 165*4887Schin sfprintf(sfstdout, "%6I*d %3o %3o\n", sizeof(pos), pos - (last - p1), c1, *(p2 - 1)); 166*4887Schin } 167*4887Schin else 168*4887Schin { 169*4887Schin sfprintf(sfstdout, "%s %s differ: char %I*d, line %I*u\n", file1, file2, sizeof(pos), pos - (last - p1), sizeof(lines), lines); 170*4887Schin return(1); 171*4887Schin } 172*4887Schin } 173*4887Schin if (c1 == '\n') 174*4887Schin lines++; 175*4887Schin } 176*4887Schin } 177*4887Schin } 178*4887Schin } 179*4887Schin 180*4887Schin int 181*4887Schin b_cmp(int argc, register char** argv, void* context) 182*4887Schin { 183*4887Schin char* s; 184*4887Schin char* e; 185*4887Schin Sfio_t* f1 = 0; 186*4887Schin Sfio_t* f2 = 0; 187*4887Schin char* file1; 188*4887Schin char* file2; 189*4887Schin int n; 190*4887Schin off_t o1 = 0; 191*4887Schin off_t o2 = 0; 192*4887Schin struct stat s1; 193*4887Schin struct stat s2; 194*4887Schin 195*4887Schin int flags = 0; 196*4887Schin 197*4887Schin NoP(argc); 198*4887Schin cmdinit(argc, argv, context, ERROR_CATALOG, 0); 199*4887Schin while (n = optget(argv, usage)) switch (n) 200*4887Schin { 201*4887Schin case 'l': 202*4887Schin flags |= CMP_VERBOSE; 203*4887Schin break; 204*4887Schin case 's': 205*4887Schin flags |= CMP_SILENT; 206*4887Schin break; 207*4887Schin case 'c': 208*4887Schin flags |= CMP_CHARS; 209*4887Schin break; 210*4887Schin case 'i': 211*4887Schin o1 = o2 = opt_info.num; 212*4887Schin break; 213*4887Schin case ':': 214*4887Schin error(2, "%s", opt_info.arg); 215*4887Schin break; 216*4887Schin case '?': 217*4887Schin error(ERROR_usage(2), "%s", opt_info.arg); 218*4887Schin break; 219*4887Schin } 220*4887Schin argv += opt_info.index; 221*4887Schin if (error_info.errors || !(file1 = *argv++) || !(file2 = *argv++)) 222*4887Schin error(ERROR_usage(2), "%s", optusage(NiL)); 223*4887Schin n = 2; 224*4887Schin if (streq(file1, "-")) 225*4887Schin f1 = sfstdin; 226*4887Schin else if (!(f1 = sfopen(NiL, file1, "r"))) 227*4887Schin { 228*4887Schin if (!(flags & CMP_SILENT)) 229*4887Schin error(ERROR_system(0), "%s: cannot open", file1); 230*4887Schin goto done; 231*4887Schin } 232*4887Schin if (streq(file2, "-")) 233*4887Schin f2 = sfstdin; 234*4887Schin else if (!(f2 = sfopen(NiL, file2, "r"))) 235*4887Schin { 236*4887Schin if (!(flags & CMP_SILENT)) 237*4887Schin error(ERROR_system(0), "%s: cannot open", file2); 238*4887Schin goto done; 239*4887Schin } 240*4887Schin if (s = *argv++) 241*4887Schin { 242*4887Schin o1 = strtol(s, &e, 0); 243*4887Schin if (*e) 244*4887Schin { 245*4887Schin error(ERROR_exit(0), "%s: %s: invalid skip", file1, s); 246*4887Schin goto done; 247*4887Schin } 248*4887Schin if (s = *argv++) 249*4887Schin { 250*4887Schin o2 = strtol(s, &e, 0); 251*4887Schin if (*e) 252*4887Schin { 253*4887Schin error(ERROR_exit(0), "%s: %s: invalid skip", file2, s); 254*4887Schin goto done; 255*4887Schin } 256*4887Schin } 257*4887Schin if (*argv) 258*4887Schin { 259*4887Schin error(ERROR_usage(0), "%s", optusage(NiL)); 260*4887Schin goto done; 261*4887Schin } 262*4887Schin } 263*4887Schin if (o1 && sfseek(f1, o1, SEEK_SET) != o1) 264*4887Schin { 265*4887Schin if (!(flags & CMP_SILENT)) 266*4887Schin error(ERROR_exit(0), "%s: EOF", file1); 267*4887Schin n = 1; 268*4887Schin goto done; 269*4887Schin } 270*4887Schin if (o2 && sfseek(f2, o2, SEEK_SET) != o2) 271*4887Schin { 272*4887Schin if (!(flags & CMP_SILENT)) 273*4887Schin error(ERROR_exit(0), "%s: EOF", file2); 274*4887Schin n = 1; 275*4887Schin goto done; 276*4887Schin } 277*4887Schin if (fstat(sffileno(f1), &s1)) 278*4887Schin error(ERROR_system(0), "%s: cannot stat", file1); 279*4887Schin else if (fstat(sffileno(f2), &s2)) 280*4887Schin error(ERROR_system(0), "%s: cannot stat", file1); 281*4887Schin else if (s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev && o1 == o2) 282*4887Schin n = 0; 283*4887Schin 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); 284*4887Schin done: 285*4887Schin if (f1 && f1 != sfstdin) sfclose(f1); 286*4887Schin if (f2 && f2 != sfstdin) sfclose(f2); 287*4887Schin return(n); 288*4887Schin } 289