cmp.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.
  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. * (at your option) any later version.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/file.h>
  22. #include <grub/mm.h>
  23. #include <grub/command.h>
  24. #include <grub/extcmd.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. #define BUFFER_SIZE 512
  28. static const struct grub_arg_option options[] =
  29. {
  30. {0, 'v', 0, N_("Enable verbose output"), 0, 0},
  31. {0, 0, 0, 0, 0, 0}
  32. };
  33. static grub_err_t
  34. grub_cmd_cmp (grub_extcmd_context_t ctxt,
  35. int argc, char **args)
  36. {
  37. grub_ssize_t rd1, rd2;
  38. grub_off_t pos;
  39. grub_file_t file1 = 0;
  40. grub_file_t file2 = 0;
  41. char *buf1 = 0;
  42. char *buf2 = 0;
  43. grub_err_t err = GRUB_ERR_TEST_FAILURE;
  44. if (argc != 2)
  45. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  46. if (ctxt->state[0].set)
  47. grub_printf_ (N_("Compare file `%s' with `%s':\n"), args[0], args[1]);
  48. file1 = grub_file_open (args[0], GRUB_FILE_TYPE_CMP);
  49. file2 = grub_file_open (args[1], GRUB_FILE_TYPE_CMP);
  50. if (! file1 || ! file2)
  51. goto cleanup;
  52. if (ctxt->state[0].set && (grub_file_size (file1) != grub_file_size (file2)))
  53. grub_printf_ (N_("Files 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]);
  56. else
  57. {
  58. pos = 0;
  59. buf1 = grub_malloc (BUFFER_SIZE);
  60. buf2 = grub_malloc (BUFFER_SIZE);
  61. if (! buf1 || ! buf2)
  62. goto cleanup;
  63. do
  64. {
  65. int i;
  66. rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
  67. rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
  68. if (rd1 != rd2)
  69. goto cleanup;
  70. for (i = 0; i < rd2; i++)
  71. {
  72. if (buf1[i] != buf2[i])
  73. {
  74. if (ctxt->state[0].set)
  75. grub_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"),
  76. (unsigned long long) (i + pos), buf1[i],
  77. args[0], buf2[i], args[1]);
  78. goto cleanup;
  79. }
  80. }
  81. pos += BUFFER_SIZE;
  82. }
  83. while (rd2);
  84. /* TRANSLATORS: it's always exactly 2 files. */
  85. if (ctxt->state[0].set)
  86. grub_printf_ (N_("The files are identical.\n"));
  87. err = GRUB_ERR_NONE;
  88. }
  89. cleanup:
  90. grub_free (buf1);
  91. grub_free (buf2);
  92. if (file1)
  93. grub_file_close (file1);
  94. if (file2)
  95. grub_file_close (file2);
  96. return err;
  97. }
  98. static grub_extcmd_t cmd;
  99. GRUB_MOD_INIT(cmp)
  100. {
  101. cmd = grub_register_extcmd ("cmp", grub_cmd_cmp, 0,
  102. N_("FILE1 FILE2"), N_("Compare two files."),
  103. options);
  104. }
  105. GRUB_MOD_FINI(cmp)
  106. {
  107. grub_unregister_extcmd (cmd);
  108. }