/* This is the most simple file difference program conceivable. Well, almost, anyway. It compares the two files specified on the command line, and returns an error code, based upon what it finds, to the operating system. If this program is embedded within a batch file, that file can examine this error code, and do something appropriate. Here is the calling sequence of the program: dif [d:][path]filename1 [d:][path]filename2 The quantites enclosed in square brackets, [], are, of course, optional. For example, suppose the first file is named sooie.dat, and it resides on the a: drive, and the second file is named suekaren.dat, and it's in subdirectory \astro\data on the current drive. Then, the calling sequence would be: dif a:sooie.dat \astro\data\suekaren.dat The program returns one of five error codes, numbered 0 through 4, which have the following meanings. 4 Invalid command line syntax. Either too many or too few parameters were given. 3 At least one of the files specified does not exist. 2 The two files are not even of the same size. They are compared no further than this. 1 The files are of the same size, but there is a mismatch somewhere. This code is returned as soon as the first mismatch is discovered. 0 The files match perfectly; byte for byte. Absolutely no differences were found. It should be noted that files of *any* type may be compared with this program; text or binary, it matters not. The program was written for, and compiled and linked with, Aztec C86 Compiler Version 3.40b Charlie Ros5e P.O. Box 1636 Boulder, Colorado, 80306 303-939-5448 88.02.08 Original release. */ #include #include #include #define Syntax 4 /* Error code definitions */ #define NoExist 3 /* */ #define SizeDiff 2 /* */ #define Mismatch 1 /* */ #define Match 0 /* */ main(nargs, arg) char *arg[]; int nargs; { char c1, c2; /* Bytes of comparison */ int err_code = Match; /* The error code */ long size1, size2, /* Sizes of the two files */ k; /* Loop counter */ FILE *fp1, *fp2; /* File pointers */ /* Check for invalid command line syntax. */ if( nargs != 3 ) err_code = Syntax; /* Command line syntax is good. Continue with the other tests. */ else { /* Compute the sizes of the two files. filesize() returns -1L if the specified file does not exist. The next two if()s test for file existence and size-match. */ size1 = filesize( arg[1] ); size2 = filesize( arg[2] ); if( size1 == -1 || size2 == -1 ) err_code = NoExist; else if( size1 != size2 ) err_code = SizeDiff; /* Both files exist, and are of the same size. Now open them, and compare them byte-for-byte. If ever there is a mismatch, close the files imme- diately, set the error code, and break out of the for() loop. */ else { fp1 = fopen(arg[1], "r"); fp2 = fopen(arg[2], "r"); for(k = 0L; k < size1; k++) { c1 = getc(fp1); c2 = getc(fp2); if( c1 != c2 ) { err_code = Mismatch; fclose(fp1); fclose(fp2); break; } } } } exit(err_code); } /************************************************************************/ /* */ /* F i l e s i z e */ /* */ /************************************************************************/ /* Purpose: To determine the size, in bytes, of a specified file. Calling Sequence: char *filename; long size, filesize(); size = filesize(filename); */ long filesize( filename ) char *filename; { long size; struct stat buf; if(access(filename, 0)) size = -1L; else { stat(filename, &buf); size = buf.st_size; } return(size); }