VirtualBox

source: vbox/trunk/src/bldprogs/VBoxCmp.cpp

Last change on this file was 106061, checked in by vboxsync, 7 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1/* $Id: VBoxCmp.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * File Compare - Compares two files byte by byte.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <errno.h>
37
38#include <iprt/string.h>
39#include <iprt/stdarg.h>
40
41
42/**
43 * Writes an error message.
44 *
45 * @returns RTEXITCODE_FAILURE.
46 * @param pcszFormat Error message.
47 * @param ... Format argument referenced in the message.
48 */
49static RTEXITCODE printErr(const char *pcszFormat, ...)
50{
51 va_list va;
52
53 fprintf(stderr, "VBoxCmp: ");
54 va_start(va, pcszFormat);
55 vfprintf(stderr, pcszFormat, va);
56 va_end(va);
57
58 return RTEXITCODE_FAILURE;
59}
60
61
62static FILE *openFile(const char *pszFile)
63{
64#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
65 FILE *pFile = fopen(pszFile, "rb");
66#else
67 FILE *pFile = fopen(pszFile, "r");
68#endif
69 if (!pFile)
70 printErr("Failed to open '%s': %s\n", pszFile, strerror(errno));
71 return pFile;
72}
73
74
75static RTEXITCODE compareFiles(FILE *pFile1, FILE *pFile2)
76{
77 if (!pFile1 || !pFile2)
78 return RTEXITCODE_FAILURE;
79
80 uint32_t cMismatches = 1;
81 RTEXITCODE rcRet = RTEXITCODE_SUCCESS;
82 uint64_t off = 0;
83 for (;;)
84 {
85 uint8_t b1;
86 size_t cb1 = fread(&b1, sizeof(b1), 1, pFile1);
87 uint8_t b2;
88 size_t cb2 = fread(&b2, sizeof(b2), 1, pFile2);
89 if (cb1 != 1 || cb2 != 1)
90 break;
91 if (b1 != b2)
92 {
93 printErr("0x%x%08x: %#04x (%3d) != %#04x (%3d)\n", (uint32_t)(off >> 32), (uint32_t)off, b1, b1, b2, b2);
94 rcRet = RTEXITCODE_FAILURE;
95 cMismatches++;
96 if (cMismatches > 128)
97 {
98 printErr("Too many mismatches, giving up\n");
99 return rcRet;
100 }
101 }
102 off++;
103 }
104
105 if (!feof(pFile1) || !feof(pFile2))
106 {
107 if (!feof(pFile1) && ferror(pFile1))
108 rcRet = printErr("Read error on file #1.\n");
109 else if (!feof(pFile2) && ferror(pFile2))
110 rcRet = printErr("Read error on file #2.\n");
111 else if (!feof(pFile2))
112 rcRet = printErr("0x%x%08x: file #1 ends before file #2\n", (uint32_t)(off >> 32), (uint32_t)off);
113 else
114 rcRet = printErr("0x%x%08x: file #2 ends before file #1\n", (uint32_t)(off >> 32), (uint32_t)off);
115 }
116
117 return rcRet;
118}
119
120
121int main(int argc, char *argv[])
122{
123 RTEXITCODE rcExit;
124
125 if (argc == 3)
126 {
127 const char *pszFile1 = argv[1];
128 const char *pszFile2 = argv[2];
129 FILE *pFile1 = openFile(pszFile1);
130 FILE *pFile2 = openFile(pszFile2);
131 rcExit = compareFiles(pFile1, pFile2);
132 if (pFile1)
133 fclose(pFile1);
134 if (pFile2)
135 fclose(pFile2);
136 }
137 else
138 rcExit = printErr("Syntax error: usage: VBoxCmp <file1> <file2>\n");
139 return rcExit;
140}
141
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette