summaryrefslogtreecommitdiffstats
path: root/usr.bin/cmp/regular.c
diff options
context:
space:
mode:
authorjoerg <joerg@FreeBSD.org>1997-01-08 12:00:55 +0000
committerjoerg <joerg@FreeBSD.org>1997-01-08 12:00:55 +0000
commit151ff8e003fe5636b084dd2b8474bb95c4af86f1 (patch)
tree7edd4b5974c91e0aee7a6cb25e43aa5d8d4284e9 /usr.bin/cmp/regular.c
parenteaaff5d31266a1b09d072f021239b0bcd26e63f5 (diff)
downloadFreeBSD-src-151ff8e003fe5636b084dd2b8474bb95c4af86f1.zip
FreeBSD-src-151ff8e003fe5636b084dd2b8474bb95c4af86f1.tar.gz
Our mmap(2) has a limitation where the `offset' parameter must be
page-aligned. cmp(1) should know about this flaw, and work around it. While i was at it, fixed an uninitialized variable as reported by -Wall.
Diffstat (limited to 'usr.bin/cmp/regular.c')
-rw-r--r--usr.bin/cmp/regular.c14
1 files changed, 12 insertions, 2 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) {
OpenPOWER on IntegriCloud