ReactOS 0.4.16-dev-2613-g9533ad7
comp.c
Go to the documentation of this file.
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 <assert.h>
30
31#include <windef.h>
32#include <winbase.h>
33
34#include <conutils.h>
35
36#include "resource.h"
37
38#define STRBUF 1024
39
40/* getline: read a line, return length */
42{
43 return fread(buff, sizeof(BYTE), STRBUF, in);
44}
45
47{
48 INT result = -1;
49 if (fseek(fd, 0, SEEK_END) == 0 && (result = ftell(fd)) != -1)
50 {
51 /* Restoring file pointer */
52 rewind(fd);
53 }
54 return result;
55}
56
57
58int wmain(int argc, WCHAR* argv[])
59{
60 /* File pointers */
61 FILE *fp1 = NULL, *fp2 = NULL;
62
63 size_t i;
64 size_t BufLen1, BufLen2;
65 PBYTE Buff1 = NULL, Buff2 = NULL;
66 WCHAR File1[_MAX_PATH + 1], // File paths
67 File2[_MAX_PATH + 1];
68 BOOL bAscii = FALSE, // /A switch
69 bLineNos = FALSE; // /L switch
71 INT FileSizeFile1, FileSizeFile2;
72 INT NumberOfOptions = 0;
73 INT FilesOK = 1;
75
76 /* Initialize the Console Standard Streams */
78
79 /* Parse command line for options */
80 for (i = 1; i < argc; i++)
81 {
82 if (argv[i][0] == L'/')
83 {
84 switch (towlower(argv[i][1]))
85 {
86 case L'a':
87 bAscii = TRUE;
88 NumberOfOptions++;
89 break;
90
91 case L'l':
92 bLineNos = TRUE;
93 NumberOfOptions++;
94 break;
95
96 case L'?':
98 return EXIT_SUCCESS;
99
100 default:
103 return EXIT_FAILURE;
104 }
105 }
106 }
107
108 if (argc - NumberOfOptions == 3)
109 {
110 wcsncpy(File1, argv[1 + NumberOfOptions], _MAX_PATH);
111 wcsncpy(File2, argv[2 + NumberOfOptions], _MAX_PATH);
112 }
113 else
114 {
116 return EXIT_FAILURE;
117 }
118
119 Buff1 = (PBYTE)malloc(STRBUF);
120 if (Buff1 == NULL)
121 {
122 ConPuts(StdErr, L"Can't get free memory for Buff1\n");
124 goto Cleanup;
125 }
126
127 Buff2 = (PBYTE)malloc(STRBUF);
128 if (Buff2 == NULL)
129 {
130 ConPuts(StdErr, L"Can't get free memory for Buff2\n");
132 goto Cleanup;
133 }
134
135 if ((fp1 = _wfopen(File1, L"rb")) == NULL)
136 {
139 goto Cleanup;
140 }
141 if ((fp2 = _wfopen(File2, L"rb")) == NULL)
142 {
145 goto Cleanup;
146 }
147
148 ConResPrintf(StdOut, IDS_COMPARING, File1, File2);
149
150 FileSizeFile1 = FileSize(fp1);
151 if (FileSizeFile1 == -1)
152 {
155 goto Cleanup;
156 }
157
158 FileSizeFile2 = FileSize(fp2);
159 if (FileSizeFile2 == -1)
160 {
163 goto Cleanup;
164 }
165
166 if (FileSizeFile1 != FileSizeFile2)
167 {
170 goto Cleanup;
171 }
172
173 LineNumber = 1;
174 Offset = 0;
175 while (1)
176 {
177 BufLen1 = GetBuff(Buff1, fp1);
178 BufLen2 = GetBuff(Buff2, fp2);
179
180 if (ferror(fp1) || ferror(fp2))
181 {
184 goto Cleanup;
185 }
186
187 if (!BufLen1 && !BufLen2)
188 break;
189
190 assert(BufLen1 == BufLen2);
191 for (i = 0; i < BufLen1; i++)
192 {
193 if (Buff1[i] != Buff2[i])
194 {
195 FilesOK = 0;
196
197 /* Reporting here a mismatch */
198 if (bLineNos)
200 else
202
203 if (bAscii)
204 {
205 ConResPrintf(StdOut, IDS_ASCIIDIFF, 1, Buff1[i]);
206 ConResPrintf(StdOut, IDS_ASCIIDIFF, 2, Buff2[i]);
207 }
208 else
209 {
212 }
213 }
214
215 Offset++;
216
217 if (Buff1[i] == '\n')
218 LineNumber++;
219 }
220 }
221
222 if (FilesOK)
224
225Cleanup:
226 if (fp2)
227 fclose(fp2);
228 if (fp1)
229 fclose(fp1);
230
231 if (Buff2)
232 free(Buff2);
233 if (Buff1)
234 free(Buff1);
235
236 return Status;
237}
238
239/* EOF */
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 LineNumber
Definition: acpixf.h:1220
#define IDS_HELP
Definition: resource.h:3
#define IDS_FILEERROR
Definition: resource.h:6
#define IDS_READERROR
Definition: resource.h:10
#define IDS_SIZEDIFFERS
Definition: resource.h:9
#define IDS_ASCIIDIFF
Definition: resource.h:13
#define IDS_FILESIZEERROR
Definition: resource.h:8
#define IDS_HEXADECIMALDIFF
Definition: resource.h:14
#define IDS_INVALIDSWITCH
Definition: resource.h:4
#define IDS_MISMATCHOFFSET
Definition: resource.h:12
#define IDS_COMPARING
Definition: resource.h:7
#define IDS_MISMATCHLINE
Definition: resource.h:11
#define IDS_BADSYNTAX
Definition: resource.h:5
#define IDS_MATCH
Definition: resource.h:15
#define SEEK_END
Definition: cabinet.c:29
size_t GetBuff(PBYTE buff, FILE *in)
Definition: comp.c:41
#define STRBUF
Definition: comp.c:38
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: conutils_noros.h:8
#define ConInitStdStreams()
Definition: conutils_noros.h:5
#define StdOut
Definition: conutils_noros.h:6
void ConResPrintf(FILE *fp, UINT nID,...)
#define StdErr
Definition: conutils_noros.h:7
void ConResPuts(FILE *fp, UINT nID)
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
MonoAssembly int argc
Definition: metahost.c:107
int CDECL fclose(FILE *file)
Definition: file.c:3757
size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE *file)
Definition: file.c:4406
FILE *CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
Definition: file.c:4335
void CDECL rewind(FILE *file)
Definition: file.c:1712
int CDECL ferror(FILE *file)
Definition: file.c:3811
#define assert(_expr)
Definition: assert.h:32
#define _MAX_PATH
Definition: stdlib.h:41
static const WCHAR Cleanup[]
Definition: register.c:80
#define L(x)
Definition: resources.c:13
static unsigned char buff[32768]
Definition: fatten.c:17
unsigned int BOOL
Definition: ntddk_ex.h:94
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
Status
Definition: gdiplustypes.h:25
GLuint in
Definition: glext.h:9616
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define EXIT_FAILURE
Definition: jerror.c:33
#define argv
Definition: mplay32.c:18
unsigned int UINT
Definition: ndis.h:50
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
BYTE * PBYTE
Definition: pedump.c:66
short WCHAR
Definition: pedump.c:58
int wmain()
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
wcsncpy
static int fd
Definition: io.c:51
#define towlower(c)
Definition: wctype.h:97
#define fseek(stream, offset, whence)
Definition: tiffiop.h:354
#define ftell(stream, offset, whence)
Definition: tiffiop.h:355
int32_t INT
Definition: typedefs.h:58
unsigned char BYTE
Definition: xxhash.c:193