~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to commands/cmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* cmd.c - command to cmp an operating system */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003,2005  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 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program 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, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/normal.h>
 
22
#include <grub/dl.h>
 
23
#include <grub/arg.h>
 
24
#include <grub/misc.h>
 
25
#include <grub/file.h>
 
26
#include <grub/mm.h>
 
27
#include <grub/gzio.h>
 
28
 
 
29
#define BUFFER_SIZE 512
 
30
 
 
31
static grub_err_t
 
32
grub_cmd_cmp (struct grub_arg_list *state __attribute__ ((unused)),
 
33
              int argc, char **args)
 
34
{
 
35
  grub_err_t err;
 
36
  grub_ssize_t rd1, rd2;
 
37
  grub_uint32_t pos;
 
38
  grub_file_t file1 = 0;
 
39
  grub_file_t file2 = 0;
 
40
  char *buf1 = 0;
 
41
  char *buf2 = 0;
 
42
 
 
43
  if (argc != 2)
 
44
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
 
45
 
 
46
  grub_printf ("Compare `%s' and `%s':\n", args[0],
 
47
               args[1]);
 
48
 
 
49
  if (! (file1 = grub_gzfile_open (args[0], 1) ) ||
 
50
      ! (file2 = grub_gzfile_open (args[1], 1) ) )
 
51
    goto cleanup;
 
52
 
 
53
  if (grub_file_size (file1) != grub_file_size (file2))
 
54
    grub_printf ("Differ in size: %d [%s], %d [%s]\n", 
 
55
                 grub_file_size (file1), args[0], 
 
56
                 grub_file_size (file2), args[1]);
 
57
  
 
58
  else
 
59
    {
 
60
      pos = 0;
 
61
 
 
62
      if (! (buf1 = (char *) grub_malloc (BUFFER_SIZE) ) ||
 
63
          ! (buf2 = (char *) grub_malloc (BUFFER_SIZE) ) )
 
64
        goto cleanup;
 
65
      do
 
66
        {
 
67
          int i;
 
68
          rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
 
69
          rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
 
70
 
 
71
          if (rd1 != rd2)
 
72
            goto cleanup;
 
73
 
 
74
          for (i = 0; i < rd2; i++)
 
75
            {
 
76
              if (buf1[i] != buf2[i])
 
77
                {
 
78
                  grub_printf ("Differ at the offset %d: 0x%x [%s], 0x%x [%s]\n",
 
79
                               i + pos, buf1[i], args[0],
 
80
                               buf2[i], args[1]);
 
81
                  goto cleanup;
 
82
                }
 
83
            }
 
84
          pos += BUFFER_SIZE;
 
85
          
 
86
        } while (rd2);
 
87
      grub_printf ("The files are identical.\n");
 
88
    }
 
89
 
 
90
cleanup:
 
91
  err=grub_errno;
 
92
  if (buf1)
 
93
    grub_free (buf1);
 
94
  if (buf2)
 
95
    grub_free (buf2);
 
96
  if (file1)
 
97
    grub_file_close (file1);
 
98
  if (file2)
 
99
    grub_file_close (file2);
 
100
 
 
101
  return err;
 
102
}
 
103
 
 
104
 
 
105
GRUB_MOD_INIT(cmp)
 
106
{
 
107
  (void) mod;                   /* To stop warning. */
 
108
  grub_register_command ("cmp", grub_cmd_cmp, GRUB_COMMAND_FLAG_BOTH,
 
109
                         "cmp FILE1 FILE2", "Compare two files.", 0);
 
110
}
 
111
 
 
112
GRUB_MOD_FINI(cmp)
 
113
{
 
114
  grub_unregister_command ("cmp");
 
115
}