cmp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/gzio.h>
  24. #include <grub/command.h>
  25. #include <grub/i18n.h>
  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, "two arguments required");
  39. file1 = grub_gzfile_open (args[0], 1);
  40. file2 = grub_gzfile_open (args[1], 1);
  41. if (! file1 || ! file2)
  42. goto cleanup;
  43. if (grub_file_size (file1) != grub_file_size (file2))
  44. grub_error (GRUB_ERR_BAD_ARGUMENT,
  45. "Files differ in size: %llu [%s], %llu [%s]",
  46. (unsigned long long) grub_file_size (file1), args[0],
  47. (unsigned long long) grub_file_size (file2), args[1]);
  48. else
  49. {
  50. pos = 0;
  51. buf1 = grub_malloc (BUFFER_SIZE);
  52. buf2 = grub_malloc (BUFFER_SIZE);
  53. if (! buf1 || ! buf2)
  54. goto cleanup;
  55. do
  56. {
  57. int i;
  58. rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
  59. rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
  60. if (rd1 != rd2)
  61. goto cleanup;
  62. for (i = 0; i < rd2; i++)
  63. {
  64. if (buf1[i] != buf2[i])
  65. {
  66. grub_error (GRUB_ERR_BAD_ARGUMENT,
  67. "Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]",
  68. (unsigned long long) (i + pos), buf1[i], args[0],
  69. buf2[i], args[1]);
  70. goto cleanup;
  71. }
  72. }
  73. pos += BUFFER_SIZE;
  74. }
  75. while (rd2);
  76. }
  77. cleanup:
  78. if (buf1)
  79. grub_free (buf1);
  80. if (buf2)
  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. }