2013-05-18  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

	* tools/tiffgt.c [__DJGPP__]: Include header for snprintf declaration.

	* tools/tiff2pdf.c [__DJGPP__]: Include header for snprintf declaration.

	* tools/ppm2tiff.c [_O_BINARY]: Define _O_BINARY as O_BINARY, _fileno as
	_fileno and _setmode as setmode.
	[__DJGPP__]: Define SET_BINARY to have interruptible terminale reads
	and writes; include <unistd.h> for isatty declaration and
	<sys/exceptn.h> for __djgpp_set_ctrl_c declaration.
	(main): Use SET_BINARY instead of setmode() to reenable SIGINT and
	SIGQUIT if stdin and/or stdout is connected to console and has been
	switched to binary mode.

	* tools/fax2ps.c [_O_BINARY]: Define _O_BINARY as O_BINARY, _fileno as
	_fileno and _setmode as setmode.
	[__DJGPP__]: Define SET_BINARY to have interruptible terminale reads
	and writes; include <unistd.h> for isatty declaration and
	<sys/exceptn.h> for __djgpp_set_ctrl_c declaration.
	(main): Use SET_BINARY instead of setmode() to reenable SIGINT and
	SIGQUIT if stdin and/or stdout is connected to console and has been
	switched to binary mode.

	* libtiff/tiffiop.h [HAVE_SEARCH_H]: New guard HAVE_LFIND added.
	Do not assume that <search.h> provides lfind().

	* libtiff/tif_jpeg.c (JPEGCleanup, JPEGPrintDir): Assertion did not
	hold true, resulting in the client application calling abort().

	* contrib/iptcutil/iptcutil.c [__DJGPP__]: Include <sys/exceptn.h> for
	__djgpp_set_ctrl_c declaration and <unistd.h> for isatty declaration.
	Define SET_BINARY macro.
	(main): Reenable SIGINT and SIGQUIT if stdin and/or stdout is connected
	to console and has been switched to binary mode.






diff -aprNU5 tiff-4.0.3.orig/contrib/iptcutil/iptcutil.c tiff-4.0.3/contrib/iptcutil/iptcutil.c
--- tiff-4.0.3.orig/contrib/iptcutil/iptcutil.c	2011-05-31 14:33:06 +0000
+++ tiff-4.0.3/contrib/iptcutil/iptcutil.c	2013-05-18 00:15:34 +0000
@@ -11,10 +11,43 @@
 # include <strings.h>
 #endif
 
 #ifdef HAVE_IO_H
 # include <io.h>
+# ifdef _O_BINARY
+#  define _O_BINARY       O_BINARY
+#  define _fileno(f)      fileno(f)
+#  define _setmode(f, m)  setmode(f, m)
+# endif /* __O_BINARY */
+# ifdef __DJGPP__
+#  include <unistd.h>       /* to declare isatty() */
+#  include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+   /* This is DJGPP-specific.  By default, switching console
+      to binary mode disables SIGINT and SIGQUIT.  But it
+      is preferable to have terminal reads and writes to
+      be interruptible.  */
+#  define SET_BINARY(f)                                          \
+   (__extension__                                                \
+     ({                                                          \
+        int file_descriptor = fileno(f);                         \
+        int previous_mode = setmode(file_descriptor, O_BINARY);  \
+        if (isatty(file_descriptor))                             \
+          __djgpp_set_ctrl_c(1);                                 \
+        previous_mode;                                           \
+     })                                                          \
+   )
+# endif /* __DJGPP__ */
+# ifdef WIN32
+#  define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+# endif
+# ifdef __CYGWIN__
+#  define SET_BINARY(f)   /* */
+# endif
+#endif
+
+#ifndef SET_BINARY
+# define SET_BINARY(f)   /* */
 #endif
 
 #ifdef HAVE_FCNTL_H
 # include <fcntl.h>
 #endif
@@ -403,41 +436,37 @@ int main(int argc, char *argv[])
         c = argv[i][1];
         switch( c )
         {
         case 't':
 	        mode = 1;
-#ifdef WIN32
           /* Set "stdout" to binary mode: */
-          _setmode( _fileno( ofile ), _O_BINARY );
-#endif
+	        SET_BINARY(ofile);
 	        break;
         case 'b':
 	        mode = 0;
-#ifdef WIN32
           /* Set "stdin" to binary mode: */
-          _setmode( _fileno( ifile ), _O_BINARY );
-#endif
+	        SET_BINARY(ifile);
 	        break;
         case 'i':
           if (mode == 0)
             ifile = fopen(argv[++i], "rb");
           else
             ifile = fopen(argv[++i], "rt");
           if (ifile == (FILE *)NULL)
             {
-	            printf("Unable to open: %s\n", argv[i]);
+              printf("Unable to open: %s\n", argv[i]);
               return 1;
             }
 	        break;
         case 'o':
           if (mode == 0)
             ofile = fopen(argv[++i], "wt");
           else
             ofile = fopen(argv[++i], "wb");
           if (ofile == (FILE *)NULL)
             {
-	            printf("Unable to open: %s\n", argv[i]);
+              printf("Unable to open: %s\n", argv[i]);
               return 1;
             }
 	        break;
         default:
 	        printf("Unknown option: %s\n", argv[i]);
diff -aprNU5 tiff-4.0.3.orig/libtiff/tif_jpeg.c tiff-4.0.3/libtiff/tif_jpeg.c
--- tiff-4.0.3.orig/libtiff/tif_jpeg.c	2012-07-06 18:48:04 +0000
+++ tiff-4.0.3/libtiff/tif_jpeg.c	2013-05-18 00:15:34 +0000
@@ -1959,11 +1959,11 @@ JPEGPostEncode(TIFF* tif)
 static void
 JPEGCleanup(TIFF* tif)
 {
 	JPEGState *sp = JState(tif);
 	
-	assert(sp != 0);
+	/* assert(sp != 0); */
 
 	tif->tif_tagmethods.vgetfield = sp->vgetparent;
 	tif->tif_tagmethods.vsetfield = sp->vsetparent;
 	tif->tif_tagmethods.printdir = sp->printdir;
 
@@ -2100,11 +2100,15 @@ JPEGVGetField(TIFF* tif, uint32 tag, va_
 static void
 JPEGPrintDir(TIFF* tif, FILE* fd, long flags)
 {
 	JPEGState* sp = JState(tif);
 
-	assert(sp != NULL);
+	/* assert(sp != NULL); */
+	if (sp == NULL) {
+		TIFFWarningExt(tif->tif_clientdata, "JPEGPrintDir", "Unknown JPEGState");
+		return;
+	}
 	(void) flags;
 
         if( sp != NULL ) {
 		if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
 			fprintf(fd, "  JPEG Tables: (%lu bytes)\n",
diff -aprNU5 tiff-4.0.3.orig/libtiff/tiffiop.h tiff-4.0.3/libtiff/tiffiop.h
--- tiff-4.0.3.orig/libtiff/tiffiop.h	2012-06-06 13:35:22 +0000
+++ tiff-4.0.3/libtiff/tiffiop.h	2013-05-18 00:15:34 +0000
@@ -48,11 +48,11 @@
 # include <assert.h>
 #else
 # define assert(x) 
 #endif
 
-#ifdef HAVE_SEARCH_H
+#if defined HAVE_SEARCH_H && defined HAVE_LFIND
 # include <search.h>
 #else
 extern void *lfind(const void *, const void *, size_t *, size_t,
 		   int (*)(const void *, const void *));
 #endif
diff -aprNU5 tiff-4.0.3.orig/tools/fax2ps.c tiff-4.0.3/tools/fax2ps.c
--- tiff-4.0.3.orig/tools/fax2ps.c	2011-04-02 19:30:20 +0000
+++ tiff-4.0.3/tools/fax2ps.c	2013-05-18 00:15:34 +0000
@@ -39,10 +39,43 @@
 # include <fcntl.h>
 #endif
 
 #ifdef HAVE_IO_H
 # include <io.h>
+# ifdef _O_BINARY
+#  define _O_BINARY       O_BINARY
+#  define _fileno(f)      fileno(f)
+#  define _setmode(f, m)  setmode(f, m)
+# endif /* _O_BINARY */
+# ifdef __DJGPP__
+#  include <unistd.h>       /* to declare isatty() */
+#  include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+   /* This is DJGPP-specific.  By default, switching console
+      to binary mode disables SIGINT and SIGQUIT.  But it
+      is preferable to have terminal reads and writes to
+      be interruptible.  */
+#  define SET_BINARY(f)                                          \
+   (__extension__                                                \
+     ({                                                          \
+        int file_descriptor = fileno(f);                         \
+        int previous_mode = setmode(file_descriptor, O_BINARY);  \
+        if (isatty(file_descriptor))                             \
+          __djgpp_set_ctrl_c(1);                                 \
+        previous_mode;                                           \
+     })                                                          \
+   )
+# endif /* __DJGPP__ */
+# ifdef WIN32
+#  define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+# endif
+# ifdef __CYGWIN__
+#  define SET_BINARY(f)   /* */
+# endif
+#endif
+
+#ifndef SET_BINARY
+# define SET_BINARY(f)   /* */
 #endif
 
 #ifdef NEED_LIBPORT
 # include "libport.h"
 #endif
@@ -385,13 +418,11 @@ main(int argc, char** argv)
 	fd = tmpfile();
 	if (fd == NULL) {
 	    fprintf(stderr, "Could not obtain temporary file.\n");
 	    exit(-2);
 	}
-#if defined(HAVE_SETMODE) && defined(O_BINARY)
-	setmode(fileno(stdin), O_BINARY);
-#endif
+	SET_BINARY(stdin);
 	while ((n = read(fileno(stdin), buf, sizeof (buf))) > 0)
 	    write(fileno(fd), buf, n);
 	lseek(fileno(fd), 0, SEEK_SET);
 #if defined(_WIN32) && defined(USE_WIN32_FILEIO)
 	tif = TIFFFdOpen(_get_osfhandle(fileno(fd)), "temp", "r");
diff -aprNU5 tiff-4.0.3.orig/tools/ppm2tiff.c tiff-4.0.3/tools/ppm2tiff.c
--- tiff-4.0.3.orig/tools/ppm2tiff.c	2010-04-10 19:22:34 +0000
+++ tiff-4.0.3/tools/ppm2tiff.c	2013-05-18 00:15:34 +0000
@@ -39,10 +39,43 @@
 # include <fcntl.h>
 #endif
 
 #ifdef HAVE_IO_H
 # include <io.h>
+# ifdef _O_BINARY
+#  define _O_BINARY       O_BINARY
+#  define _fileno(f)      fileno(f)
+#  define _setmode(f, m)  setmode(f, m)
+# endif /* _O_BINARY */
+# ifdef __DJGPP__
+#  include <unistd.h>       /* to declare isatty() */
+#  include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+   /* This is DJGPP-specific.  By default, switching console
+      to binary mode disables SIGINT and SIGQUIT.  But it
+      is preferable to have terminal reads and writes to
+      be interruptible.  */
+#  define SET_BINARY(f)                                          \
+   (__extension__                                                \
+     ({                                                          \
+        int file_descriptor = fileno(f);                         \
+        int previous_mode = setmode(file_descriptor, O_BINARY);  \
+        if (isatty(file_descriptor))                             \
+          __djgpp_set_ctrl_c(1);                                 \
+        previous_mode;                                           \
+     })                                                          \
+   )
+# endif /* __DJGPP__ */
+# ifdef WIN32
+#  define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+# endif
+# ifdef __CYGWIN__
+#  define SET_BINARY(f)   /* */
+# endif
+#endif
+
+#ifndef SET_BINARY
+# define SET_BINARY(f)   /* */
 #endif
 
 #ifdef NEED_LIBPORT
 # include "libport.h"
 #endif
@@ -128,13 +161,11 @@ main(int argc, char* argv[])
 			return (-1);
 		}
 	} else {
 		infile = "<stdin>";
 		in = stdin;
-#if defined(HAVE_SETMODE) && defined(O_BINARY)
-		setmode(fileno(stdin), O_BINARY);
-#endif
+		SET_BINARY(stdin);
 	}
 
 	if (fgetc(in) != 'P')
 		BadPPM(infile);
 	switch (fgetc(in)) {
diff -aprNU5 tiff-4.0.3.orig/tools/tiff2pdf.c tiff-4.0.3/tools/tiff2pdf.c
--- tiff-4.0.3.orig/tools/tiff2pdf.c	2012-07-26 02:56:42 +0000
+++ tiff-4.0.3/tools/tiff2pdf.c	2013-05-18 00:15:34 +0000
@@ -31,10 +31,14 @@
 #include <string.h>
 #include <ctype.h>
 #include <time.h>
 #include <errno.h>
 
+#ifdef __DJGPP__
+# include "snprintf.h"
+#endif
+
 #if HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 
 #ifdef HAVE_FCNTL_H
diff -aprNU5 tiff-4.0.3.orig/tools/tiffgt.c tiff-4.0.3/tools/tiffgt.c
--- tiff-4.0.3.orig/tools/tiffgt.c	2010-07-08 16:10:24 +0000
+++ tiff-4.0.3/tools/tiffgt.c	2013-05-18 00:15:34 +0000
@@ -29,10 +29,14 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#ifdef __DJGPP__
+# include "snprintf.h"
+#endif
+
 #if HAVE_APPLE_OPENGL_FRAMEWORK
 # include <OpenGL/gl.h>
 # include <GLUT/glut.h>
 #else
 # include <GL/gl.h>
