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