Index: etc/mtree/BSD.tests.dist =================================================================== --- etc/mtree/BSD.tests.dist +++ etc/mtree/BSD.tests.dist @@ -994,6 +994,8 @@ .. diff .. + diff3 + .. dirname .. du Index: usr.bin/diff3/diff3.1 =================================================================== --- usr.bin/diff3/diff3.1 +++ usr.bin/diff3/diff3.1 @@ -38,7 +38,12 @@ .Nd 3-way differential file comparison .Sh SYNOPSIS .Nm diff3 -.Op Fl 3aEeXx +.Op Fl 3aEeiXx +.Op Fl Fl diff-program Ar program +.Op Fl Fl strip-trailing-cr +.Op Fl L | Fl Fl label Ar label1 +.Op Fl L | Fl Fl label Ar label2 +.Op Fl L | Fl Fl label Ar label3 .Ar file1 file2 file3 .Sh DESCRIPTION The @@ -60,15 +65,16 @@ .Pp The options are as follows: .Bl -tag -width "-E, -X" -.It Fl 3 +.It Fl 3 , Fl Fl easy-only Produces an output script suitable for .Xr ed 1 with changes specific only to .Ar file3 . -.It Fl a +.It Fl a , Fl Fl text Treat all files as ASCII. -.It Fl E , X +.It Fl E , Fl Fl show-overlap +.It Fl X Similar to .Fl e and @@ -77,7 +83,7 @@ be noted with ==== in the normal listing) differently. The overlapping lines from both files will be inserted by the edit script, bracketed by "<<<<<<" and ">>>>>>" lines. -.It Fl e +.It Fl e , Fl Fl ed Produces output in a form suitable as an input script for the .Xr ed 1 utility. @@ -99,11 +105,29 @@ It is useful for backing out changes specific to .Ar file2 only. -.It Fl x +.It Fl i +Appends 'w' and 'q' +.Xr ed 1 +commands. +.It Fl L , Fl Fl label +Defines labels to print instead of file names +.Ar file1 , +.Ar file2 +and +.Ar file3 . +.It Fl x, Fl Fl overlap-only Produces an output script suitable for .Xr ed 1 with changes specific only to all three versions. +.It Fl Fl diff-program Ar program +Use +.Ar program +instead of the default +.Xr diff 1 +to compare files. +.It Fl Fl strip-trailing-cr +Strip trailing carriage return on input files. .El .Pp The Index: usr.bin/diff3/diff3.c =================================================================== --- usr.bin/diff3/diff3.c +++ usr.bin/diff3/diff3.c @@ -126,7 +126,7 @@ */ static int last[4]; static int Aflag, eflag, iflag, mflag, Tflag; -static int oflag; /* indicates whether to mark overlaps (-E or -X)*/ +static int oflag; /* indicates whether to mark overlaps (-E or -X) */ static int strip_cr; static char *f1mark, *f2mark, *f3mark; @@ -170,9 +170,9 @@ static void usage(void) { - fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L lable1] [-L label2] " - "[ -L label3] file1 file2 file3\n"); - exit (2); + fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L label1] [-L label2] " + "[-L label3] file1 file2 file3\n"); + exit(2); } static int @@ -186,7 +186,7 @@ f = fdopen(fd, "r"); if (f == NULL) err(2, "fdopen"); - for (i=0; (p = getchange(f)); i++) { + for (i = 0; (p = getchange(f)); i++) { if (i >= szchanges - 1) increase(); a = b = (int)strtoimax(p, &p, 10); @@ -196,7 +196,7 @@ } kind = *p++; c = d = (int)strtoimax(p, &p, 10); - if (*p==',') { + if (*p == ',') { p++; d = (int)strtoimax(p, &p, 10); } @@ -212,8 +212,8 @@ (*dd)[i].new.to = d; } if (i) { - (*dd)[i].old.from = (*dd)[i-1].old.to; - (*dd)[i].new.from = (*dd)[i-1].new.to; + (*dd)[i].old.from = (*dd)[i - 1].old.to; + (*dd)[i].new.from = (*dd)[i - 1].new.to; } fclose(f); return (i); @@ -222,9 +222,9 @@ static int diffexec(const char *diffprog, char **diffargv, int fd[]) { - int pid, pd; + int pd; - switch (pid = pdfork(&pd, PD_CLOEXEC)) { + switch (pdfork(&pd, PD_CLOEXEC)) { case 0: close(fd[0]); if (dup2(fd[1], STDOUT_FILENO) == -1) @@ -264,6 +264,12 @@ if ((len = getline(&buf, &bufsize, b)) < 0) return (NULL); + if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) { + buf[len - 2] = '\n'; + buf[len - 1] = '\0'; + len--; + } + if (n != NULL) *n = len; @@ -396,7 +402,7 @@ printf("%da\n", rold->from - 1); else { printf("%d", rold->from); - if (rold->to > rold->from+1) + if (rold->to > rold->from + 1) printf(",%d", rold->to - 1); printf("c\n"); } @@ -433,7 +439,7 @@ if ((line = get_line(fp[i], &j)) == NULL) errx(EXIT_FAILURE, "logic error"); if (pr != NULL) - printf("%s%s", Tflag == 1? "\t" : pr, line); + printf("%s%s", Tflag == 1 ? "\t" : pr, line); cline[i]++; } return ((int) n); @@ -455,13 +461,13 @@ skip(0, r1->from, NULL); skip(1, r2->from, NULL); nchar = 0; - for (nline=0; nline < r1->to - r1->from; nline++) { + for (nline = 0; nline < r1->to - r1->from; nline++) { do { c = getc(fp[0]); d = getc(fp[1]); if (c == -1 && d == -1) break; - if (c == -1 || d== -1) + if (c == -1 || d == -1) errx(EXIT_FAILURE, "logic error"); nchar++; if (c != d) { @@ -482,6 +488,7 @@ for (i = 0; i < 2; i++) (void)fseek(fp[i], (long)-nchar, SEEK_CUR); } + /* * collect an editing script for later regurgitation */ @@ -514,7 +521,7 @@ if (!oflag || !overlap[n]) { prange(&de[n].old); } else { - printf("%da\n", de[n].old.to -1); + printf("%da\n", de[n].old.to - 1); if (Aflag) { printf("%s\n", f2mark); fseek(fp[1], de[n].old.from, SEEK_SET); @@ -529,8 +536,9 @@ printf("=======\n"); } fseek(fp[2], (long)de[n].new.from, SEEK_SET); - for (k = de[n].new.to - de[n].new.from; k > 0; k-= j) { + for (k = de[n].new.to - de[n].new.from; k > 0; k -= j) { size_t r; + j = k > BUFSIZ ? BUFSIZ : k; r = fread(block, 1, j, fp[2]); if (r == 0) { @@ -597,7 +605,7 @@ char *labels[] = { NULL, NULL, NULL }; const char *diffprog = DIFF_PATH; char *file1, *file2, *file3; - char *diffargv[6]; + char *diffargv[7]; int diffargc = 0; int fd13[2], fd23[2]; int pd13, pd23; @@ -655,6 +663,7 @@ break; case STRIPCR_OPT: strip_cr = 1; + diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr"); break; } } @@ -759,7 +768,7 @@ for (i = 0; i < nke; i++) { status = e[i].data; if (WIFEXITED(status) && WEXITSTATUS(status) >= 2) - errx(2, "diff exited abormally"); + errx(2, "diff exited abnormally"); else if (WIFSIGNALED(status)) errx(2, "diff killed by signal %d", WTERMSIG(status)); Index: usr.bin/diff3/tests/1cr.txt =================================================================== --- /dev/null +++ usr.bin/diff3/tests/1cr.txt @@ -0,0 +1,6 @@ +This is a more complete test +which multiple lines +and a few typ0s to fix +also I plan to add few lines + and to remove +other lines Index: usr.bin/diff3/tests/Makefile =================================================================== --- usr.bin/diff3/tests/Makefile +++ usr.bin/diff3/tests/Makefile @@ -6,6 +6,7 @@ ${PACKAGE}FILES+= \ 1.txt \ + 1cr.txt \ 2.txt \ 3.txt \ 1.out \ Index: usr.bin/diff3/tests/diff3_test.sh =================================================================== --- usr.bin/diff3/tests/diff3_test.sh +++ usr.bin/diff3/tests/diff3_test.sh @@ -8,6 +8,9 @@ atf_check -o file:$(atf_get_srcdir)/1.out \ diff3 $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt + atf_check -o file:$(atf_get_srcdir)/1.out \ + diff3 --strip-trailing-cr $(atf_get_srcdir)/1cr.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt + atf_check -o file:$(atf_get_srcdir)/2.out \ diff3 -e $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt