diff3prog.c revision b369f4b871a39ef94e220443957975f445f52eb6
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#
/*
* diff3 - 3-way differential file comparison
*/
/*
* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
*
* d13 = diff report on f1 vs f3
* d23 = diff report on f2 vs f3
* f1, f2, f3 the 3 files
* if changes in f1 overlap with changes in f3, m1 and m3 are used
* to mark the overlaps; otherwise, the file names f1 and f3 are used
* (only for options E and X).
*/
/*
* from is first in range of changed lines
* to is last+1
* from = to = line after point of insertion
* for added lines
*/
#define NC 4096
/*
* de is used to gather editing scripts,
* that are later spewed out in reverse order.
* its first element must be all zero
* the "new" component of de contains line positions
* or byte positions depending on when you look(!?)
*/
/*
* array overlap indicates which sections in de correspond to
* lines that are different in all three files.
*/
static int overlapcnt = 0;
/*
* the number of the last-read line in each file
* is kept in cline[0-2]
*/
static int cline[3];
/*
* the latest known correspondence between line
* numbers of the 3 files is stored in last[1-3]
*/
static int last[4];
static int eflag;
static int oflag; /* indicates whether to mark overlaps (-E or -X) */
static int debug = 0;
/* markers for -E and -X: */
/* Need space for "<<<<<<< " or ">>>>>>> " plus filename */
static int save_err; /* saves errno */
static int digit(int c);
static void separate(char *s);
static void trouble();
static void edscript(int n);
static void usage();
int
{
int i, m, n;
eflag = 0;
oflag = 0;
case 'e':
eflag = 3;
break;
case '3':
eflag = 2;
break;
case 'x':
eflag = 1;
break;
case 'E':
eflag = 3;
oflag = 1;
break;
case 'X':
break;
default:
usage();
break;
}
argv++;
argc--;
}
if (argc < 6)
usage();
if (oflag) {
}
for (i = 0; i <= 2; i++)
argv[i+3]);
perror("");
exit(1);
}
merge(m, n);
return (0);
}
/*
* pick up the line numbers of all changes from
* one change file
* (this puts the numbers in a vector, which is not
* strictly necessary, since the vector is processed
* in one sequential pass. The vector could be optimized
* out of existence)
*/
static int
{
int i;
int a, b, c, d;
char kind;
char *p;
perror("");
exit(1);
}
if (i >= NC) {
exit(0);
}
p = line;
a = b = number(&p);
if (*p == ',') {
p++;
b = number(&p);
}
kind = *p++;
c = d = number(&p);
if (*p == ',') {
p++;
d = number(&p);
}
if (kind == 'a')
a++;
if (kind == 'd')
c++;
b++;
d++;
}
return (i);
}
static int
{
int nn;
nn = 0;
return (nn);
}
static int
digit(int c)
{
return (c >= '0' && c <= '9');
}
static int
{
while (getline(b))
return (1);
return (0);
}
static int
{
int i, c;
for (i = 0; i < sizeof (line)-1; i++) {
c = getc(b);
if (c == EOF) {
line[i] = 0;
return (i);
}
line[i] = c;
if (c == '\n') {
line[++i] = 0;
return (i);
}
}
return (0);
}
static void
{
int dup;
int j;
j = 0;
if (debug) {
(void) printf("%d,%d=%d,%d %d,%d=%d,%d\n",
}
/* first file is different from others */
/* stuff peculiar to 1st file */
if (eflag == 0) {
separate("1");
}
d1++;
continue;
}
/* second file is different from others */
if (eflag == 0) {
separate("2");
}
d2++;
continue;
}
/*
* merge overlapping changes in first file
* this happens after extension see below
*/
d1++;
continue;
}
/* merge overlapping changes in second */
d2++;
continue;
}
/* stuff peculiar to third file or different in all */
/*
* dup = 0 means all files differ
* dup = 1 meands files 1&2 identical
*/
if (eflag == 0) {
} else
d1++;
d2++;
continue;
}
/*
* overlapping changes from file1 & 2
* extend changes appropriately to
* make them coincide
*/
}
}
}
if (eflag) {
edscript(j);
if (j)
(void) printf("w\nq\n");
}
}
static void
separate(char *s)
{
(void) printf("====%s\n", s);
}
/*
* the range of ines rold.from thru rold.to in file i
* is to be changed. it is to be printed only if
* it does not duplicate something to be printed later
*/
static void
{
(void) printf("%d:", i);
if (dup)
return;
if (debug)
return;
i--;
}
/*
* print the range of line numbers, rold.from thru rold.to
* as n1, n2 or n1
*/
static void
{
else {
(void) printf("c\n");
}
}
/*
* no difference was reported by diff between file 1(or 2)
* and file 3, and an artificial dummy difference (trange)
* must be ginned up to correspond to the change reported
* in the other file
*/
static void
{
int delta;
}
/*
* skip to just befor line number from in file i
* if "pr" is nonzero, print all skipped stuff
* with string pr as a prefix
*/
static int
{
int j, n;
trouble();
if (pr)
cline[i]++;
}
return (n);
}
/*
* return 1 or 0 according as the old range
* (in file 1) contains exactly the same data
* as the new range (in file 2)
*/
static int
{
int c, d;
int nchar;
int nline;
return (0);
nchar = 0;
do {
if (c == -1 || d == -1)
trouble();
nchar++;
if (c != d) {
return (0);
}
} while (c != '\n');
}
return (1);
}
static void
{
int i;
for (i = 0; i < 2; i++)
}
static void
trouble()
{
abort();
}
/*
* collect an editing script for later regurgitation
*/
static int
{
return (j);
j++;
if (!dup) overlapcnt++;
return (j);
}
/* regurgitate */
static void
edscript(int n)
{
int j, k;
for (n = n; n > 0; n--) {
else
trouble();
}
(void) printf(".\n");
else {
}
}
}
static void
usage()
{
"\tusage: diff3prog [-ex3EX] d13 d23 f1 f2 f3 [m1 m2]\n");
exit(1);
}