cmp.c 2.83 KB
Newer Older
1 2
/* cmd.c - command to cmp an operating system */
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2003,2005,2006,2007,2009  Free Software Foundation, Inc.
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  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
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20 21 22
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/file.h>
23
#include <grub/mm.h>
24
#include <grub/command.h>
25
#include <grub/i18n.h>
26

27 28
GRUB_MOD_LICENSE ("GPLv3+");

29
#define BUFFER_SIZE 512
30

31
static grub_err_t
32
grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
33 34
	      int argc, char **args)
{
35
  grub_ssize_t rd1, rd2;
36
  grub_off_t pos;
37 38 39 40
  grub_file_t file1 = 0;
  grub_file_t file2 = 0;
  char *buf1 = 0;
  char *buf2 = 0;
41 42

  if (argc != 2)
43
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
44

45 46
  grub_printf_ (N_("Compare file `%s' with `%s':\n"), args[0],
		args[1]);
47

48 49
  file1 = grub_file_open (args[0]);
  file2 = grub_file_open (args[1]);
50
  if (! file1 || ! file2)
51
    goto cleanup;
52

53
  if (grub_file_size (file1) != grub_file_size (file2))
54 55 56
    grub_printf_ (N_("Files differ in size: %llu [%s], %llu [%s]\n"),
		  (unsigned long long) grub_file_size (file1), args[0],
		  (unsigned long long) grub_file_size (file2), args[1]);
57 58
  else
    {
59 60
      pos = 0;

61 62
      buf1 = grub_malloc (BUFFER_SIZE);
      buf2 = grub_malloc (BUFFER_SIZE);
63

64
      if (! buf1 || ! buf2)
65
        goto cleanup;
66

67 68 69
      do
	{
	  int i;
70

71 72
	  rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
	  rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
73 74

	  if (rd1 != rd2)
75
	    goto cleanup;
76

77
	  for (i = 0; i < rd2; i++)
78 79 80
	    {
	      if (buf1[i] != buf2[i])
		{
81 82 83
		  grub_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]);
84
		  goto cleanup;
85 86
		}
	    }
87
	  pos += BUFFER_SIZE;
88

89 90
	}
      while (rd2);
91

92 93
      /* TRANSLATORS: it's always exactly 2 files.  */
      grub_printf_ (N_("The files are identical.\n"));
94 95
    }

96
cleanup:
97

98 99
  grub_free (buf1);
  grub_free (buf2);
100 101 102 103 104
  if (file1)
    grub_file_close (file1);
  if (file2)
    grub_file_close (file2);

105
  return grub_errno;
106 107
}

108
static grub_command_t cmd;
109

110
GRUB_MOD_INIT(cmp)
111
{
112
  cmd = grub_register_command ("cmp", grub_cmd_cmp,
113
			       N_("FILE1 FILE2"), N_("Compare two files."));
114 115
}

116
GRUB_MOD_FINI(cmp)
117
{
118
  grub_unregister_command (cmd);
119
}