8738df2d2c2688b4d615c185374fcdfac3fa3ecf
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 * 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)
29 // #include <string.h>
33 #define WIN32_NO_STATUS
42 VOID
PrintResourceString(INT resID
, ...)
44 WCHAR bufSrc
[RC_STRING_MAX_SIZE
];
45 WCHAR bufFormatted
[RC_STRING_MAX_SIZE
];
46 CHAR bufFormattedOem
[RC_STRING_MAX_SIZE
];
49 va_start(args
, resID
);
51 if (LoadStringW(GetModuleHandleW(NULL
), resID
, bufSrc
, ARRAYSIZE(bufSrc
)))
52 vswprintf(bufFormatted
, bufSrc
, args
);
54 swprintf(bufFormatted
, L
"Resource loading error!");
56 CharToOemW(bufFormatted
, bufFormattedOem
);
57 fputs(bufFormattedOem
, stdout
);
62 /* getline: read a line, return length */
63 INT
GetBuff(PBYTE buff
, FILE* in
)
65 return fread(buff
, sizeof(BYTE
), STRBUF
, in
);
68 INT
FileSize(FILE* fd
)
71 if (fseek(fd
, 0, SEEK_END
) == 0 && (result
= ftell(fd
)) != -1)
73 /* Restoring file pointer */
79 /* Print program usage */
82 PrintResourceString(IDS_HELP
);
86 int wmain (int argc
, WCHAR
* argv
[])
97 WCHAR File1
[_MAX_PATH
+ 1], // File paths
99 BOOL bAscii
= FALSE
, // /A switch
100 bLineNos
= FALSE
; // /L switch
105 INT NumberOfOptions
= 0;
107 INT Status
= EXIT_SUCCESS
;
109 /* Parse command line for options */
110 for (i
= 1; i
< argc
; i
++)
112 if (argv
[i
][0] == L
'/')
131 PrintResourceString(IDS_INVALIDSWITCH
, argv
[i
][1]);
138 if (argc
- NumberOfOptions
== 3)
140 wcsncpy(File1
, argv
[1 + NumberOfOptions
], _MAX_PATH
);
141 wcsncpy(File2
, argv
[2 + NumberOfOptions
], _MAX_PATH
);
145 PrintResourceString(IDS_BADSYNTAX
);
149 Buff1
= (PBYTE
)malloc(STRBUF
);
152 wprintf(L
"Can't get free memory for Buff1\n");
153 Status
= EXIT_FAILURE
;
157 Buff2
= (PBYTE
)malloc(STRBUF
);
160 wprintf(L
"Can't get free memory for Buff2\n");
161 Status
= EXIT_FAILURE
;
165 if ((fp1
= _wfopen(File1
, L
"rb")) == NULL
)
167 PrintResourceString(IDS_FILEERROR
, File1
);
168 Status
= EXIT_FAILURE
;
171 if ((fp2
= _wfopen(File2
, L
"rb")) == NULL
)
173 PrintResourceString(IDS_FILEERROR
, File2
);
174 Status
= EXIT_FAILURE
;
178 PrintResourceString(IDS_COMPARING
, File1
, File2
);
180 FileSizeFile1
= FileSize(fp1
);
181 if (FileSizeFile1
== -1)
183 PrintResourceString(IDS_FILESIZEERROR
, File1
);
184 Status
= EXIT_FAILURE
;
188 FileSizeFile2
= FileSize(fp2
);
189 if (FileSizeFile2
== -1)
191 PrintResourceString(IDS_FILESIZEERROR
, File2
);
192 Status
= EXIT_FAILURE
;
196 if (FileSizeFile1
!= FileSizeFile2
)
198 PrintResourceString(IDS_SIZEDIFFERS
);
199 Status
= EXIT_FAILURE
;
207 BufLen1
= GetBuff(Buff1
, fp1
);
208 BufLen2
= GetBuff(Buff2
, fp2
);
210 if (ferror(fp1
) || ferror(fp2
))
212 PrintResourceString(IDS_READERROR
);
213 Status
= EXIT_FAILURE
;
217 if (!BufLen1
&& !BufLen2
)
220 assert(BufLen1
== BufLen2
);
221 for (i
= 0; i
< BufLen1
; i
++)
223 if (Buff1
[i
] != Buff2
[i
])
227 /* Reporting here a mismatch */
230 PrintResourceString(IDS_MISMATCHLINE
, LineNumber
);
234 PrintResourceString(IDS_MISMATCHOFFSET
, Offset
);
239 PrintResourceString(IDS_ASCIIDIFF
, 1, Buff1
[i
]);
240 PrintResourceString(IDS_ASCIIDIFF
, 2, Buff2
[i
]);
244 PrintResourceString(IDS_HEXADECIMALDIFF
, 1, Buff1
[i
]);
245 PrintResourceString(IDS_HEXADECIMALDIFF
, 2, Buff2
[i
]);
251 if (Buff1
[i
] == '\n')
257 PrintResourceString(IDS_MATCH
);