]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/cmp/regular.c
Update to ELF Tool Chain r3668
[FreeBSD/FreeBSD.git] / usr.bin / cmp / regular.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)regular.c   8.3 (Berkeley) 4/2/94";
35 #endif
36 #endif
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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 <signal.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <unistd.h>
51
52 #include "extern.h"
53
54 static u_char *remmap(u_char *, int, off_t);
55 static void segv_handler(int);
56 #define MMAP_CHUNK (8*1024*1024)
57
58 #define ROUNDPAGE(i) ((i) & ~pagemask)
59
60 void
61 c_regular(int fd1, const char *file1, off_t skip1, off_t len1,
62     int fd2, const char *file2, off_t skip2, off_t len2)
63 {
64         u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
65         off_t byte, length, line;
66         int dfound;
67         off_t pagemask, off1, off2;
68         size_t pagesize;
69         struct sigaction act, oact;
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         sigemptyset(&act.sa_mask);
82         act.sa_flags = SA_NODEFER;
83         act.sa_handler = segv_handler;
84         if (sigaction(SIGSEGV, &act, &oact))
85                 err(ERR_EXIT, "sigaction()");
86
87         pagesize = getpagesize();
88         pagemask = (off_t)pagesize - 1;
89         off1 = ROUNDPAGE(skip1);
90         off2 = ROUNDPAGE(skip2);
91
92         length = MIN(len1, len2);
93
94         if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
95                 c_special(fd1, file1, skip1, fd2, file2, skip2);
96                 return;
97         }
98
99         if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
100                 munmap(m1, MMAP_CHUNK);
101                 c_special(fd1, file1, skip1, fd2, file2, skip2);
102                 return;
103         }
104
105         dfound = 0;
106         e1 = m1 + MMAP_CHUNK;
107         e2 = m2 + MMAP_CHUNK;
108         p1 = m1 + (skip1 - off1);
109         p2 = m2 + (skip2 - off2);
110
111         for (byte = line = 1; length--; ++byte) {
112                 if ((ch = *p1) != *p2) {
113                         if (xflag) {
114                                 dfound = 1;
115                                 (void)printf("%08llx %02x %02x\n",
116                                     (long long)byte - 1, ch, *p2);
117                         } else if (lflag) {
118                                 dfound = 1;
119                                 (void)printf("%6lld %3o %3o\n",
120                                     (long long)byte, ch, *p2);
121                         } else
122                                 diffmsg(file1, file2, byte, line);
123                                 /* NOTREACHED */
124                 }
125                 if (ch == '\n')
126                         ++line;
127                 if (++p1 == e1) {
128                         off1 += MMAP_CHUNK;
129                         if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
130                                 munmap(m2, MMAP_CHUNK);
131                                 err(ERR_EXIT, "remmap %s", file1);
132                         }
133                         e1 = m1 + MMAP_CHUNK;
134                 }
135                 if (++p2 == e2) {
136                         off2 += MMAP_CHUNK;
137                         if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
138                                 munmap(m1, MMAP_CHUNK);
139                                 err(ERR_EXIT, "remmap %s", file2);
140                         }
141                         e2 = m2 + MMAP_CHUNK;
142                 }
143         }
144         munmap(m1, MMAP_CHUNK);
145         munmap(m2, MMAP_CHUNK);
146
147         if (sigaction(SIGSEGV, &oact, NULL))
148                 err(ERR_EXIT, "sigaction()");
149
150         if (len1 != len2)
151                 eofmsg (len1 > len2 ? file2 : file1);
152         if (dfound)
153                 exit(DIFF_EXIT);
154 }
155
156 static u_char *
157 remmap(u_char *mem, int fd, off_t offset)
158 {
159         if (mem != NULL)
160                 munmap(mem, MMAP_CHUNK);
161         mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
162         if (mem == MAP_FAILED)
163                 return (NULL);
164         madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
165         return (mem);
166 }
167
168 static void
169 segv_handler(int sig __unused) {
170         static const char msg[] = "cmp: Input/output error (caught SIGSEGV)\n";
171
172         write(STDERR_FILENO, msg, sizeof(msg));
173         _exit(EXIT_FAILURE);
174 }