2011-05-29  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

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

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


2011-05-27  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

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


2011-05-25  Juan Manuel Guerrero  <juan.guerrero@gmx.de>

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

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

	* tools/tiff2pdf.c (t2p_init): Use "l" conversion specifier when
	printing long unsigned integer.
	(t2p_read_tiff_init): Use "l" conversion specifier when printing long
	unsigned integer.
	(t2p_write_pdf): Use "l" conversion specifier when printing long
	unsigned integer.

	* 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-3.9.5.orig/contrib/iptcutil/iptcutil.c tiff-3.9.5/contrib/iptcutil/iptcutil.c
--- tiff-3.9.5.orig/contrib/iptcutil/iptcutil.c	2010-06-08 18:50:40 +0000
+++ tiff-3.9.5/contrib/iptcutil/iptcutil.c	2011-05-29 11:31:16 +0000
@@ -12,10 +12,35 @@
 # 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
+# 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 we want
+      terminal reads and writes to be interruptible.  */
+#  define SET_BINARY(f)                                         \
+    ({ 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
 
 #ifdef HAVE_FCNTL_H
 # include <fcntl.h>
 #endif
@@ -401,21 +426,17 @@ 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
diff -aprNU5 tiff-3.9.5.orig/libtiff/tif_jpeg.c tiff-3.9.5/libtiff/tif_jpeg.c
--- tiff-3.9.5.orig/libtiff/tif_jpeg.c	2011-01-04 04:31:28 +0000
+++ tiff-3.9.5/libtiff/tif_jpeg.c	2011-05-29 11:31:16 +0000
@@ -1583,20 +1583,22 @@ 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;
 
-	if( sp->cinfo_initialized )
-	    TIFFjpeg_destroy(sp);	/* release libjpeg resources */
-	if (sp->jpegtables)		/* tag value */
-		_TIFFfree(sp->jpegtables);
+	if (sp != NULL) {
+		if( sp->cinfo_initialized )
+		    TIFFjpeg_destroy(sp);	/* release libjpeg resources */
+		if (sp->jpegtables)		/* tag value */
+			_TIFFfree(sp->jpegtables);
+	}
 	_TIFFfree(tif->tif_data);	/* release local state */
 	tif->tif_data = NULL;
 
 	_TIFFSetDefaultCompressionState(tif);
 }
@@ -1825,11 +1827,15 @@ JPEGVGetField(TIFF* tif, ttag_t 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 (TIFFFieldSet(tif,FIELD_JPEGTABLES))
 		fprintf(fd, "  JPEG Tables: (%lu bytes)\n",
 			(unsigned long) sp->jpegtables_length);
diff -aprNU5 tiff-3.9.5.orig/libtiff/tiffiop.h tiff-3.9.5/libtiff/tiffiop.h
--- tiff-3.9.5.orig/libtiff/tiffiop.h	2011-03-28 13:43:42 +0000
+++ tiff-3.9.5/libtiff/tiffiop.h	2011-05-29 11:31:16 +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-3.9.5.orig/tools/fax2ps.c tiff-3.9.5/tools/fax2ps.c
--- tiff-3.9.5.orig/tools/fax2ps.c	2011-04-02 19:18:12 +0000
+++ tiff-3.9.5/tools/fax2ps.c	2011-05-29 11:31:16 +0000
@@ -39,10 +39,35 @@
 # 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
+# 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 we want
+      terminal reads and writes to be interruptible.  */
+#  define SET_BINARY(f)                                         \
+    ({ 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
 
 #include "tiffio.h"
 
 float	defxres = 204.;		/* default x resolution (pixels/inch) */
@@ -381,13 +406,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-3.9.5.orig/tools/ppm2tiff.c tiff-3.9.5/tools/ppm2tiff.c
--- tiff-3.9.5.orig/tools/ppm2tiff.c	2010-06-08 18:50:44 +0000
+++ tiff-3.9.5/tools/ppm2tiff.c	2011-05-29 11:31:16 +0000
@@ -39,10 +39,35 @@
 # 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
+# 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 we want
+      terminal reads and writes to be interruptible.  */
+#  define SET_BINARY(f)                                         \
+    ({ 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
 
 #include "tiffio.h"
 
 #ifndef HAVE_GETOPT
@@ -124,13 +149,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-3.9.5.orig/tools/tiff2pdf.c tiff-3.9.5/tools/tiff2pdf.c
--- tiff-3.9.5.orig/tools/tiff2pdf.c	2010-12-14 01:45:50 +0000
+++ tiff-3.9.5/tools/tiff2pdf.c	2011-05-29 11:31:16 +0000
@@ -31,10 +31,14 @@
 #include <string.h>
 #include <ctype.h>
 #include <time.h>
 #include <errno.h>
 
+#ifdef __DJGPP__
+# include "../djgpp/snprintf.h"
+#endif
+
 #if HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 
 #ifdef HAVE_FCNTL_H
@@ -893,11 +897,11 @@ T2P* t2p_init()
 {
 	T2P* t2p = (T2P*) _TIFFmalloc(sizeof(T2P));
 	if(t2p==NULL){
 		TIFFError(
 			TIFF2PDF_MODULE, 
-			"Can't allocate %u bytes of memory for t2p_init", 
+			"Can't allocate %lu bytes of memory for t2p_init", 
 			sizeof(T2P));
 		return( (T2P*) NULL );
 	}
 	_TIFFmemset(t2p, 0x00, sizeof(T2P));
 	t2p->pdf_majorversion=1;
@@ -1006,22 +1010,22 @@ void t2p_read_tiff_init(T2P* t2p, TIFF*
 	directorycount=TIFFNumberOfDirectories(input);
 	t2p->tiff_pages = (T2P_PAGE*) _TIFFmalloc(directorycount * sizeof(T2P_PAGE));
 	if(t2p->tiff_pages==NULL){
 		TIFFError(
 			TIFF2PDF_MODULE, 
-			"Can't allocate %u bytes of memory for tiff_pages array, %s", 
+			"Can't allocate %lu bytes of memory for tiff_pages array, %s", 
 			directorycount * sizeof(T2P_PAGE), 
 			TIFFFileName(input));
 		t2p->t2p_error = T2P_ERR_ERROR;
 		return;
 	}
 	_TIFFmemset( t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE));
 	t2p->tiff_tiles = (T2P_TILES*) _TIFFmalloc(directorycount * sizeof(T2P_TILES));
 	if(t2p->tiff_tiles==NULL){
 		TIFFError(
 			TIFF2PDF_MODULE, 
-			"Can't allocate %u bytes of memory for tiff_tiles array, %s", 
+			"Can't allocate %lu bytes of memory for tiff_tiles array, %s", 
 			directorycount * sizeof(T2P_TILES), 
 			TIFFFileName(input));
 		t2p->t2p_error = T2P_ERR_ERROR;
 		return;
 	}
@@ -1149,11 +1153,11 @@ void t2p_read_tiff_init(T2P* t2p, TIFF*
 				t2p->tiff_tiles[i].tiles_tilecount 
 				* sizeof(T2P_TILE) );
 			if( t2p->tiff_tiles[i].tiles_tiles == NULL){
 				TIFFError(
 					TIFF2PDF_MODULE, 
-					"Can't allocate %u bytes of memory for t2p_read_tiff_init, %s", 
+					"Can't allocate %lu bytes of memory for t2p_read_tiff_init, %s", 
 					t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE), 
 					TIFFFileName(input));
 				t2p->t2p_error = T2P_ERR_ERROR;
 				return;
 			}
@@ -5141,11 +5145,11 @@ tsize_t t2p_write_pdf(T2P* t2p, TIFF* in
 	if(t2p->t2p_error!=T2P_ERR_OK){return(0);}
 	t2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(t2p->pdf_xrefcount * sizeof(uint32) );
 	if(t2p->pdf_xrefoffsets==NULL){
 		TIFFError(
 			TIFF2PDF_MODULE, 
-			"Can't allocate %u bytes of memory for t2p_write_pdf", 
+			"Can't allocate %lu bytes of memory for t2p_write_pdf", 
 			t2p->pdf_xrefcount * sizeof(uint32) );
 		return(written);
 	}
 	t2p->pdf_xrefcount=0;
 	t2p->pdf_catalog=1;
diff -aprNU5 tiff-3.9.5.orig/tools/tiffgt.c tiff-3.9.5/tools/tiffgt.c
--- tiff-3.9.5.orig/tools/tiffgt.c	2010-06-08 18:50:44 +0000
+++ tiff-3.9.5/tools/tiffgt.c	2011-05-29 11:32:02 +0000
@@ -29,10 +29,14 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#ifdef __DJGPP__
+# include "../djgpp/snprintf.h"
+#endif
+
 #if HAVE_APPLE_OPENGL_FRAMEWORK
 # include <OpenGL/gl.h>
 # include <GLUT/glut.h>
 #else
 # include <GL/gl.h>
