]> git.proxmox.com Git - grub2.git/blame - commands/cmp.c
merge with mainline
[grub2.git] / commands / cmp.c
CommitLineData
db1771cf 1/* cmd.c - command to cmp an operating system */
2/*
4b13b216 3 * GRUB -- GRand Unified Bootloader
58bc8bd5 4 * Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc.
db1771cf 5 *
5a79f472 6 * GRUB is free software: you can redistribute it and/or modify
db1771cf 7 * it under the terms of the GNU General Public License as published by
5a79f472 8 * the Free Software Foundation, either version 3 of the License, or
db1771cf 9 * (at your option) any later version.
10 *
5a79f472 11 * GRUB is distributed in the hope that it will be useful,
db1771cf 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
5a79f472 17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
db1771cf 18 */
19
4b13b216 20#include <grub/dl.h>
4b13b216 21#include <grub/misc.h>
22#include <grub/file.h>
68c864eb 23#include <grub/mm.h>
d9864ee1 24#include <grub/gzio.h>
b1b797cb 25#include <grub/command.h>
77a79592 26#include <grub/i18n.h>
68c864eb 27
28#define BUFFER_SIZE 512
db1771cf 29
4b13b216 30static grub_err_t
b1b797cb 31grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
db1771cf 32 int argc, char **args)
33{
68c864eb 34 grub_ssize_t rd1, rd2;
524a1e6a 35 grub_off_t pos;
68c864eb 36 grub_file_t file1 = 0;
37 grub_file_t file2 = 0;
38 char *buf1 = 0;
39 char *buf2 = 0;
db1771cf 40
41 if (argc != 2)
4b13b216 42 return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
db1771cf 43
941903f2 44 grub_printf ("Compare file `%s' with `%s':\n", args[0],
db1771cf 45 args[1]);
46
524a1e6a 47 file1 = grub_gzfile_open (args[0], 1);
48 file2 = grub_gzfile_open (args[1], 1);
49 if (! file1 || ! file2)
68c864eb 50 goto cleanup;
db1771cf 51
4b13b216 52 if (grub_file_size (file1) != grub_file_size (file2))
941903f2 53 grub_printf ("Files differ in size: %llu [%s], %llu [%s]\n",
e4e8eaa5 54 (unsigned long long) grub_file_size (file1), args[0],
55 (unsigned long long) grub_file_size (file2), args[1]);
db1771cf 56 else
57 {
68c864eb 58 pos = 0;
59
524a1e6a 60 buf1 = grub_malloc (BUFFER_SIZE);
61 buf2 = grub_malloc (BUFFER_SIZE);
b39f9d20 62
524a1e6a 63 if (! buf1 || ! buf2)
68c864eb 64 goto cleanup;
b39f9d20 65
db1771cf 66 do
67 {
68 int i;
b39f9d20 69
68c864eb 70 rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
71 rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
db1771cf 72
73 if (rd1 != rd2)
68c864eb 74 goto cleanup;
db1771cf 75
68c864eb 76 for (i = 0; i < rd2; i++)
db1771cf 77 {
78 if (buf1[i] != buf2[i])
79 {
941903f2 80 grub_printf ("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n",
e4e8eaa5 81 (unsigned long long) (i + pos), buf1[i], args[0],
db1771cf 82 buf2[i], args[1]);
68c864eb 83 goto cleanup;
db1771cf 84 }
85 }
68c864eb 86 pos += BUFFER_SIZE;
b39f9d20 87
524a1e6a 88 }
89 while (rd2);
b39f9d20 90
68c864eb 91 grub_printf ("The files are identical.\n");
db1771cf 92 }
93
68c864eb 94cleanup:
b39f9d20 95
68c864eb 96 if (buf1)
97 grub_free (buf1);
98 if (buf2)
99 grub_free (buf2);
100 if (file1)
101 grub_file_close (file1);
102 if (file2)
103 grub_file_close (file2);
104
524a1e6a 105 return grub_errno;
db1771cf 106}
107
b1b797cb 108static grub_command_t cmd;
db1771cf 109\f
6d099807 110GRUB_MOD_INIT(cmp)
db1771cf 111{
b1b797cb 112 cmd = grub_register_command ("cmp", grub_cmd_cmp,
77a79592 113 N_("FILE1 FILE2"), N_("Compare two files."));
db1771cf 114}
115
6d099807 116GRUB_MOD_FINI(cmp)
db1771cf 117{
b1b797cb 118 grub_unregister_command (cmd);
db1771cf 119}