8738df2d2c2688b4d615c185374fcdfac3fa3ecf
[reactos.git] / reactos / base / applications / cmdutils / comp / comp.c
1 /*
2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /*
20 * PROJECT: ReactOS Comp utility
21 * COPYRIGHT: See COPYING in the top level directory
22 * FILE: base/applications/cmdutils/comp/comp.c
23 * PURPOSE: Compares the contents of two files
24 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 // #include <string.h>
30 // #include <wchar.h>
31 #include <assert.h>
32
33 #define WIN32_NO_STATUS
34 #include <windef.h>
35 #include <winbase.h>
36 #include <winuser.h>
37
38 #include "resource.h"
39
40 #define STRBUF 1024
41
42 VOID PrintResourceString(INT resID, ...)
43 {
44 WCHAR bufSrc[RC_STRING_MAX_SIZE];
45 WCHAR bufFormatted[RC_STRING_MAX_SIZE];
46 CHAR bufFormattedOem[RC_STRING_MAX_SIZE];
47
48 va_list args;
49 va_start(args, resID);
50
51 if (LoadStringW(GetModuleHandleW(NULL), resID, bufSrc, ARRAYSIZE(bufSrc)))
52 vswprintf(bufFormatted, bufSrc, args);
53 else
54 swprintf(bufFormatted, L"Resource loading error!");
55
56 CharToOemW(bufFormatted, bufFormattedOem);
57 fputs(bufFormattedOem, stdout);
58
59 va_end(args);
60 }
61
62 /* getline: read a line, return length */
63 INT GetBuff(PBYTE buff, FILE* in)
64 {
65 return fread(buff, sizeof(BYTE), STRBUF, in);
66 }
67
68 INT FileSize(FILE* fd)
69 {
70 INT result = -1;
71 if (fseek(fd, 0, SEEK_END) == 0 && (result = ftell(fd)) != -1)
72 {
73 /* Restoring file pointer */
74 rewind(fd);
75 }
76 return result;
77 }
78
79 /* Print program usage */
80 VOID Usage(VOID)
81 {
82 PrintResourceString(IDS_HELP);
83 }
84
85
86 int wmain (int argc, WCHAR* argv[])
87 {
88 INT i;
89
90 /* File pointers */
91 FILE *fp1 = NULL;
92 FILE *fp2 = NULL;
93
94 INT BufLen1, BufLen2;
95 PBYTE Buff1 = NULL;
96 PBYTE Buff2 = NULL;
97 WCHAR File1[_MAX_PATH + 1], // File paths
98 File2[_MAX_PATH + 1];
99 BOOL bAscii = FALSE, // /A switch
100 bLineNos = FALSE; // /L switch
101 UINT LineNumber;
102 UINT Offset;
103 INT FileSizeFile1;
104 INT FileSizeFile2;
105 INT NumberOfOptions = 0;
106 INT FilesOK = 1;
107 INT Status = EXIT_SUCCESS;
108
109 /* Parse command line for options */
110 for (i = 1; i < argc; i++)
111 {
112 if (argv[i][0] == L'/')
113 {
114 switch (argv[i][1])
115 {
116 case L'A':
117 bAscii = TRUE;
118 NumberOfOptions++;
119 break;
120
121 case L'L':
122 bLineNos = TRUE;
123 NumberOfOptions++;
124 break;
125
126 case L'?':
127 Usage();
128 return EXIT_SUCCESS;
129
130 default:
131 PrintResourceString(IDS_INVALIDSWITCH, argv[i][1]);
132 Usage();
133 return EXIT_FAILURE;
134 }
135 }
136 }
137
138 if (argc - NumberOfOptions == 3)
139 {
140 wcsncpy(File1, argv[1 + NumberOfOptions], _MAX_PATH);
141 wcsncpy(File2, argv[2 + NumberOfOptions], _MAX_PATH);
142 }
143 else
144 {
145 PrintResourceString(IDS_BADSYNTAX);
146 return EXIT_FAILURE;
147 }
148
149 Buff1 = (PBYTE)malloc(STRBUF);
150 if (Buff1 == NULL)
151 {
152 wprintf(L"Can't get free memory for Buff1\n");
153 Status = EXIT_FAILURE;
154 goto Cleanup;
155 }
156
157 Buff2 = (PBYTE)malloc(STRBUF);
158 if (Buff2 == NULL)
159 {
160 wprintf(L"Can't get free memory for Buff2\n");
161 Status = EXIT_FAILURE;
162 goto Cleanup;
163 }
164
165 if ((fp1 = _wfopen(File1, L"rb")) == NULL)
166 {
167 PrintResourceString(IDS_FILEERROR, File1);
168 Status = EXIT_FAILURE;
169 goto Cleanup;
170 }
171 if ((fp2 = _wfopen(File2, L"rb")) == NULL)
172 {
173 PrintResourceString(IDS_FILEERROR, File2);
174 Status = EXIT_FAILURE;
175 goto Cleanup;
176 }
177
178 PrintResourceString(IDS_COMPARING, File1, File2);
179
180 FileSizeFile1 = FileSize(fp1);
181 if (FileSizeFile1 == -1)
182 {
183 PrintResourceString(IDS_FILESIZEERROR, File1);
184 Status = EXIT_FAILURE;
185 goto Cleanup;
186 }
187
188 FileSizeFile2 = FileSize(fp2);
189 if (FileSizeFile2 == -1)
190 {
191 PrintResourceString(IDS_FILESIZEERROR, File2);
192 Status = EXIT_FAILURE;
193 goto Cleanup;
194 }
195
196 if (FileSizeFile1 != FileSizeFile2)
197 {
198 PrintResourceString(IDS_SIZEDIFFERS);
199 Status = EXIT_FAILURE;
200 goto Cleanup;
201 }
202
203 LineNumber = 1;
204 Offset = 0;
205 while (1)
206 {
207 BufLen1 = GetBuff(Buff1, fp1);
208 BufLen2 = GetBuff(Buff2, fp2);
209
210 if (ferror(fp1) || ferror(fp2))
211 {
212 PrintResourceString(IDS_READERROR);
213 Status = EXIT_FAILURE;
214 goto Cleanup;
215 }
216
217 if (!BufLen1 && !BufLen2)
218 break;
219
220 assert(BufLen1 == BufLen2);
221 for (i = 0; i < BufLen1; i++)
222 {
223 if (Buff1[i] != Buff2[i])
224 {
225 FilesOK = 0;
226
227 /* Reporting here a mismatch */
228 if (bLineNos)
229 {
230 PrintResourceString(IDS_MISMATCHLINE, LineNumber);
231 }
232 else
233 {
234 PrintResourceString(IDS_MISMATCHOFFSET, Offset);
235 }
236
237 if (bAscii)
238 {
239 PrintResourceString(IDS_ASCIIDIFF, 1, Buff1[i]);
240 PrintResourceString(IDS_ASCIIDIFF, 2, Buff2[i]);
241 }
242 else
243 {
244 PrintResourceString(IDS_HEXADECIMALDIFF, 1, Buff1[i]);
245 PrintResourceString(IDS_HEXADECIMALDIFF, 2, Buff2[i]);
246 }
247 }
248
249 Offset++;
250
251 if (Buff1[i] == '\n')
252 LineNumber++;
253 }
254 }
255
256 if (FilesOK)
257 PrintResourceString(IDS_MATCH);
258
259 Cleanup:
260 if (fp2)
261 fclose(fp2);
262 if (fp1)
263 fclose(fp1);
264
265 if (Buff2)
266 free(Buff2);
267 if (Buff1)
268 free(Buff1);
269
270 return Status;
271 }
272
273 /* EOF */