summaryrefslogtreecommitdiffstats
path: root/src/examples/cmp.c
blob: 94b406d1518431bc17dd36625bbcb803fe80abc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* cat.c

   Compares two files. */

#include <stdio.h>
#include <syscall.h>

int
main (int argc, char *argv[]) 
{
  int fd[2];

  if (argc != 3) 
    {
      printf ("usage: cmp A B\n");
      return EXIT_FAILURE;
    }

  /* Open files. */
  fd[0] = open (argv[1]);
  if (fd[0] < 0) 
    {
      printf ("%s: open failed\n", argv[1]);
      return EXIT_FAILURE;
    }
  fd[1] = open (argv[2]);
  if (fd[1] < 0) 
    {
      printf ("%s: open failed\n", argv[1]);
      return EXIT_FAILURE;
    }

  /* Compare data. */
  for (;;) 
    {
      int pos;
      char buffer[2][1024];
      int bytes_read[2];
      int min_read;
      int i;

      pos = tell (fd[0]);
      bytes_read[0] = read (fd[0], buffer[0], sizeof buffer[0]);
      bytes_read[1] = read (fd[1], buffer[1], sizeof buffer[1]);
      min_read = bytes_read[0] < bytes_read[1] ? bytes_read[0] : bytes_read[1];
      if (min_read == 0)
        break;

      for (i = 0; i < min_read; i++)
        if (buffer[0][i] != buffer[1][i]) 
          {
            printf ("Byte %d is %02hhx ('%c') in %s but %02hhx ('%c') in %s\n",
                    pos + i,
                    buffer[0][i], buffer[0][i], argv[1],
                    buffer[1][i], buffer[1][i], argv[2]);
            return EXIT_FAILURE;
          }

      if (min_read < bytes_read[1])
        printf ("%s is shorter than %s\n", argv[1], argv[2]);
      else if (min_read < bytes_read[0])
        printf ("%s is shorter than %s\n", argv[2], argv[1]);
    }

  printf ("%s and %s are identical\n", argv[1], argv[2]);

  return EXIT_SUCCESS;
}