vaseboot/VasEBoot-core/commands/cmp.c

120 lines
3.0 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* cmd.c - command to cmp an operating system */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc.
*
* VasEBoot 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.
*
* VasEBoot 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 VasEBoot. 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/i18n.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
#define BUFFER_SIZE 512
static VasEBoot_err_t
VasEBoot_cmd_cmp (VasEBoot_command_t cmd __attribute__ ((unused)),
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;
if (argc != 2)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("two arguments expected"));
VasEBoot_printf_ (N_("Compare file `%s' with `%s':\n"), args[0],
args[1]);
file1 = VasEBoot_file_open (args[0]);
file2 = VasEBoot_file_open (args[1]);
if (! file1 || ! file2)
goto cleanup;
if (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])
{
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. */
VasEBoot_printf_ (N_("The files are identical.\n"));
}
cleanup:
VasEBoot_free (buf1);
VasEBoot_free (buf2);
if (file1)
VasEBoot_file_close (file1);
if (file2)
VasEBoot_file_close (file2);
return VasEBoot_errno;
}
static VasEBoot_command_t cmd;
VasEBoot_MOD_INIT(cmp)
{
cmd = VasEBoot_register_command ("cmp", VasEBoot_cmd_cmp,
N_("FILE1 FILE2"), N_("Compare two files."));
}
VasEBoot_MOD_FINI(cmp)
{
VasEBoot_unregister_command (cmd);
}