132 lines
3.4 KiB
C
132 lines
3.4 KiB
C
/* cmd.c - command to cmp an operating system */
|
||
/*
|
||
* VAS_EBOOT -- GRand Unified Bootloader
|
||
* Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc.
|
||
*
|
||
* VAS_EBOOT is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* VAS_EBOOT is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License
|
||
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include <VasEBoot/dl.h>
|
||
#include <VasEBoot/misc.h>
|
||
#include <VasEBoot/file.h>
|
||
#include <VasEBoot/mm.h>
|
||
#include <VasEBoot/command.h>
|
||
#include <VasEBoot/extcmd.h>
|
||
#include <VasEBoot/i18n.h>
|
||
|
||
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
||
|
||
#define BUFFER_SIZE 512
|
||
|
||
static const struct VasEBoot_arg_option options[] =
|
||
{
|
||
{0, 'v', 0, N_("Enable verbose output"), 0, 0},
|
||
{0, 0, 0, 0, 0, 0}
|
||
};
|
||
|
||
static VasEBoot_err_t
|
||
VasEBoot_cmd_cmp (VasEBoot_extcmd_context_t ctxt,
|
||
int argc, char **args)
|
||
{
|
||
VasEBoot_ssize_t rd1, rd2;
|
||
VasEBoot_off_t pos;
|
||
VasEBoot_file_t file1 = 0;
|
||
VasEBoot_file_t file2 = 0;
|
||
char *buf1 = 0;
|
||
char *buf2 = 0;
|
||
VasEBoot_err_t err = VAS_EBOOT_ERR_TEST_FAILURE;
|
||
|
||
if (argc != 2)
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("two arguments expected"));
|
||
|
||
if (ctxt->state[0].set)
|
||
VasEBoot_printf_ (N_("Compare file `%s' with `%s':\n"), args[0], args[1]);
|
||
|
||
file1 = VasEBoot_file_open (args[0], VAS_EBOOT_FILE_TYPE_CMP);
|
||
file2 = VasEBoot_file_open (args[1], VAS_EBOOT_FILE_TYPE_CMP);
|
||
if (! file1 || ! file2)
|
||
goto cleanup;
|
||
|
||
if (ctxt->state[0].set && (VasEBoot_file_size (file1) != VasEBoot_file_size (file2)))
|
||
VasEBoot_printf_ (N_("Files differ in size: %llu [%s], %llu [%s]\n"),
|
||
(unsigned long long) VasEBoot_file_size (file1), args[0],
|
||
(unsigned long long) VasEBoot_file_size (file2), args[1]);
|
||
else
|
||
{
|
||
pos = 0;
|
||
|
||
buf1 = VasEBoot_malloc (BUFFER_SIZE);
|
||
buf2 = VasEBoot_malloc (BUFFER_SIZE);
|
||
|
||
if (! buf1 || ! buf2)
|
||
goto cleanup;
|
||
|
||
do
|
||
{
|
||
int i;
|
||
|
||
rd1 = VasEBoot_file_read (file1, buf1, BUFFER_SIZE);
|
||
rd2 = VasEBoot_file_read (file2, buf2, BUFFER_SIZE);
|
||
|
||
if (rd1 != rd2)
|
||
goto cleanup;
|
||
|
||
for (i = 0; i < rd2; i++)
|
||
{
|
||
if (buf1[i] != buf2[i])
|
||
{
|
||
if (ctxt->state[0].set)
|
||
VasEBoot_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"),
|
||
(unsigned long long) (i + pos), buf1[i],
|
||
args[0], buf2[i], args[1]);
|
||
goto cleanup;
|
||
}
|
||
}
|
||
pos += BUFFER_SIZE;
|
||
|
||
}
|
||
while (rd2);
|
||
|
||
/* TRANSLATORS: it's always exactly 2 files. */
|
||
if (ctxt->state[0].set)
|
||
VasEBoot_printf_ (N_("The files are identical.\n"));
|
||
err = VAS_EBOOT_ERR_NONE;
|
||
}
|
||
|
||
cleanup:
|
||
|
||
VasEBoot_free (buf1);
|
||
VasEBoot_free (buf2);
|
||
if (file1)
|
||
VasEBoot_file_close (file1);
|
||
if (file2)
|
||
VasEBoot_file_close (file2);
|
||
|
||
return err;
|
||
}
|
||
|
||
static VasEBoot_extcmd_t cmd;
|
||
|
||
VAS_EBOOT_MOD_INIT(cmp)
|
||
{
|
||
cmd = VasEBoot_register_extcmd ("cmp", VasEBoot_cmd_cmp, 0,
|
||
N_("FILE1 FILE2"), N_("Compare two files."),
|
||
options);
|
||
}
|
||
|
||
VAS_EBOOT_MOD_FINI(cmp)
|
||
{
|
||
VasEBoot_unregister_extcmd (cmd);
|
||
}
|