Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Jul 2012 17:40:58 +0000
From:      jhagewood@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r239847 - in soc2012/jhagewood: diff/diff diff3 sdiff sdiff/sdiff
Message-ID:  <20120727174058.23515106566B@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhagewood
Date: Fri Jul 27 17:40:57 2012
New Revision: 239847
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=239847

Log:
  Added binary support for sdiff

Modified:
  soc2012/jhagewood/diff/diff/diffreg.c
  soc2012/jhagewood/diff3/TODO
  soc2012/jhagewood/sdiff/TODO
  soc2012/jhagewood/sdiff/hagewood-sdiff.patch
  soc2012/jhagewood/sdiff/sdiff/sdiff.c

Modified: soc2012/jhagewood/diff/diff/diffreg.c
==============================================================================
--- soc2012/jhagewood/diff/diff/diffreg.c	Fri Jul 27 16:38:02 2012	(r239846)
+++ soc2012/jhagewood/diff/diff/diffreg.c	Fri Jul 27 17:40:57 2012	(r239847)
@@ -78,7 +78,6 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <limits.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -100,6 +99,7 @@
 # define TIMESPEC_NS(timespec) 0
 #endif
 
+#define MAX_INT	2147483647
 #define MAX_CHECK 768	/* 3 kilobytes of chars. */
 
 /*
@@ -344,8 +344,8 @@
 			if (filebehave == FILE_NORMAL) 
 				f1 = fopen(file1, "r");
 			if (filebehave == FILE_GZIP) {
-				gz1 = gzopen(file1, "r");
-				gzread(gz1, buf, MAX_INT);
+				gz1 = gzdopen(file1, "r");
+				gzdread(gz1, buf, MAX_INT);
 				f1 = fdopen(buf, "r");
 			}
 		}
@@ -372,8 +372,8 @@
 			if (filebehave == FILE_NORMAL) 
 				f2 = fopen(file2, "r");
 			if (filebehave == FILE_GZIP)
-				gz2 = gzopen(file2, "r");
-				gzread(gz2, buf, MAX_INT);
+				gz2 = gzdopen(file2, "r");
+				gzdread(gz2, buf, MAX_INT);
 				f2 = fdopen(buf, "r");
 		}
 	}

Modified: soc2012/jhagewood/diff3/TODO
==============================================================================
--- soc2012/jhagewood/diff3/TODO	Fri Jul 27 16:38:02 2012	(r239846)
+++ soc2012/jhagewood/diff3/TODO	Fri Jul 27 17:40:57 2012	(r239847)
@@ -11,4 +11,4 @@
 - BUG: Goes to usage when argc < 5 FIX: argc < 3
 - BUG: Would not open files correctly FIX: change which argv[] is passed 
 to fopen()
-- BUG: Seg faults. 
+- BUG: Seg faults. FIXED

Modified: soc2012/jhagewood/sdiff/TODO
==============================================================================
--- soc2012/jhagewood/sdiff/TODO	Fri Jul 27 16:38:02 2012	(r239846)
+++ soc2012/jhagewood/sdiff/TODO	Fri Jul 27 17:40:57 2012	(r239847)
@@ -4,7 +4,7 @@
 Fix --width output indention			COMPLETE
 --help						COMPLETE
 Add more information to man file.		COMPLETE
-Fix sdiff to work with binary data		INCOMPLETE
+Fix sdiff to work with binary data		COMPLETE	TODO: Add some error checking
 Replace goto statements				COMPLETE
 
 NOTES:

Modified: soc2012/jhagewood/sdiff/hagewood-sdiff.patch
==============================================================================
--- soc2012/jhagewood/sdiff/hagewood-sdiff.patch	Fri Jul 27 16:38:02 2012	(r239846)
+++ soc2012/jhagewood/sdiff/hagewood-sdiff.patch	Fri Jul 27 17:40:57 2012	(r239847)
@@ -1,6 +1,6 @@
-diff -rupN jhagewood/sdiff/sdiff-orig/common.c jhagewood/sdiff/sdiff/common.c
---- jhagewood/sdiff/sdiff-orig/common.c	2012-07-14 03:47:32.000000000 -0400
-+++ jhagewood/sdiff/sdiff/common.c	2012-07-16 19:27:40.000000000 -0400
+diff -rupN sdiff-orig/common.c sdiff/common.c
+--- sdiff-orig/common.c	2012-07-26 03:11:02.000000000 -0400
++++ sdiff/common.c	2012-07-26 03:11:02.000000000 -0400
 @@ -5,16 +5,24 @@
   * Public domain.
   */
@@ -28,9 +28,9 @@
 -	exit(2);
 +	exit(1);
  }
-diff -rupN jhagewood/sdiff/sdiff-orig/common.h jhagewood/sdiff/sdiff/common.h
---- jhagewood/sdiff/sdiff-orig/common.h	2012-07-14 03:47:32.000000000 -0400
-+++ jhagewood/sdiff/sdiff/common.h	2012-07-16 19:27:30.000000000 -0400
+diff -rupN sdiff-orig/common.h sdiff/common.h
+--- sdiff-orig/common.h	2012-07-26 03:11:02.000000000 -0400
++++ sdiff/common.h	2012-07-26 03:11:02.000000000 -0400
 @@ -5,8 +5,4 @@
   * Public domain.
   */
@@ -41,9 +41,9 @@
 -
 -__dead void cleanup(const char *);
 +void cleanup(const char *) __dead2;
-diff -rupN jhagewood/sdiff/sdiff-orig/edit.c jhagewood/sdiff/sdiff/edit.c
---- jhagewood/sdiff/sdiff-orig/edit.c	2012-07-14 03:47:32.000000000 -0400
-+++ jhagewood/sdiff/sdiff/edit.c	2012-07-16 19:29:10.000000000 -0400
+diff -rupN sdiff-orig/edit.c sdiff/edit.c
+--- sdiff-orig/edit.c	2012-07-26 03:11:02.000000000 -0400
++++ sdiff/edit.c	2012-07-26 03:11:02.000000000 -0400
 @@ -4,6 +4,14 @@
   * Written by Raymond Lai <ray@cyth.net>.
   * Public domain.
@@ -59,9 +59,9 @@
  
  #include <sys/types.h>
  #include <sys/wait.h>
-diff -rupN jhagewood/sdiff/sdiff-orig/sdiff.1 jhagewood/sdiff/sdiff/sdiff.1
---- jhagewood/sdiff/sdiff-orig/sdiff.1	2012-07-14 03:47:32.000000000 -0400
-+++ jhagewood/sdiff/sdiff/sdiff.1	2012-07-14 03:47:32.000000000 -0400
+diff -rupN sdiff-orig/sdiff.1 sdiff/sdiff.1
+--- sdiff-orig/sdiff.1	2012-07-26 03:11:02.000000000 -0400
++++ sdiff/sdiff.1	2012-07-26 03:11:02.000000000 -0400
 @@ -1,9 +1,10 @@
 +.\" $FreeBSD$
  .\" $OpenBSD: sdiff.1,v 1.15 2007/06/29 14:48:07 jmc Exp $
@@ -171,9 +171,9 @@
 -.Nm
 -may not work with binary data.
 +
-diff -rupN jhagewood/sdiff/sdiff-orig/sdiff.c jhagewood/sdiff/sdiff/sdiff.c
---- jhagewood/sdiff/sdiff-orig/sdiff.c	2012-07-14 03:47:32.000000000 -0400
-+++ jhagewood/sdiff/sdiff/sdiff.c	2012-07-16 19:26:27.000000000 -0400
+diff -rupN sdiff-orig/sdiff.c sdiff/sdiff.c
+--- sdiff-orig/sdiff.c	2012-07-26 03:11:02.000000000 -0400
++++ sdiff/sdiff.c	2012-07-27 04:03:20.000000000 -0400
 @@ -5,6 +5,14 @@
   * Public domain.
   */
@@ -189,7 +189,26 @@
  #include <sys/param.h>
  #include <sys/queue.h>
  #include <sys/stat.h>
-@@ -62,81 +70,108 @@ static void printd(FILE *, size_t);
+@@ -41,6 +49,9 @@
+  */
+ #define WIDTH_MIN 5
+ 
++/* 3 kilobytes of chars */
++#define MAX_CHECK 768
++
+ /* A single diff line. */
+ struct diffline {
+ 	SIMPLEQ_ENTRY(diffline) diffentries;
+@@ -52,6 +63,8 @@ struct diffline {
+ static void astrcat(char **, const char *);
+ static void enqueue(char *, char, char *);
+ static char *mktmpcpy(const char *);
++static int istextfile(FILE *);
++static void binexec(char *, char *, char *); __dead2;
+ static void freediff(struct diffline *);
+ static void int_usage(void);
+ static int parsecmd(FILE *, FILE *, FILE *);
+@@ -62,81 +75,108 @@ static void printd(FILE *, size_t);
  static void println(const char *, const char, const char *);
  static void processq(void);
  static void prompt(const char *, const char *);
@@ -327,7 +346,7 @@
 +	{ "ignore-all-space",           no_argument,            NULL,   'W' },   
 +         
 +	{ NULL,                         0,                      NULL,   '\0'}
-+};
+ };
 +
 +static const char *help_msg[] = { 
 +	"\nusage: sdiff [-abdilstW] [-I regexp] [-o outfile] [-w width] file1 file2\n",
@@ -350,13 +369,13 @@
 +	"\t\t--tabsize NUM, Change size of tabs (default 8.)",
 +	
 +	NULL,
- };
++};
 +char **help_strs = (char **)help_msg;
 +
  /*
   * Create temporary file if source_file is not a regular file.
   * Returns temporary file name if one was malloced, NULL if unnecessary.
-@@ -175,7 +210,7 @@ mktmpcpy(const char *source_file)
+@@ -175,7 +215,7 @@ mktmpcpy(const char *source_file)
  		err(2, "asprintf");
  	if ((ofd = mkstemp(target_file)) == -1) {
  		warn("error opening %s", target_file);
@@ -365,7 +384,7 @@
  	}
  	while ((rcount = read(ifd, buf, sizeof(buf))) != -1 &&
  	    rcount != 0) {
-@@ -184,22 +219,18 @@ mktmpcpy(const char *source_file)
+@@ -184,22 +224,18 @@ mktmpcpy(const char *source_file)
  		wcount = write(ofd, buf, (size_t)rcount);
  		if (-1 == wcount || rcount != wcount) {
  			warn("error writing to %s", target_file);
@@ -390,7 +409,16 @@
  }
  
  int
-@@ -240,18 +271,13 @@ main(int argc, char **argv)
+@@ -211,7 +247,7 @@ main(int argc, char **argv)
+ 	pid_t pid=0; pid_t ppid =-1;
+ 	const char *outfile = NULL;
+ 	struct option *popt;
+-	char **diffargv, *diffprog = "diff", *filename1, *filename2,
++	char **diffargv, *diffprog = "/usr/bin/diff", *filename1, *filename2,
+ 	    *tmp1, *tmp2, *s1, *s2;
+ 
+ 	/*
+@@ -240,18 +276,13 @@ main(int argc, char **argv)
  		const char *errstr;
  
  		switch (ch) {
@@ -409,7 +437,7 @@
  		/* combine no-arg single switches */
  		case 'a':
  		case 'B':
-@@ -261,11 +287,17 @@ main(int argc, char **argv)
+@@ -261,11 +292,17 @@ main(int argc, char **argv)
  		case 'i':
  		case 't':
  		case 'H':
@@ -430,7 +458,7 @@
  		case DIFFPROG_OPT:
  			diffargv[0] = diffprog = optarg;
  			break;
-@@ -289,32 +321,27 @@ main(int argc, char **argv)
+@@ -289,32 +326,27 @@ main(int argc, char **argv)
  			if (errstr)
  				errx(2, "width is %s: %s", errstr, optarg);
  			break;
@@ -470,7 +498,7 @@
  			diffargv[i] = diffargv[i+1];
  		}
  		diffargv[diffargc-1] = NULL;
-@@ -362,19 +389,18 @@ main(int argc, char **argv)
+@@ -362,19 +394,18 @@ main(int argc, char **argv)
  	/* Add NULL to end of array to indicate end of array. */
  	diffargv[diffargc++] = NULL;
  
@@ -495,7 +523,7 @@
  		case 0:
  			/* child */
  			/* We don't read from the pipe. */
-@@ -383,7 +409,6 @@ main(int argc, char **argv)
+@@ -383,7 +414,6 @@ main(int argc, char **argv)
  				err(2, "child could not duplicate descriptor");
  			/* Free unused descriptor. */
  			close(fd[1]);
@@ -503,7 +531,64 @@
  			execvp(diffprog, diffargv);
  			err(2, "could not execute diff: %s", diffprog);
  			break;
-@@ -461,6 +486,7 @@ main(int argc, char **argv)
+@@ -406,6 +436,16 @@ main(int argc, char **argv)
+ 	if ((file2 = fopen(filename2, "r")) == NULL)
+ 		err(2, "could not open %s", filename2);
+ 
++	if (!istextfile(file1) || !istextfile(file2)) {
++		fclose(file1);
++		fclose(file2);
++		fclose(diffpipe);
++		unlink(tmp1);
++		unlink(tmp2);
++		free(tmp1);
++		free(tmp2);
++		binexec(diffprog, filename1, filename2);
++	}
+ 	/* Line numbers start at one. */
+ 	file1ln = file2ln = 1;
+ 
+@@ -452,6 +492,39 @@ main(int argc, char **argv)
+ 	return (WEXITSTATUS(status));
+ }
+ 
++static void
++binexec(char *diffprog, char *f1, char *f2)
++{
++	
++	char *args[] = {diffprog, f1, f2, (char *) 0};
++	
++	execv(diffprog, args);
++	printf("Could not execute diff process.\n");
++	exit(1);
++}
++/*
++ * Checks whether a file appears to be a text file. 
++ */
++static int
++istextfile(FILE *f)
++{
++	int	i;
++	char ch;
++
++	if (f == NULL)
++		return (1);
++	rewind(f);
++	for (i = 0; i <= MAX_CHECK || ch != EOF; i++) {
++		ch = fgetc(f);
++		if (ch == '\0') {
++			rewind(f);
++			return (0);
++		}
++	}
++	rewind(f);
++	return (1);
++}
++
+ /*
+  * Prints an individual column (left or right), taking into account
+  * that tabs are variable-width.  Takes a string, the current column
+@@ -461,6 +534,7 @@ main(int argc, char **argv)
  static void
  printcol(const char *s, size_t *col, const size_t col_max)
  {
@@ -511,7 +596,7 @@
  	for (; *s && *col < col_max; ++s) {
  		size_t new_col;
  
-@@ -484,11 +510,9 @@ printcol(const char *s, size_t *col, con
+@@ -484,11 +558,9 @@ printcol(const char *s, size_t *col, con
  				return;
  			*col = new_col;
  			break;
@@ -523,7 +608,7 @@
  		putchar(*s);
  	}
  }
-@@ -512,56 +536,47 @@ prompt(const char *s1, const char *s2)
+@@ -512,56 +584,47 @@ prompt(const char *s1, const char *s2)
  		/* Skip leading whitespace. */
  		for (p = cmd; isspace(*p); ++p)
  			;
@@ -589,7 +674,7 @@
  		free(cmd);
  		return;
  	}
-@@ -570,7 +585,7 @@ PROMPT:
+@@ -570,7 +633,7 @@ PROMPT:
  	 * If there was no error, we received an EOF from stdin, so we
  	 * should quit.
  	 */
@@ -598,7 +683,7 @@
  	fclose(outfp);
  	exit(0);
  }
-@@ -678,7 +693,6 @@ parsecmd(FILE *diffpipe, FILE *file1, FI
+@@ -678,7 +741,6 @@ parsecmd(FILE *diffpipe, FILE *file1, FI
  
  	/* A range is specified for file1. */
  	if (c == ',') {
@@ -606,7 +691,7 @@
  		q = p;
  		/* Go to character after file2end. */
  		while (isdigit(*p))
-@@ -690,7 +704,6 @@ parsecmd(FILE *diffpipe, FILE *file1, FI
+@@ -690,7 +752,6 @@ parsecmd(FILE *diffpipe, FILE *file1, FI
  			errx(2, "file1 end is %s: %s", errstr, line);
  		if (file1start > file1end)
  			errx(2, "invalid line range in file1: %s", line);
@@ -614,7 +699,7 @@
  	} else
  		file1end = file1start;
  
-@@ -809,17 +822,14 @@ parsecmd(FILE *diffpipe, FILE *file1, FI
+@@ -809,17 +870,14 @@ parsecmd(FILE *diffpipe, FILE *file1, FI
  		printa(file2, file2end);
  		n = file2end - file2start + 1;
  		break;
@@ -632,7 +717,7 @@
  	default:
  		errx(2, "invalid diff command: %c: %s", cmd, line);
  	}
-@@ -854,6 +864,7 @@ enqueue(char *left, char div, char *righ
+@@ -854,6 +912,7 @@ enqueue(char *left, char div, char *righ
  static void
  freediff(struct diffline *diffp)
  {
@@ -640,7 +725,7 @@
  	free(diffp->left);
  	free(diffp->right);
  	free(diffp);
-@@ -876,7 +887,6 @@ astrcat(char **s, const char *append)
+@@ -876,7 +935,6 @@ astrcat(char **s, const char *append)
  	static const char *oldstr = NULL;
  	char *newstr;
  
@@ -648,7 +733,7 @@
  	/*
  	 * First string is NULL, so just copy append.
  	 */
-@@ -1002,7 +1012,6 @@ printa(FILE *file, size_t line2)
+@@ -1002,7 +1060,6 @@ printa(FILE *file, size_t line2)
  			errx(2, "append ended early");
  		enqueue(NULL, '>', line);
  	}
@@ -656,7 +741,7 @@
  	processq();
  }
  
-@@ -1103,24 +1112,35 @@ printd(FILE *file1, size_t file1end)
+@@ -1103,24 +1160,35 @@ printd(FILE *file1, size_t file1end)
  static void
  int_usage(void)
  {

Modified: soc2012/jhagewood/sdiff/sdiff/sdiff.c
==============================================================================
--- soc2012/jhagewood/sdiff/sdiff/sdiff.c	Fri Jul 27 16:38:02 2012	(r239846)
+++ soc2012/jhagewood/sdiff/sdiff/sdiff.c	Fri Jul 27 17:40:57 2012	(r239847)
@@ -49,6 +49,9 @@
  */
 #define WIDTH_MIN 5
 
+/* 3 kilobytes of chars */
+#define MAX_CHECK 768
+
 /* A single diff line. */
 struct diffline {
 	SIMPLEQ_ENTRY(diffline) diffentries;
@@ -60,6 +63,8 @@
 static void astrcat(char **, const char *);
 static void enqueue(char *, char, char *);
 static char *mktmpcpy(const char *);
+static int istextfile(FILE *);
+static void binexec(char *, char *, char *); __dead2;
 static void freediff(struct diffline *);
 static void int_usage(void);
 static int parsecmd(FILE *, FILE *, FILE *);
@@ -242,7 +247,7 @@
 	pid_t pid=0; pid_t ppid =-1;
 	const char *outfile = NULL;
 	struct option *popt;
-	char **diffargv, *diffprog = "diff", *filename1, *filename2,
+	char **diffargv, *diffprog = "/usr/bin/diff", *filename1, *filename2,
 	    *tmp1, *tmp2, *s1, *s2;
 
 	/*
@@ -431,6 +436,16 @@
 	if ((file2 = fopen(filename2, "r")) == NULL)
 		err(2, "could not open %s", filename2);
 
+	if (!istextfile(file1) || !istextfile(file2)) {
+		fclose(file1);
+		fclose(file2);
+		fclose(diffpipe);
+		unlink(tmp1);
+		unlink(tmp2);
+		free(tmp1);
+		free(tmp2);
+		binexec(diffprog, filename1, filename2);
+	}
 	/* Line numbers start at one. */
 	file1ln = file2ln = 1;
 
@@ -477,6 +492,39 @@
 	return (WEXITSTATUS(status));
 }
 
+static void
+binexec(char *diffprog, char *f1, char *f2)
+{
+	
+	char *args[] = {diffprog, f1, f2, (char *) 0};
+	
+	execv(diffprog, args);
+	printf("Could not execute diff process.\n");
+	exit(1);
+}
+/*
+ * Checks whether a file appears to be a text file. 
+ */
+static int
+istextfile(FILE *f)
+{
+	int	i;
+	char ch;
+
+	if (f == NULL)
+		return (1);
+	rewind(f);
+	for (i = 0; i <= MAX_CHECK || ch != EOF; i++) {
+		ch = fgetc(f);
+		if (ch == '\0') {
+			rewind(f);
+			return (0);
+		}
+	}
+	rewind(f);
+	return (1);
+}
+
 /*
  * Prints an individual column (left or right), taking into account
  * that tabs are variable-width.  Takes a string, the current column



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120727174058.23515106566B>