From owner-svn-src-user@freebsd.org Mon Dec 26 11:42:34 2016 Return-Path: Delivered-To: svn-src-user@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B9ED9C91867 for ; Mon, 26 Dec 2016 11:42:34 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 94C4A1C89; Mon, 26 Dec 2016 11:42:34 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uBQBgXTb072454; Mon, 26 Dec 2016 11:42:33 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uBQBgXOM072449; Mon, 26 Dec 2016 11:42:33 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201612261142.uBQBgXOM072449@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Mon, 26 Dec 2016 11:42:33 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r310594 - user/bapt/diff X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 26 Dec 2016 11:42:34 -0000 Author: bapt Date: Mon Dec 26 11:42:33 2016 New Revision: 310594 URL: https://svnweb.freebsd.org/changeset/base/310594 Log: Implement diff -l for compatibility with GNU diff Obtained from: soc2012 Modified: user/bapt/diff/Makefile user/bapt/diff/diff.c user/bapt/diff/diff.h user/bapt/diff/diffreg.c Modified: user/bapt/diff/Makefile ============================================================================== --- user/bapt/diff/Makefile Mon Dec 26 11:20:40 2016 (r310593) +++ user/bapt/diff/Makefile Mon Dec 26 11:42:33 2016 (r310594) @@ -3,5 +3,6 @@ PROG= diff SRCS= diff.c diffdir.c diffreg.c xmalloc.c WARNS= 7 +CFLAGS+= -Wno-incompatible-pointer-types-discards-qualifiers .include Modified: user/bapt/diff/diff.c ============================================================================== --- user/bapt/diff/diff.c Mon Dec 26 11:20:40 2016 (r310593) +++ user/bapt/diff/diff.c Mon Dec 26 11:42:33 2016 (r310594) @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$"); #include "diff.h" #include "xmalloc.h" -int Nflag, Pflag, rflag, sflag, Tflag; +int lflag, Nflag, Pflag, rflag, sflag, Tflag; int diff_format, diff_context, status; char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; struct stat stb1, stb2; @@ -57,6 +57,7 @@ static struct option longopts[] = { { "forward-ed", no_argument, 0, 'f' }, { "ignore-matching-lines", required_argument, 0, 'I' }, { "ignore-case", no_argument, 0, 'i' }, + { "paginate", no_argument, NULL, 'l' }, { "label", required_argument, 0, 'L' }, { "new-file", no_argument, 0, 'N' }, { "rcs", no_argument, 0, 'n' }, @@ -154,6 +155,9 @@ main(int argc, char **argv) else usage(); break; + case 'l': + lflag = 1; + break; case 'N': Nflag = 1; break; Modified: user/bapt/diff/diff.h ============================================================================== --- user/bapt/diff/diff.h Mon Dec 26 11:20:40 2016 (r310593) +++ user/bapt/diff/diff.h Mon Dec 26 11:42:33 2016 (r310594) @@ -82,7 +82,7 @@ struct excludes { struct excludes *next; }; -extern int Nflag, Pflag, rflag, sflag, Tflag; +extern int lflag, Nflag, Pflag, rflag, sflag, Tflag; extern int diff_format, diff_context, status; extern char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; extern struct stat stb1, stb2; Modified: user/bapt/diff/diffreg.c ============================================================================== --- user/bapt/diff/diffreg.c Mon Dec 26 11:20:40 2016 (r310593) +++ user/bapt/diff/diffreg.c Mon Dec 26 11:42:33 2016 (r310594) @@ -86,6 +86,8 @@ __FBSDID("$FreeBSD$"); #include "diff.h" #include "xmalloc.h" +#define _PATH_PR "/usr/bin/pr" + #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) @@ -297,6 +299,8 @@ diffreg(char *file1, char *file2, int fl { FILE *f1, *f2; int i, rval; + int ostdout = -1; + pid_t pid = -1; f1 = f2 = NULL; rval = D_SAME; @@ -372,6 +376,43 @@ diffreg(char *file1, char *file2, int fl status |= 1; goto closem; } + if (lflag) { + /* redirect stdout to pr */ + int pfd[2]; + char *header; + char * prargv[] = { "pr", "-h", NULL, "-f", NULL }; + + xasprintf(&header, "%s %s %s", diffargs, file1, file2); + prargv[2] = header; + fflush(stdout); + rewind(stdout); + pipe(pfd); + switch ((pid = fork())) { + case -1: + status |= 2; + free(header); + err(2, "No more processes"); + case 0: + /* child */ + if (pfd[0] != STDIN_FILENO) { + dup2(pfd[0], STDIN_FILENO); + close(pfd[0]); + } + close(pfd[1]); + execv(_PATH_PR, (char *const *)prargv); + _exit(127); + default: + /* parent */ + if (pfd[1] != STDOUT_FILENO) { + ostdout = dup(STDOUT_FILENO); + dup2(pfd[1], STDOUT_FILENO); + close(pfd[1]); + } + close(pfd[0]); + rewind(stdout); + free(header); + } + } prepare(0, f1, stb1.st_size, flags); prepare(1, f2, stb2.st_size, flags); @@ -404,6 +445,20 @@ diffreg(char *file1, char *file2, int fl ixnew = xreallocarray(ixnew, len[1] + 2, sizeof(*ixnew)); check(f1, f2, flags); output(file1, f1, file2, f2, flags); + if (ostdout != -1) { + int wstatus; + + /* close the pipe to pr and restore stdout */ + fflush(stdout); + rewind(stdout); + if (ostdout != STDOUT_FILENO) { + close(STDOUT_FILENO); + dup2(ostdout, STDOUT_FILENO); + close(ostdout); + } + waitpid(pid, &wstatus, 0); + } + closem: if (anychange) { status |= 1;