Deleted Added
full compact
regular.c (87282) regular.c (87628)
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.bin/cmp/regular.c 87282 2001-12-03 20:56:16Z dwmalone $");
37
34#if 0
38#ifndef lint
35#ifndef lint
39static const char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94";
36static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94";
40#endif
37#endif
38#endif
41
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/usr.bin/cmp/regular.c 87628 2001-12-10 21:13:08Z dwmalone $");
42
42#include <sys/param.h>
43#include <sys/mman.h>
44#include <sys/stat.h>
45
46#include <err.h>
47#include <limits.h>
48#include <stdlib.h>
49#include <stdio.h>
50#include <string.h>
51#include <unistd.h>
52
53#include "extern.h"
54
55static u_char *remmap __P((u_char *, int, off_t));
56#define MMAP_CHUNK (8*1024*1024)
57
58#define ROUNDPAGE(i) ((i) & ~pagemask)
59
60void
61c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
62 int fd1, fd2;
63 const char *file1, *file2;
64 off_t skip1, len1, skip2, len2;
65{
66 u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
67 off_t byte, length, line;
68 int dfound;
69 off_t pagemask, off1, off2;
70 size_t pagesize;
71
72 if (skip1 > len1)
73 eofmsg(file1);
74 len1 -= skip1;
75 if (skip2 > len2)
76 eofmsg(file2);
77 len2 -= skip2;
78
79 if (sflag && len1 != len2)
80 exit(DIFF_EXIT);
81
82 pagesize = getpagesize();
83 pagemask = (off_t)pagesize - 1;
84 off1 = ROUNDPAGE(skip1);
85 off2 = ROUNDPAGE(skip2);
86
87 length = MIN(len1, len2);
88
89 if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
90 c_special(fd1, file1, skip1, fd2, file2, skip2);
91 return;
92 }
93
94 if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
95 munmap(m1, MMAP_CHUNK);
96 c_special(fd1, file1, skip1, fd2, file2, skip2);
97 return;
98 }
99
100 dfound = 0;
101 e1 = m1 + MMAP_CHUNK;
102 e2 = m2 + MMAP_CHUNK;
103 p1 = m1 + (skip1 - off1);
104 p2 = m2 + (skip2 - off2);
105
106 for (byte = line = 1; length--; ++byte) {
107 if ((ch = *p1) != *p2) {
108 if (xflag) {
109 dfound = 1;
110 (void)printf("%08llx %02x %02x\n",
111 (long long)byte - 1, ch, *p2);
112 } else if (lflag) {
113 dfound = 1;
114 (void)printf("%6lld %3o %3o\n",
115 (long long)byte, ch, *p2);
116 } else
117 diffmsg(file1, file2, byte, line);
118 /* NOTREACHED */
119 }
120 if (ch == '\n')
121 ++line;
122 if (++p1 == e1) {
123 off1 += MMAP_CHUNK;
124 if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
125 munmap(m2, MMAP_CHUNK);
126 err(ERR_EXIT, "remmap %s", file1);
127 }
128 e1 = m1 + MMAP_CHUNK;
129 }
130 if (++p2 == e2) {
131 off2 += MMAP_CHUNK;
132 if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
133 munmap(m1, MMAP_CHUNK);
134 err(ERR_EXIT, "remmap %s", file2);
135 }
136 e2 = m2 + MMAP_CHUNK;
137 }
138 }
139 munmap(m1, MMAP_CHUNK);
140 munmap(m2, MMAP_CHUNK);
141
142 if (len1 != len2)
143 eofmsg (len1 > len2 ? file2 : file1);
144 if (dfound)
145 exit(DIFF_EXIT);
146}
147
148static u_char *
149remmap(mem, fd, offset)
150 u_char *mem;
151 int fd;
152 off_t offset;
153{
154 if (mem != NULL)
155 munmap(mem, MMAP_CHUNK);
156 mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
157 if (mem == MAP_FAILED)
158 return (NULL);
159 madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
160 return (mem);
161}
43#include <sys/param.h>
44#include <sys/mman.h>
45#include <sys/stat.h>
46
47#include <err.h>
48#include <limits.h>
49#include <stdlib.h>
50#include <stdio.h>
51#include <string.h>
52#include <unistd.h>
53
54#include "extern.h"
55
56static u_char *remmap __P((u_char *, int, off_t));
57#define MMAP_CHUNK (8*1024*1024)
58
59#define ROUNDPAGE(i) ((i) & ~pagemask)
60
61void
62c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
63 int fd1, fd2;
64 const char *file1, *file2;
65 off_t skip1, len1, skip2, len2;
66{
67 u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
68 off_t byte, length, line;
69 int dfound;
70 off_t pagemask, off1, off2;
71 size_t pagesize;
72
73 if (skip1 > len1)
74 eofmsg(file1);
75 len1 -= skip1;
76 if (skip2 > len2)
77 eofmsg(file2);
78 len2 -= skip2;
79
80 if (sflag && len1 != len2)
81 exit(DIFF_EXIT);
82
83 pagesize = getpagesize();
84 pagemask = (off_t)pagesize - 1;
85 off1 = ROUNDPAGE(skip1);
86 off2 = ROUNDPAGE(skip2);
87
88 length = MIN(len1, len2);
89
90 if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
91 c_special(fd1, file1, skip1, fd2, file2, skip2);
92 return;
93 }
94
95 if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
96 munmap(m1, MMAP_CHUNK);
97 c_special(fd1, file1, skip1, fd2, file2, skip2);
98 return;
99 }
100
101 dfound = 0;
102 e1 = m1 + MMAP_CHUNK;
103 e2 = m2 + MMAP_CHUNK;
104 p1 = m1 + (skip1 - off1);
105 p2 = m2 + (skip2 - off2);
106
107 for (byte = line = 1; length--; ++byte) {
108 if ((ch = *p1) != *p2) {
109 if (xflag) {
110 dfound = 1;
111 (void)printf("%08llx %02x %02x\n",
112 (long long)byte - 1, ch, *p2);
113 } else if (lflag) {
114 dfound = 1;
115 (void)printf("%6lld %3o %3o\n",
116 (long long)byte, ch, *p2);
117 } else
118 diffmsg(file1, file2, byte, line);
119 /* NOTREACHED */
120 }
121 if (ch == '\n')
122 ++line;
123 if (++p1 == e1) {
124 off1 += MMAP_CHUNK;
125 if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
126 munmap(m2, MMAP_CHUNK);
127 err(ERR_EXIT, "remmap %s", file1);
128 }
129 e1 = m1 + MMAP_CHUNK;
130 }
131 if (++p2 == e2) {
132 off2 += MMAP_CHUNK;
133 if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
134 munmap(m1, MMAP_CHUNK);
135 err(ERR_EXIT, "remmap %s", file2);
136 }
137 e2 = m2 + MMAP_CHUNK;
138 }
139 }
140 munmap(m1, MMAP_CHUNK);
141 munmap(m2, MMAP_CHUNK);
142
143 if (len1 != len2)
144 eofmsg (len1 > len2 ? file2 : file1);
145 if (dfound)
146 exit(DIFF_EXIT);
147}
148
149static u_char *
150remmap(mem, fd, offset)
151 u_char *mem;
152 int fd;
153 off_t offset;
154{
155 if (mem != NULL)
156 munmap(mem, MMAP_CHUNK);
157 mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
158 if (mem == MAP_FAILED)
159 return (NULL);
160 madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
161 return (mem);
162}