Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini cmp implementation for busybox |
| 4 | * |
| 5 | * |
| 6 | * Copyright (C) 2000 by Lineo, inc. |
| 7 | * Written by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "busybox.h" |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | int cmp_main(int argc, char **argv) |
| 31 | { |
| 32 | FILE *fp1 = NULL, *fp2 = stdin; |
| 33 | char *filename1 = argv[1], *filename2 = "-"; |
| 34 | int c1, c2, char_pos = 1, line_pos = 1; |
| 35 | |
| 36 | /* parse argv[] */ |
| 37 | if (argc < 2 || 3 < argc) |
| 38 | usage(cmp_usage); |
| 39 | |
| 40 | fp1 = xfopen(argv[1], "r"); |
| 41 | if (argv[2] != NULL) { |
| 42 | fp2 = xfopen(argv[2], "r"); |
| 43 | filename2 = argv[2]; |
| 44 | } |
| 45 | |
| 46 | do { |
| 47 | c1 = fgetc(fp1); |
| 48 | c2 = fgetc(fp2); |
| 49 | if (c1 != c2) { |
| 50 | if (c1 == EOF) |
| 51 | printf("EOF on %s\n", filename1); |
| 52 | else if (c2 == EOF) |
| 53 | printf("EOF on %s\n", filename2); |
| 54 | else |
| 55 | printf("%s %s differ: char %d, line %d\n", filename1, filename2, |
| 56 | char_pos, line_pos); |
| 57 | return 1; |
| 58 | } |
| 59 | char_pos++; |
| 60 | if (c1 == '\n') |
| 61 | line_pos++; |
| 62 | } while (c1 != EOF); |
| 63 | |
| 64 | return EXIT_SUCCESS; |
| 65 | } |