2012-05-01  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.

	* 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.

	* 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.1.orig/contrib/iptcutil/iptcutil.c tiff-4.0.1/contrib/iptcutil/iptcutil.c
--- tiff-4.0.1.orig/contrib/iptcutil/iptcutil.c	2011-05-31 14:33:06 +0000
+++ tiff-4.0.1/contrib/iptcutil/iptcutil.c	2012-05-01 12:51:56 +0000
@@ -11,10 +11,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 /* __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 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
@@ -403,21 +428,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-4.0.1.orig/libtiff/tif_jpeg.c tiff-4.0.1/libtiff/tif_jpeg.c
--- tiff-4.0.1.orig/libtiff/tif_jpeg.c	2012-02-04 20:13:12 +0000
+++ tiff-4.0.1/libtiff/tif_jpeg.c	2012-05-01 12:56:42 +0000
@@ -1991,11 +1991,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;
 
@@ -2132,11 +2132,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.1.orig/libtiff/tiffiop.h tiff-4.0.1/libtiff/tiffiop.h
--- tiff-4.0.1.orig/libtiff/tiffiop.h	2011-02-19 16:26:08 +0000
+++ tiff-4.0.1/libtiff/tiffiop.h	2012-05-01 12:58:36 +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.1.orig/tools/fax2ps.c tiff-4.0.1/tools/fax2ps.c
--- tiff-4.0.1.orig/tools/fax2ps.c	2011-04-02 19:30:20 +0000
+++ tiff-4.0.1/tools/fax2ps.c	2012-05-01 13:00:48 +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 /* _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 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 NEED_LIBPORT
 # include "libport.h"
 #endif
@@ -385,13 +410,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.1.orig/tools/ppm2tiff.c tiff-4.0.1/tools/ppm2tiff.c
--- tiff-4.0.1.orig/tools/ppm2tiff.c	2010-04-10 19:22:34 +0000
+++ tiff-4.0.1/tools/ppm2tiff.c	2012-05-01 13:01:50 +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 /* _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 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 NEED_LIBPORT
 # include "libport.h"
 #endif
@@ -128,13 +153,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.1.orig/tools/tiff2pdf.c tiff-4.0.1/tools/tiff2pdf.c
--- tiff-4.0.1.orig/tools/tiff2pdf.c	2011-05-31 17:00:02 +0000
+++ tiff-4.0.1/tools/tiff2pdf.c	2012-05-01 13:06:26 +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
@@ -1416,11 +1420,11 @@ void t2p_read_tiff_data(T2P* t2p, TIFF*
 			t2p->pdf_palette = (unsigned char*)
 				_TIFFmalloc(t2p->pdf_palettesize*3);
 			if(t2p->pdf_palette==NULL){
 				TIFFError(
 					TIFF2PDF_MODULE, 
-					"Can't allocate %u bytes of memory for t2p_read_tiff_image, %s", 
+					"Can't allocate %lu bytes of memory for t2p_read_tiff_image, %s", 
 					t2p->pdf_palettesize, 
 					TIFFFileName(input));
 				t2p->t2p_error = T2P_ERR_ERROR;
 				return;
 			}
@@ -5145,11 +5149,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", 
 			(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );
 		return(written);
 	}
 	t2p->pdf_xrefcount=0;
 	t2p->pdf_catalog=1;
diff -aprNU5 tiff-4.0.1.orig/tools/tiffgt.c tiff-4.0.1/tools/tiffgt.c
--- tiff-4.0.1.orig/tools/tiffgt.c	2010-07-08 16:10:24 +0000
+++ tiff-4.0.1/tools/tiffgt.c	2012-05-01 13:07:10 +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>
