Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / cmp.c
blobcc23ee67ea372c6c0fc4014ce9cfaf56e3924545
1 /* cmd.c - command to cmp an operating system */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007,2009 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/command.h>
25 #include <grub/i18n.h>
27 GRUB_MOD_LICENSE ("GPLv3+");
29 #define BUFFER_SIZE 512
31 static grub_err_t
32 grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
33 int argc, char **args)
35 grub_ssize_t rd1, rd2;
36 grub_off_t pos;
37 grub_file_t file1 = 0;
38 grub_file_t file2 = 0;
39 char *buf1 = 0;
40 char *buf2 = 0;
42 if (argc != 2)
43 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
45 grub_printf_ (N_("Compare file `%s' with `%s':\n"), args[0],
46 args[1]);
48 file1 = grub_file_open (args[0]);
49 file2 = grub_file_open (args[1]);
50 if (! file1 || ! file2)
51 goto cleanup;
53 if (grub_file_size (file1) != grub_file_size (file2))
54 grub_printf_ (N_("Files differ in size: %llu [%s], %llu [%s]\n"),
55 (unsigned long long) grub_file_size (file1), args[0],
56 (unsigned long long) grub_file_size (file2), args[1]);
57 else
59 pos = 0;
61 buf1 = grub_malloc (BUFFER_SIZE);
62 buf2 = grub_malloc (BUFFER_SIZE);
64 if (! buf1 || ! buf2)
65 goto cleanup;
69 int i;
71 rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
72 rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
74 if (rd1 != rd2)
75 goto cleanup;
77 for (i = 0; i < rd2; i++)
79 if (buf1[i] != buf2[i])
81 grub_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"),
82 (unsigned long long) (i + pos), buf1[i],
83 args[0], buf2[i], args[1]);
84 goto cleanup;
87 pos += BUFFER_SIZE;
90 while (rd2);
92 /* TRANSLATORS: it's always exactly 2 files. */
93 grub_printf_ (N_("The files are identical.\n"));
96 cleanup:
98 grub_free (buf1);
99 grub_free (buf2);
100 if (file1)
101 grub_file_close (file1);
102 if (file2)
103 grub_file_close (file2);
105 return grub_errno;
108 static grub_command_t cmd;
110 GRUB_MOD_INIT(cmp)
112 cmd = grub_register_command ("cmp", grub_cmd_cmp,
113 N_("FILE1 FILE2"), N_("Compare two files."));
116 GRUB_MOD_FINI(cmp)
118 grub_unregister_command (cmd);