diff options
Diffstat (limited to 'usr.bin/cmp')
-rw-r--r-- | usr.bin/cmp/regular.c | 14 | ||||
-rw-r--r-- | usr.bin/cmp/special.c | 2 |
2 files changed, 13 insertions, 3 deletions
diff --git a/usr.bin/cmp/regular.c b/usr.bin/cmp/regular.c index 76f2387..5d301fa 100644 --- a/usr.bin/cmp/regular.c +++ b/usr.bin/cmp/regular.c @@ -44,9 +44,12 @@ static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94"; #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <unistd.h> #include "extern.h" +#define ROUNDPAGE(i) ((i) & ~pagemask) + void c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2) int fd1, fd2; @@ -56,6 +59,7 @@ c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2) u_char ch, *p1, *p2; off_t byte, length, line; int dfound; + off_t pagemask, off1, off2; if (sflag && len1 != len2) exit(1); @@ -67,21 +71,27 @@ c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2) eofmsg(file2); len2 -= skip2; + pagemask = (off_t)getpagesize() - 1; + off1 = ROUNDPAGE(skip1); + off2 = ROUNDPAGE(skip2); + length = MIN(len1, len2); if (length > SIZE_T_MAX) return (c_special(fd1, file1, skip1, fd2, file2, skip2)); if ((p1 = (u_char *)mmap(NULL, - (size_t)length, PROT_READ, 0, fd1, skip1)) == (u_char *)-1) + (size_t)length, PROT_READ, 0, fd1, off1)) == (u_char *)-1) err(ERR_EXIT, "%s", file1); madvise(p1, length, MADV_SEQUENTIAL); if ((p2 = (u_char *)mmap(NULL, - (size_t)length, PROT_READ, 0, fd2, skip2)) == (u_char *)-1) + (size_t)length, PROT_READ, 0, fd2, off2)) == (u_char *)-1) err(ERR_EXIT, "%s", file2); madvise(p2, length, MADV_SEQUENTIAL); dfound = 0; + p1 += skip1 - off1; + p2 += skip2 - off2; for (byte = line = 1; length--; ++p1, ++p2, ++byte) { if ((ch = *p1) != *p2) if (lflag) { diff --git a/usr.bin/cmp/special.c b/usr.bin/cmp/special.c index 0dcd0c5..0a15fa1 100644 --- a/usr.bin/cmp/special.c +++ b/usr.bin/cmp/special.c @@ -60,6 +60,7 @@ c_special(fd1, file1, skip1, fd2, file2, skip2) if ((fp2 = fdopen(fd2, "r")) == NULL) err(ERR_EXIT, "%s", file2); + dfound = 0; while (skip1--) if (getc(fp1) == EOF) goto eof; @@ -67,7 +68,6 @@ c_special(fd1, file1, skip1, fd2, file2, skip2) if (getc(fp2) == EOF) goto eof; - dfound = 0; for (byte = line = 1;; ++byte) { ch1 = getc(fp1); ch2 = getc(fp2); |