1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdint.h>
5b93e5812faffd3b6c5fb349072413aace31918d8Olivier Bailly#include <unistd.h>
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/ioctl.h>
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint cmp_main(int argc, char *argv[])
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int c;
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd1, fd2;
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	char buf1[4096], buf2[4096];
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int res, res1, res2;
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int rv = 0;
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int i;
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int filepos = 0;
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int show_byte = 0;
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int show_all = 0;
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int limit = 0;
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        c = getopt(argc, argv, "bln:");
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (c == EOF)
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        switch (c) {
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'b':
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            show_byte = 1;
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'l':
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            show_all = 1;
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'n':
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            limit = atoi(optarg);
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case '?':
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "%s: invalid option -%c\n",
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argv[0], optopt);
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            exit(1);
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while (1);
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (optind + 2 != argc) {
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "Usage: %s [-b] [-l] [-n count] file1 file2\n", argv[0]);
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(1);
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd1 = open(argv[optind], O_RDONLY);
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd1 < 0) {
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd2 = open(argv[optind+1], O_RDONLY);
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd2 < 0) {
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "could not open %s, %s\n", argv[optind+1], strerror(errno));
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(1) {
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res1 = read(fd1, &buf1, sizeof(buf1));
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res2 = read(fd2, &buf2, sizeof(buf2));
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		res = res1 < res2 ? res1 : res2;
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if(res1 == 0 && res2 == 0) {
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			return rv;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		for(i = 0; i < res; i++) {
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			if(buf1[i] != buf2[i]) {
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				printf("%s %s differ byte %d", argv[optind], argv[optind+1], filepos + i);
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				if(show_byte)
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project					printf(" 0x%02x 0x%02x", buf1[i], buf2[i]);
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				printf("\n");
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				if(!show_all)
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project					return 1;
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				rv = 1;
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			}
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			if(limit) {
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				limit--;
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				if(limit == 0)
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project					return rv;
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			}
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if(res1 != res2 || res < 0) {
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			printf("%s on %s\n", res < 0 ? "Read error" : "EOF", res1 < res2 ? argv[optind] : argv[optind+1]);
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			return 1;
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		filepos += res;
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
92