00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <syscall.h>
00008
00009 int
00010 main (int argc, char *argv[])
00011 {
00012 int fd[2];
00013
00014 if (argc != 3)
00015 {
00016 printf ("usage: cmp A B\n");
00017 return EXIT_FAILURE;
00018 }
00019
00020
00021 fd[0] = open (argv[1]);
00022 if (fd[0] < 0)
00023 {
00024 printf ("%s: open failed\n", argv[1]);
00025 return EXIT_FAILURE;
00026 }
00027 fd[1] = open (argv[2]);
00028 if (fd[1] < 0)
00029 {
00030 printf ("%s: open failed\n", argv[1]);
00031 return EXIT_FAILURE;
00032 }
00033
00034
00035 for (;;)
00036 {
00037 int pos;
00038 char buffer[2][1024];
00039 int bytes_read[2];
00040 int min_read;
00041 int i;
00042
00043 pos = tell (fd[0]);
00044 bytes_read[0] = read (fd[0], buffer[0], sizeof buffer[0]);
00045 bytes_read[1] = read (fd[1], buffer[1], sizeof buffer[1]);
00046 min_read = bytes_read[0] < bytes_read[1] ? bytes_read[0] : bytes_read[1];
00047 if (min_read == 0)
00048 break;
00049
00050 for (i = 0; i < min_read; i++)
00051 if (buffer[0][i] != buffer[1][i])
00052 {
00053 printf ("Byte %d is %02hhx ('%c') in %s but %02hhx ('%c') in %s\n",
00054 pos + i,
00055 buffer[0][i], buffer[0][i], argv[1],
00056 buffer[1][i], buffer[1][i], argv[2]);
00057 return EXIT_FAILURE;
00058 }
00059
00060 if (min_read < bytes_read[1])
00061 printf ("%s is shorter than %s\n", argv[1], argv[2]);
00062 else if (min_read < bytes_read[0])
00063 printf ("%s is shorter than %s\n", argv[2], argv[1]);
00064 }
00065
00066 printf ("%s and %s are identical\n", argv[1], argv[2]);
00067
00068 return EXIT_SUCCESS;
00069 }