1 | /* $NetBSD: regular.c,v 1.20 2006/06/03 21:47:55 christos Exp $ */
|
---|
2 |
|
---|
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 | /*#include <sys/cdefs.h>*/
|
---|
33 | #ifndef lint
|
---|
34 | /*#if 0
|
---|
35 | static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94";
|
---|
36 | #else
|
---|
37 | __RCSID("$NetBSD: regular.c,v 1.20 2006/06/03 21:47:55 christos Exp $");
|
---|
38 | #endif*/
|
---|
39 | #endif /* not lint */
|
---|
40 |
|
---|
41 | #include <sys/stat.h>
|
---|
42 |
|
---|
43 | #include "err.h"
|
---|
44 | #include <limits.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <string.h>
|
---|
48 |
|
---|
49 | #include "cmp_extern.h"
|
---|
50 |
|
---|
51 | static int
|
---|
52 | c_regular(int fd1, char *file1, off_t skip1, off_t len1,
|
---|
53 | int fd2, char *file2, off_t skip2, off_t len2)
|
---|
54 | {
|
---|
55 | unsigned char ch, *p1, *p2, *b1 = 0, *b2 = 0;
|
---|
56 | off_t byte, length, line, bytes_read;
|
---|
57 | int dfound;
|
---|
58 | size_t blk_sz, blk_cnt;
|
---|
59 |
|
---|
60 | if (sflag && len1 != len2)
|
---|
61 | return(1);
|
---|
62 |
|
---|
63 | if (skip1 > len1)
|
---|
64 | return eofmsg(file1, len1 + 1, 0);
|
---|
65 | len1 -= skip1;
|
---|
66 | if (skip2 > len2)
|
---|
67 | return eofmsg(file2, len2 + 1, 0);
|
---|
68 | len2 -= skip2;
|
---|
69 |
|
---|
70 | if (skip1 && lseek(fd1, skip1, SEEK_SET) < 0)
|
---|
71 | goto l_special;
|
---|
72 | if (skip2 && lseek(fd2, skip2, SEEK_SET) < 0) {
|
---|
73 | if (skip1 && lseek(fd1, 0, SEEK_SET) < 0)
|
---|
74 | return err(1, "seek failed");
|
---|
75 | goto l_special;
|
---|
76 | }
|
---|
77 |
|
---|
78 | #define CMP_BUF_SIZE (128*1024)
|
---|
79 |
|
---|
80 | b1 = malloc(CMP_BUF_SIZE);
|
---|
81 | b2 = malloc(CMP_BUF_SIZE);
|
---|
82 | if (!b1 || !b2)
|
---|
83 | goto l_malloc_failed;
|
---|
84 |
|
---|
85 | byte = line = 1;
|
---|
86 | dfound = 0;
|
---|
87 | length = len1;
|
---|
88 | if (length > len2)
|
---|
89 | length = len2;
|
---|
90 | for (blk_sz = CMP_BUF_SIZE; length != 0; length -= blk_sz) {
|
---|
91 | if ((off_t)blk_sz > length)
|
---|
92 | blk_sz = length;
|
---|
93 |
|
---|
94 | bytes_read = read(fd1, b1, blk_sz);
|
---|
95 | if (bytes_read != blk_sz)
|
---|
96 | goto l_read_error;
|
---|
97 |
|
---|
98 | bytes_read = read(fd2, b2, blk_sz);
|
---|
99 | if (bytes_read != blk_sz)
|
---|
100 | goto l_read_error;
|
---|
101 |
|
---|
102 | blk_cnt = blk_sz;
|
---|
103 | p1 = b1;
|
---|
104 | p2 = b2;
|
---|
105 | for (; blk_cnt--; ++p1, ++p2, ++byte) {
|
---|
106 | if ((ch = *p1) != *p2) {
|
---|
107 | if (!lflag) {
|
---|
108 | free(b1);
|
---|
109 | free(b2);
|
---|
110 | return diffmsg(file1, file2, byte, line);
|
---|
111 | }
|
---|
112 | dfound = 1;
|
---|
113 | (void)printf("%6lld %3o %3o\n",
|
---|
114 | (long long)byte, ch, *p2);
|
---|
115 | }
|
---|
116 | if (ch == '\n')
|
---|
117 | ++line;
|
---|
118 | }
|
---|
119 | skip1 += blk_sz;
|
---|
120 | skip2 += blk_sz;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (len1 != len2)
|
---|
124 | return eofmsg(len1 > len2 ? file2 : file1, byte, line);
|
---|
125 | if (dfound)
|
---|
126 | return(DIFF_EXIT);
|
---|
127 | return(0);
|
---|
128 |
|
---|
129 | l_read_error:
|
---|
130 | if ( lseek(fd1, 0, SEEK_SET) < 0
|
---|
131 | || lseek(fd2, 0, SEEK_SET) < 0) {
|
---|
132 | err(1, "seek failed");
|
---|
133 | free(b1);
|
---|
134 | free(b2);
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 | l_malloc_failed:
|
---|
138 | free(b1);
|
---|
139 | free(b2);
|
---|
140 | l_special:
|
---|
141 | return c_special(fd1, file1, skip1, fd2, file2, skip2);
|
---|
142 | }
|
---|
143 |
|
---|