/[grub]/grub2/commands/cmp.c
ViewVC logotype

Diff of /grub2/commands/cmp.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by okuji, Sun Apr 4 13:46:00 2004 UTC revision 1.3 by subdino, Sat Jul 9 16:29:02 2005 UTC
# Line 23  Line 23 
23  #include <grub/arg.h>  #include <grub/arg.h>
24  #include <grub/misc.h>  #include <grub/misc.h>
25  #include <grub/file.h>  #include <grub/file.h>
26    #include <grub/mm.h>
27    
28    #define BUFFER_SIZE 512
29    
30  static grub_err_t  static grub_err_t
31  grub_cmd_cmp (struct grub_arg_list *state __attribute__ ((unused)),  grub_cmd_cmp (struct grub_arg_list *state __attribute__ ((unused)),
32                int argc, char **args)                int argc, char **args)
33  {  {
34    grub_file_t file1;    grub_err_t err;
35    grub_file_t file2;    grub_ssize_t rd1, rd2;
36      grub_uint32_t pos;
37      grub_file_t file1 = 0;
38      grub_file_t file2 = 0;
39      char *buf1 = 0;
40      char *buf2 = 0;
41    
42    if (argc != 2)    if (argc != 2)
43      return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");      return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
# Line 37  grub_cmd_cmp (struct grub_arg_list *stat Line 45  grub_cmd_cmp (struct grub_arg_list *stat
45    grub_printf ("Compare `%s' and `%s':\n", args[0],    grub_printf ("Compare `%s' and `%s':\n", args[0],
46                 args[1]);                 args[1]);
47    
48    file1 = grub_file_open (args[0]);    if (! (file1 = grub_file_open (args[0]) ) ||
49    if (! file1)        ! (file2 = grub_file_open (args[1]) ) )
50      return grub_errno;      goto cleanup;
   
   file2 = grub_file_open (args[1]);  
   if (! file2)  
     {  
       grub_file_close (file2);  
       return grub_errno;  
     }  
51    
52    if (grub_file_size (file1) != grub_file_size (file2))    if (grub_file_size (file1) != grub_file_size (file2))
53      grub_printf ("Differ in size: %d [%s], %d [%s]\n",      grub_printf ("Differ in size: %d [%s], %d [%s]\n",
# Line 55  grub_cmd_cmp (struct grub_arg_list *stat Line 56  grub_cmd_cmp (struct grub_arg_list *stat
56        
57    else    else
58      {      {
59        char buf1[512];        pos = 0;
60        char buf2[512];  
61        grub_ssize_t rd1, rd2;        if (! (buf1 = (char *) grub_malloc (BUFFER_SIZE) ) ||
62        grub_uint32_t pos = 0;            ! (buf2 = (char *) grub_malloc (BUFFER_SIZE) ) )
63                goto cleanup;
64        do        do
65          {          {
66            int i;            int i;
67            rd1 = grub_file_read (file1, buf1, 512);            rd1 = grub_file_read (file1, buf1, BUFFER_SIZE);
68            rd2 = grub_file_read (file2, buf2, 512);            rd2 = grub_file_read (file2, buf2, BUFFER_SIZE);
69    
70            if (rd1 != rd2)            if (rd1 != rd2)
71              return 0;              goto cleanup;
72    
73            for (i = 0; i < 512; i++)            for (i = 0; i < rd2; i++)
74              {              {
75                if (buf1[i] != buf2[i])                if (buf1[i] != buf2[i])
76                  {                  {
77                    grub_printf ("Differ at the offset %d: 0x%x [%s], 0x%x [%s]\n",                    grub_printf ("Differ at the offset %d: 0x%x [%s], 0x%x [%s]\n",
78                                 i + pos, buf1[i], args[0],                                 i + pos, buf1[i], args[0],
79                                 buf2[i], args[1]);                                 buf2[i], args[1]);
80                      goto cleanup;
                   grub_file_close (file1);  
                   grub_file_close (file2);  
                   return 0;  
81                  }                  }
82              }              }
83            pos += 512;            pos += BUFFER_SIZE;
84                        
85          } while (rd2);          } while (rd2);
86          grub_printf ("The files are identical.\n");
87      }      }
88    
89    grub_file_close (file1);  cleanup:
90    grub_file_close (file2);    err=grub_errno;
91      if (buf1)
92    grub_printf ("The files are identical.\n");      grub_free (buf1);
93      if (buf2)
94        grub_free (buf2);
95      if (file1)
96        grub_file_close (file1);
97      if (file2)
98        grub_file_close (file2);
99    
100    return 0;    return err;
101  }  }
102    
103    

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26