2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS comp utility
22 * FILE: base/applications/cmdutils/comp/comp.c
23 * PURPOSE: Compares the contents of two files
24 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
39 /* getline: read a line, return length */
40 INT
GetBuff(char *buff
, FILE *in
)
42 return fread(buff
, 1, STRBUF
, in
);
45 INT
FileSize(FILE * fd
)
48 if (fseek(fd
, 0, SEEK_END
) == 0 && (result
= ftell(fd
)) != -1)
50 /* Restoring file pointer */
56 /* Print program usage */
59 _tprintf(_T("\nCompares the contents of two files or sets of files.\n\n"
60 "COMP [/L] [/A] [data1] [data2]\n\n"
61 " data1 Specifies location and name of first file to compare.\n"
62 " data2 Specifies location and name of second file to compare.\n"
63 " /A Display differences in ASCII characters.\n"
64 " /L Display line numbers for differences.\n"));
68 int _tmain (int argc
, TCHAR
*argv
[])
79 TCHAR File1
[_MAX_PATH
+ 1], // File paths
81 BOOL bAscii
= FALSE
, // /A switch
82 bLineNos
= FALSE
; // /L switch
87 INT NumberOfOptions
= 0;
89 INT Status
= EXIT_SUCCESS
;
91 /* Parse command line for options */
92 for (i
= 1; i
< argc
; i
++)
94 if (argv
[i
][0] == '/')
113 _tprintf(_T("Invalid switch - /%c\n"), argv
[i
][1]);
120 if (argc
- NumberOfOptions
== 3)
122 _tcsncpy(File1
, argv
[1 + NumberOfOptions
], _MAX_PATH
);
123 _tcsncpy(File2
, argv
[2 + NumberOfOptions
], _MAX_PATH
);
127 _tprintf(_T("Bad command line syntax\n"));
131 Buff1
= (TCHAR
*)malloc(STRBUF
* sizeof(TCHAR
));
134 _tprintf(_T("Can't get free memory for Buff1\n"));
138 Buff2
= (TCHAR
*)malloc(STRBUF
* sizeof(TCHAR
));
141 _tprintf(_T("Can't get free memory for Buff2\n"));
142 Status
= EXIT_FAILURE
;
146 if ((fp1
= fopen(File1
, "rb")) == NULL
)
148 _tprintf(_T("Can't find/open file: %s\n"), File1
);
149 Status
= EXIT_FAILURE
;
152 if ((fp2
= fopen(File2
, "rb")) == NULL
)
154 _tprintf(_T("Can't find/open file: %s\n"), File2
);
155 Status
= EXIT_FAILURE
;
160 _tprintf(_T("Comparing %s and %s...\n"), File1
, File2
);
162 FileSizeFile1
= FileSize(fp1
);
163 if (FileSizeFile1
== -1)
165 _tprintf(_T("Can't determine size of file: %s\n"), File1
);
166 Status
= EXIT_FAILURE
;
170 FileSizeFile2
= FileSize(fp2
);
171 if (FileSizeFile2
== -1)
173 _tprintf(_T("Can't determine size of file: %s\n"), File2
);
174 Status
= EXIT_FAILURE
;
178 if (FileSizeFile1
!= FileSizeFile2
)
180 _tprintf(_T("Files are different sizes.\n"));
181 Status
= EXIT_FAILURE
;
189 BufLen1
= GetBuff(Buff1
, fp1
);
190 BufLen2
= GetBuff(Buff2
, fp2
);
192 if (ferror(fp1
) || ferror(fp2
))
194 _tprintf(_T("Files read error.\n"));
195 Status
= EXIT_FAILURE
;
199 if (!BufLen1
&& !BufLen2
)
202 assert(BufLen1
== BufLen2
);
203 for (i
= 0; i
< BufLen1
; i
++)
205 if (Buff1
[i
] != Buff2
[i
])
209 /* Reporting here a mismatch */
212 _tprintf(_T("Compare error at LINE %d\n"), LineNumber
);
216 _tprintf(_T("Compare error at OFFSET %d\n"), Offset
);
221 _tprintf(_T("file1 = %c\n"), Buff1
[i
]);
222 _tprintf(_T("file2 = %c\n"), Buff2
[i
]);
226 _tprintf(_T("file1 = %X\n"), Buff1
[i
]);
227 _tprintf(_T("file2 = %X\n"), Buff2
[i
]);
232 if (Buff1
[i
] == '\n')
239 _tprintf(_T("Files compare OK\n"));