ReactOS  0.4.12-dev-18-gf469aca
comp.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <windef.h>
#include <winbase.h>
#include <conutils.h>
#include "resource.h"
Include dependency graph for comp.c:

Go to the source code of this file.

Macros

#define WIN32_NO_STATUS
 
#define STRBUF   1024
 

Functions

INT GetBuff (PBYTE buff, FILE *in)
 
INT FileSize (FILE *fd)
 
int wmain (int argc, WCHAR *argv[])
 

Macro Definition Documentation

◆ STRBUF

#define STRBUF   1024

Definition at line 39 of file comp.c.

Referenced by GetBuff(), and wmain().

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 31 of file comp.c.

Function Documentation

◆ FileSize()

INT FileSize ( FILE fd)

Definition at line 47 of file comp.c.

Referenced by wmain().

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 }
int32_t INT
Definition: typedefs.h:56
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
_CRTIMP void __cdecl rewind(_Inout_ FILE *_File)
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
#define SEEK_END
Definition: cabinet.c:27
GLuint64EXT * result
Definition: glext.h:11304

◆ GetBuff()

INT GetBuff ( PBYTE  buff,
FILE in 
)

Definition at line 42 of file comp.c.

Referenced by wmain().

43 {
44  return fread(buff, sizeof(BYTE), STRBUF, in);
45 }
_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)
unsigned char BYTE
Definition: ntddk_ex.h:96
#define STRBUF
Definition: comp.c:39
static unsigned char buff[32768]
Definition: fatten.c:17

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

Definition at line 59 of file comp.c.

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:
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 }
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
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
#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
#define IDS_BADSYNTAX
Definition: resource.h:5
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
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
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
#define towlower(c)
Definition: wctype.h:97
#define IDS_INVALIDSWITCH
Definition: resource.h:4
#define malloc
Definition: debug_ros.c:4
#define IDS_SIZEDIFFERS
Definition: resource.h:9
BYTE * PBYTE
Definition: pedump.c:66