769c525a7dfd61c06f102e35059233499c2e94fa
[mmh] / sbr / fdcompare.c
1 /*
2 ** fdcompare.c -- are two files identical?
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11
12 int
13 fdcompare (int fd1, int fd2)
14 {
15         register int i, n1, n2, resp;
16         register char *c1, *c2;
17         char b1[BUFSIZ], b2[BUFSIZ];
18
19         resp = 1;
20         while ((n1 = read (fd1, b1, sizeof(b1))) >= 0
21                 && (n2 = read (fd2, b2, sizeof(b2))) >= 0
22                 && n1 == n2) {
23                 c1 = b1;
24                 c2 = b2;
25                 for (i = n1 < sizeof(b1) ? n1 : sizeof(b1); i--;)
26                         if (*c1++ != *c2++) {
27                                 resp = 0;
28                                 goto leave;
29                         }
30                 if (n1 < sizeof(b1))
31                         goto leave;
32         }
33         resp = 0;
34
35 leave: ;
36         lseek (fd1, (off_t) 0, SEEK_SET);
37         lseek (fd2, (off_t) 0, SEEK_SET);
38         return resp;
39 }