#include #include #include #include int main(int argc, char* argv[]) { FILE* f1; FILE* f2; char* progname; char* p; int c1,c2; char buf1[BUFSIZ]; char buf2[BUFSIZ]; int same; p = strrchr(argv[0], '\\'); progname = p ? p+1 : argv[0]; /* name of this program, */ p = strrchr(progname, '.'); /* with path and extension removed */ if (p) *p = 0; if (argc != 3) { printf( "Usage: %s file1 file2 compares file1 and file2\n", progname ); printf( " If the files are identical, file2 is erased.\n" ); printf( " If the files are different, file2 replaces file1.\n" ); printf( " If file1 does not exist, file2 is renamed to file1.\n" ); exit(1); } if (access(argv[1], 0) != 0) /* file1 doesn't exist */ { rename(argv[2], argv[1]); printf("%s created.\n", argv[1]); return 0; } f1 = fopen(argv[1], "r"); if (!f1) { printf("%s: Can't open %s\n", progname, argv[1]); exit(1); } f2 = fopen( argv[2], "r" ); if (!f2) { printf("%s: Can't open %s\n", progname, argv[2]); exit(1); } #if 0 do { c1 = fgetc(f1); c2 = fgetc(f2); } while( c1 == c2 && c1 != EOF ); #endif do { c1 = fread(buf1, 1, BUFSIZ, f1); c2 = fread(buf2, 1, BUFSIZ, f2); same = c1 == c2 && memcmp(buf1, buf2, c1) == 0; } while (same && c1 == BUFSIZ); fclose( f1 ); fclose( f2 ); #if 0 if (c1 == c2) /* == EOF */ #endif if (same) { remove(argv[2]); printf("No change.\n"); return 1; } else { remove(argv[1]); rename(argv[2], argv[1]); printf("%s updated.\n", argv[1]); return 0; } }