changed zfs files naming to (hdX,Y)/filesystem@@file
[grub2/phcoder.git] / commands / cmp.c
blob1258b1d63c99ad65efda26764b4538aea2b9991b
1 /* cmd.c - command to cmp an operating system */
2 /*
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/dl.h>
21 #include <grub/misc.h>
22 #include <grub/file.h>
23 #include <grub/mm.h>
24 #include <grub/gzio.h>
25 #include <grub/command.h>
27 #define BUFFER_SIZE 512
29 static grub_err_t
30 grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
31 int argc, char **args)
33 grub_ssize_t rd1, rd2;
34 grub_off_t pos;
35 grub_file_t file1 = 0;
36 grub_file_t file2 = 0;
37 char *buf1 = 0;
38 char *buf2 = 0;
40 if (argc != 2)
41 return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
43 grub_printf ("Compare `%s' and `%s':\n", args[0],
44 args[1]);
46 file1 = grub_gzfile_open (args[0], 1);
47 file2 = grub_gzfile_open (args[1], 1);
48 if (! file1 || ! file2)
49 goto cleanup;
51 if (grub_file_size (file1) != grub_file_size (file2))
52 grub_printf ("Differ in size: %llu [%s], %llu [%s]\n",
53 (unsigned long long) grub_file_size (file1), args[0],
54 (unsigned long long) grub_file_size (file2), args[1]);
55 else
57 pos = 0;
59 buf1 = grub_malloc (BUFFER_SIZE);
60 buf2 = grub_malloc (BUFFER_SIZE);
62 if (! buf1 || ! buf2)
63 goto cleanup;
67 int i;
69 rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
70 rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
72 if (rd1 != rd2)
73 goto cleanup;
75 for (i = 0; i < rd2; i++)
77 if (buf1[i] != buf2[i])
79 grub_printf ("Differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n",
80 (unsigned long long) (i + pos), buf1[i], args[0],
81 buf2[i], args[1]);
82 goto cleanup;
85 pos += BUFFER_SIZE;
88 while (rd2);
90 grub_printf ("The files are identical.\n");
93 cleanup:
95 if (buf1)
96 grub_free (buf1);
97 if (buf2)
98 grub_free (buf2);
99 if (file1)
100 grub_file_close (file1);
101 if (file2)
102 grub_file_close (file2);
104 return grub_errno;
107 static grub_command_t cmd;
109 GRUB_MOD_INIT(cmp)
111 cmd = grub_register_command ("cmp", grub_cmd_cmp,
112 "cmp FILE1 FILE2", "Compare two files.");
115 GRUB_MOD_FINI(cmp)
117 grub_unregister_command (cmd);