cmp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/i18n.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #define BUFFER_SIZE 512
  27. static grub_err_t
  28. grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
  29. int argc, char **args)
  30. {
  31. grub_ssize_t rd1, rd2;
  32. grub_off_t pos;
  33. grub_file_t file1 = 0;
  34. grub_file_t file2 = 0;
  35. char *buf1 = 0;
  36. char *buf2 = 0;
  37. if (argc != 2)
  38. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  39. grub_printf_ (N_("Compare file `%s' with `%s':\n"), args[0],
  40. args[1]);
  41. file1 = grub_file_open (args[0]);
  42. file2 = grub_file_open (args[1]);
  43. if (! file1 || ! file2)
  44. goto cleanup;
  45. if (grub_file_size (file1) != grub_file_size (file2))
  46. grub_printf_ (N_("Files differ in size: %llu [%s], %llu [%s]\n"),
  47. (unsigned long long) grub_file_size (file1), args[0],
  48. (unsigned long long) grub_file_size (file2), args[1]);
  49. else
  50. {
  51. pos = 0;
  52. buf1 = grub_malloc (BUFFER_SIZE);
  53. buf2 = grub_malloc (BUFFER_SIZE);
  54. if (! buf1 || ! buf2)
  55. goto cleanup;
  56. do
  57. {
  58. int i;
  59. rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
  60. rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
  61. if (rd1 != rd2)
  62. goto cleanup;
  63. for (i = 0; i < rd2; i++)
  64. {
  65. if (buf1[i] != buf2[i])
  66. {
  67. grub_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"),
  68. (unsigned long long) (i + pos), buf1[i],
  69. args[0], buf2[i], args[1]);
  70. goto cleanup;
  71. }
  72. }
  73. pos += BUFFER_SIZE;
  74. }
  75. while (rd2);
  76. /* TRANSLATORS: it's always exactly 2 files. */
  77. grub_printf_ (N_("The files are identical.\n"));
  78. }
  79. cleanup:
  80. grub_free (buf1);
  81. grub_free (buf2);
  82. if (file1)
  83. grub_file_close (file1);
  84. if (file2)
  85. grub_file_close (file2);
  86. return grub_errno;
  87. }
  88. static grub_command_t cmd;
  89. GRUB_MOD_INIT(cmp)
  90. {
  91. cmd = grub_register_command ("cmp", grub_cmd_cmp,
  92. N_("FILE1 FILE2"), N_("Compare two files."));
  93. }
  94. GRUB_MOD_FINI(cmp)
  95. {
  96. grub_unregister_command (cmd);
  97. }