ReactOS  0.4.12-dev-18-gf469aca
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 #define WIN32_NO_STATUS
32 #include <windef.h>
33 #include <winbase.h>
34 
35 #include <conutils.h>
36 
37 #include "resource.h"
38 
39 #define STRBUF 1024
40 
41 /* getline: read a line, return length */
43 {
44  return fread(buff, sizeof(BYTE), STRBUF, in);
45 }
46 
48 {
49  INT result = -1;
50  if (fseek(fd, 0, SEEK_END) == 0 && (result = ftell(fd)) != -1)
51  {
52  /* Restoring file pointer */
53  rewind(fd);
54  }
55  return result;
56 }
57 
58 
59 int wmain(int argc, WCHAR* argv[])
60 {
61  INT i;
62 
63  /* File pointers */
64  FILE *fp1 = NULL;
65  FILE *fp2 = NULL;
66 
67  INT BufLen1, BufLen2;
68  PBYTE Buff1 = NULL;
69  PBYTE Buff2 = NULL;
70  WCHAR File1[_MAX_PATH + 1], // File paths
71  File2[_MAX_PATH + 1];
72  BOOL bAscii = FALSE, // /A switch
73  bLineNos = FALSE; // /L switch
75  UINT Offset;
76  INT FileSizeFile1;
77  INT FileSizeFile2;
78  INT NumberOfOptions = 0;
79  INT FilesOK = 1;
81 
82  /* Initialize the Console Standard Streams */
84 
85  /* Parse command line for options */
86  for (i = 1; i < argc; i++)
87  {
88  if (argv[i][0] == L'/')
89  {
90  switch (towlower(argv[i][1]))
91  {
92  case L'a':
93  bAscii = TRUE;
94  NumberOfOptions++;
95  break;
96 
97  case L'l':
98  bLineNos = TRUE;
99  NumberOfOptions++;
100  break;
101 
102  case L'?':
104  return EXIT_SUCCESS;
105 
106  default:
107  ConResPrintf(StdErr, IDS_INVALIDSWITCH, argv[i][1]);
109  return EXIT_FAILURE;
110  }
111  }
112  }
113 
114  if (argc - NumberOfOptions == 3)
115  {
116  wcsncpy(File1, argv[1 + NumberOfOptions], _MAX_PATH);
117  wcsncpy(File2, argv[2 + NumberOfOptions], _MAX_PATH);
118  }
119  else
120  {
122  return EXIT_FAILURE;
123  }
124 
125  Buff1 = (PBYTE)malloc(STRBUF);
126  if (Buff1 == NULL)
127  {
128  ConPuts(StdErr, L"Can't get free memory for Buff1\n");
129  Status = EXIT_FAILURE;
130  goto Cleanup;
131  }
132 
133  Buff2 = (PBYTE)malloc(STRBUF);
134  if (Buff2 == NULL)
135  {
136  ConPuts(StdErr, L"Can't get free memory for Buff2\n");
137  Status = EXIT_FAILURE;
138  goto Cleanup;
139  }
140 
141  if ((fp1 = _wfopen(File1, L"rb")) == NULL)
142  {
144  Status = EXIT_FAILURE;
145  goto Cleanup;
146  }
147  if ((fp2 = _wfopen(File2, L"rb")) == NULL)
148  {
150  Status = EXIT_FAILURE;
151  goto Cleanup;
152  }
153 
154  ConResPrintf(StdOut, IDS_COMPARING, File1, File2);
155 
156  FileSizeFile1 = FileSize(fp1);
157  if (FileSizeFile1 == -1)
158  {
160  Status = EXIT_FAILURE;
161  goto Cleanup;
162  }
163 
164  FileSizeFile2 = FileSize(fp2);
165  if (FileSizeFile2 == -1)
166  {
168  Status = EXIT_FAILURE;
169  goto Cleanup;
170  }
171 
172  if (FileSizeFile1 != FileSizeFile2)
173  {
175  Status = EXIT_FAILURE;
176  goto Cleanup;
177  }
178 
179  LineNumber = 1;
180  Offset = 0;
181  while (1)
182  {
183  BufLen1 = GetBuff(Buff1, fp1);
184  BufLen2 = GetBuff(Buff2, fp2);
185 
186  if (ferror(fp1) || ferror(fp2))
187  {
189  Status = EXIT_FAILURE;
190  goto Cleanup;
191  }
192 
193  if (!BufLen1 && !BufLen2)
194  break;
195 
196  assert(BufLen1 == BufLen2);
197  for (i = 0; i < BufLen1; i++)
198  {
199  if (Buff1[i] != Buff2[i])
200  {
201  FilesOK = 0;
202 
203  /* Reporting here a mismatch */
204  if (bLineNos)
205  ConResPrintf(StdOut, IDS_MISMATCHLINE, LineNumber);
206  else
208 
209  if (bAscii)
210  {
211  ConResPrintf(StdOut, IDS_ASCIIDIFF, 1, Buff1[i]);
212  ConResPrintf(StdOut, IDS_ASCIIDIFF, 2, Buff2[i]);
213  }
214  else
215  {
216  ConResPrintf(StdOut, IDS_HEXADECIMALDIFF, 1, Buff1[i]);
217  ConResPrintf(StdOut, IDS_HEXADECIMALDIFF, 2, Buff2[i]);
218  }
219  }
220 
221  Offset++;
222 
223  if (Buff1[i] == '\n')
224  LineNumber++;
225  }
226  }
227 
228  if (FilesOK)
230 
231 Cleanup:
232  if (fp2)
233  fclose(fp2);
234  if (fp1)
235  fclose(fp1);
236 
237  if (Buff2)
238  free(Buff2);
239  if (Buff1)
240  free(Buff1);
241 
242  return Status;
243 }
244 
245 /* EOF */
static int argc
Definition: ServiceArgs.c:12
_Check_return_ _CRTIMP int __cdecl ferror(_In_ FILE *_File)
#define TRUE
Definition: types.h:120
#define IDS_READERROR
Definition: resource.h:10
_Check_return_ _CRTIMP FILE *__cdecl _wfopen(_In_z_ const wchar_t *_Filename, _In_z_ const wchar_t *_Mode)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define IDS_MATCH
Definition: resource.h:15
#define free
Definition: debug_ros.c:5
#define assert(x)
Definition: debug.h:53
static int fd
Definition: io.c:51
int32_t INT
Definition: typedefs.h:56
#define IDS_FILEERROR
Definition: resource.h:6
static char ** argv
Definition: ServiceArgs.c:11
INT FileSize(FILE *fd)
Definition: comp.c:47
#define IDS_HEXADECIMALDIFF
Definition: resource.h:14
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
GLenum GLclampf GLint i
Definition: glfuncs.h:14
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
#define ConInitStdStreams()
Definition: stream.h:122
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
#define _MAX_PATH
Definition: utility.h:77
smooth NULL
Definition: ftsmooth.c:416
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
#define IDS_BADSYNTAX
Definition: resource.h:5
_CRTIMP void __cdecl rewind(_Inout_ FILE *_File)
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
int wmain(int argc, WCHAR *argv[])
Definition: comp.c:59
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IDS_MISMATCHOFFSET
Definition: resource.h:12
unsigned int UINT
Definition: ndis.h:50
#define EXIT_FAILURE
Definition: jerror.c:33
#define StdErr
Definition: stream.h:77
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1087
#define IDS_MISMATCHLINE
Definition: resource.h:11
#define IDS_FILESIZEERROR
Definition: resource.h:8
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
static const WCHAR Cleanup[]
Definition: register.c:80
unsigned char BYTE
Definition: ntddk_ex.h:96
GLfloat CONST GLvector4f * in
Definition: m_xform.h:122
Status
Definition: gdiplustypes.h:24
#define IDS_COMPARING
Definition: resource.h:7
_CRTIMP wchar_t *__cdecl wcsncpy(wchar_t *_Dest, const wchar_t *_Source, size_t _Count)
#define IDS_ASCIIDIFF
Definition: resource.h:13
#define STRBUF
Definition: comp.c:39
#define IDS_HELP
Definition: resource.h:3
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:1209
INT GetBuff(PBYTE buff, FILE *in)
Definition: comp.c:42
INT ConPuts(IN PCON_STREAM Stream, IN LPWSTR szStr)
Definition: outstream.c:427
#define StdOut
Definition: stream.h:76
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
#define towlower(c)
Definition: wctype.h:97
#define SEEK_END
Definition: cabinet.c:27
#define IDS_INVALIDSWITCH
Definition: resource.h:4
#define malloc
Definition: debug_ros.c:4
#define IDS_SIZEDIFFERS
Definition: resource.h:9
GLuint64EXT * result
Definition: glext.h:11304
BYTE * PBYTE
Definition: pedump.c:66
static unsigned char buff[32768]
Definition: fatten.c:17