1 /* cmd.c - command to cmp an operating system */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007 Free Software Foundation, Inc.
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 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
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.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/normal.h>
23 #include <grub/misc.h>
24 #include <grub/file.h>
26 #include <grub/gzio.h>
28 #define BUFFER_SIZE 512
31 grub_cmd_cmp (struct grub_arg_list
*state
__attribute__ ((unused
)),
32 int argc
, char **args
)
34 grub_ssize_t rd1
, rd2
;
36 grub_file_t file1
= 0;
37 grub_file_t file2
= 0;
42 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "two arguments required");
44 grub_printf ("Compare `%s' and `%s':\n", args
[0],
47 file1
= grub_gzfile_open (args
[0], 1);
48 file2
= grub_gzfile_open (args
[1], 1);
49 if (! file1
|| ! file2
)
52 if (grub_file_size (file1
) != grub_file_size (file2
))
53 grub_printf ("Differ in size: %llu [%s], %llu [%s]\n",
54 (unsigned long long) grub_file_size (file1
), args
[0],
55 (unsigned long long) grub_file_size (file2
), args
[1]);
60 buf1
= grub_malloc (BUFFER_SIZE
);
61 buf2
= grub_malloc (BUFFER_SIZE
);
70 rd1
= grub_file_read (file1
, buf1
, BUFFER_SIZE
);
71 rd2
= grub_file_read (file2
, buf2
, BUFFER_SIZE
);
76 for (i
= 0; i
< rd2
; i
++)
78 if (buf1
[i
] != buf2
[i
])
80 grub_printf ("Differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n",
81 (unsigned long long) (i
+ pos
), buf1
[i
], args
[0],
91 grub_printf ("The files are identical.\n");
101 grub_file_close (file1
);
103 grub_file_close (file2
);
111 (void) mod
; /* To stop warning. */
112 grub_register_command ("cmp", grub_cmd_cmp
, GRUB_COMMAND_FLAG_BOTH
,
113 "cmp FILE1 FILE2", "Compare two files.", 0);
118 grub_unregister_command ("cmp");