Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / cmp / regular.c
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  * $FreeBSD: src/usr.bin/cmp/regular.c,v 1.7.2.3 2001/11/21 10:47:54 dwmalone Exp $
34  *
35  */
36
37 #ifndef lint
38 static const char sccsid[] = "@(#)regular.c     8.3 (Berkeley) 4/2/94";
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/mman.h>
43 #include <sys/stat.h>
44
45 #include <err.h>
46 #include <limits.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "extern.h"
53
54 static u_char *remmap __P((u_char *, int, off_t));
55 #define MMAP_CHUNK (8*1024*1024)
56
57 #define ROUNDPAGE(i) ((i) & ~pagemask)
58
59 void
60 c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
61         int fd1, fd2;
62         const char *file1, *file2;
63         off_t skip1, len1, skip2, len2;
64 {
65         u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
66         off_t byte, length, line;
67         int dfound;
68         off_t pagemask, off1, off2;
69         size_t pagesize;
70
71         if (skip1 > len1)
72                 eofmsg(file1);
73         len1 -= skip1;
74         if (skip2 > len2)
75                 eofmsg(file2);
76         len2 -= skip2;
77
78         if (sflag && len1 != len2)
79                 exit(DIFF_EXIT);
80
81         pagesize = getpagesize();
82         pagemask = (off_t)pagesize - 1;
83         off1 = ROUNDPAGE(skip1);
84         off2 = ROUNDPAGE(skip2);
85
86         length = MIN(len1, len2);
87
88         if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
89                 c_special(fd1, file1, skip1, fd2, file2, skip2);
90                 return;
91         }
92
93         if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
94                 munmap(m1, MMAP_CHUNK);
95                 c_special(fd1, file1, skip1, fd2, file2, skip2);
96                 return;
97         }
98
99         dfound = 0;
100         e1 = m1 + MMAP_CHUNK;
101         e2 = m2 + MMAP_CHUNK;
102         p1 = m1 + (skip1 - off1);
103         p2 = m2 + (skip2 - off2);
104
105         for (byte = line = 1; length--; ++byte) {
106                 if ((ch = *p1) != *p2) {
107                         if (xflag) {
108                                 dfound = 1;
109                                 (void)printf("%08qx %02x %02x\n", byte - 1, ch, *p2);
110                         } else if (lflag) {
111                                 dfound = 1;
112                                 (void)printf("%6qd %3o %3o\n", byte, ch, *p2);
113                         } else
114                                 diffmsg(file1, file2, byte, line);
115                                 /* NOTREACHED */
116                 }
117                 if (ch == '\n')
118                         ++line;
119                 if (++p1 == e1) {
120                         off1 += MMAP_CHUNK;
121                         if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
122                                 munmap(m2, MMAP_CHUNK);
123                                 err(ERR_EXIT, "remmap %s", file1);
124                         }
125                         e1 = m1 + MMAP_CHUNK;
126                 }
127                 if (++p2 == e2) {
128                         off2 += MMAP_CHUNK;
129                         if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
130                                 munmap(m1, MMAP_CHUNK);
131                                 err(ERR_EXIT, "remmap %s", file2);
132                         }
133                         e2 = m2 + MMAP_CHUNK;
134                 }
135         }
136         munmap(m1, MMAP_CHUNK);
137         munmap(m2, MMAP_CHUNK);
138
139         if (len1 != len2)
140                 eofmsg (len1 > len2 ? file2 : file1);
141         if (dfound)
142                 exit(DIFF_EXIT);
143 }
144
145 static u_char *
146 remmap(mem, fd, offset)
147         u_char  *mem;
148         int     fd;
149         off_t   offset;
150 {
151         if (mem != NULL)
152                 munmap(mem, MMAP_CHUNK);
153         mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
154         if (mem == MAP_FAILED)
155                 return (NULL);
156         madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
157         return (mem);
158 }