2022-07-17  Juan Manuel Guerrero <juan.guerrero@gmx.de>


	* examples/dump_psnr.c [_O_BINARY]: Define OS specific setmode stuff.
	[__DJGPP__]: Include required DJGPP specific headers.
	(main): Use SET_BINARY to switch stdin/stdout to binary mode.   For DJGPP,
	reenable SIGINT and SIGQUIT signals if stdin and/or stdout is connected
	to console and has been switched into binary mode.

	* examples/dump_video.c [_O_BINARY]: Define OS specific setmode stuff.
	[__DJGPP__]: Include required DJGPP specific headers.
	(main): Use SET_BINARY to switch stdin/stdout to binary mode.   For DJGPP,
	reenable SIGINT and SIGQUIT signals if stdin and/or stdout is connected
	to console and has been switched into binary mode.

	* examples/encoder_example.c [_O_BINARY]: Define OS specific setmode stuff.
	[__DJGPP__]: Include required DJGPP specific headers.
	(id_file, main): Use SET_BINARY to switch stdin/stdout to binary mode.
	 For DJGPP, reenable SIGINT and SIGQUIT signals if stdin and/or stdout
	is connected to console and has been switched into binary mode.

	* examples/player_example.c [_O_BINARY]: Define OS specific setmode stuff.
	[__DJGPP__]: Include required DJGPP specific headers.
	(main): Use SET_BINARY to switch stdin/stdout to binary mode.   For DJGPP,
	reenable SIGINT and SIGQUIT signals if stdin and/or stdout is connected
	to console and has been switched into binary mode.






diff -aprNU3 libtheora-1.1.1.orig/examples/dump_psnr.c libtheora-1.1.1/examples/dump_psnr.c
--- libtheora-1.1.1.orig/examples/dump_psnr.c	2009-08-22 18:10:00 +0000
+++ libtheora-1.1.1/examples/dump_psnr.c	2022-07-17 09:37:48 +0000
@@ -49,6 +49,50 @@
 #include <signal.h>
 #include "theora/theoradec.h"
 
+/* We need to set stdin/stdout to binary mode, not only for _WIN32. */
+#if defined(_O_BINARY)
+# define _O_BINARY       O_BINARY
+# define _fileno(f)      fileno(f)
+# define _setmode(f, m)  setmode(f, m)
+#endif /* _O_BINARY */
+
+#if defined(__DJGPP__)
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#   define __gnuc_extension__  __extension__
+# else
+#   define __gnuc_extension__
+# endif
+
+# include <io.h>           /* declares setmode() */
+# include <fcntl.h>        /* defines O_BINARY */
+# 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)                                          \
+  (__gnuc_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__ */
+#if defined(_WIN32)
+# define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+#endif
+#if defined(__CYGWIN__)
+# define SET_BINARY(f)   /* */
+#endif
+
+#if !defined(SET_BINARY)
+# define SET_BINARY(f)   /* */
+#endif
+
 const char *optstring = "fsy";
 struct option options [] = {
   {"frame-type",no_argument,NULL,'f'},
@@ -1054,13 +1098,10 @@ int main(int _argc,char *_argv[]){
   FILE        *fin;
   int          long_option_index;
   int          c;
-#ifdef _WIN32
-  /*We need to set stdin/stdout to binary mode on windows.
-    Beware the evil ifdef.
-    We avoid these where we can, but this one we cannot.
-    Don't add any more, you'll probably go to hell if you do.*/
-  _setmode(_fileno(stdin),_O_BINARY);
-#endif
+
+  /* We need to set stdin/stdout to binary mode. Damn windows. */
+  SET_BINARY(stdin);
+
   /*Process option arguments.*/
   while((c=getopt_long(_argc,_argv,optstring,options,&long_option_index))!=EOF){
     switch(c){
diff -aprNU3 libtheora-1.1.1.orig/examples/dump_video.c libtheora-1.1.1/examples/dump_video.c
--- libtheora-1.1.1.orig/examples/dump_video.c	2009-08-22 18:10:00 +0000
+++ libtheora-1.1.1/examples/dump_video.c	2022-07-17 09:37:46 +0000
@@ -51,6 +51,50 @@
 #include "getopt.h"
 #include "theora/theoradec.h"
 
+/* We need to set stdin/stdout to binary mode, not only for _WIN32. */
+#if defined(_O_BINARY)
+# define _O_BINARY       O_BINARY
+# define _fileno(f)      fileno(f)
+# define _setmode(f, m)  setmode(f, m)
+#endif /* _O_BINARY */
+
+#if defined(__DJGPP__)
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#   define __gnuc_extension__  __extension__
+# else
+#   define __gnuc_extension__
+# endif
+
+# include <io.h>           /* declares setmode() */
+# include <fcntl.h>        /* defines O_BINARY */
+# 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)                                          \
+  (__gnuc_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__ */
+#if defined(_WIN32)
+# define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+#endif
+#if defined(__CYGWIN__)
+# define SET_BINARY(f)   /* */
+#endif
+
+#if !defined(SET_BINARY)
+# define SET_BINARY(f)   /* */
+#endif
+
 const char *optstring = "o:rf";
 struct option options [] = {
   {"output",required_argument,NULL,'o'},
@@ -214,12 +258,9 @@ int main(int argc,char *argv[]){
   FILE *infile = stdin;
   outfile = stdout;
 
-#ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */
-  /* Beware the evil ifdef. We avoid these where we can, but this one we
-     cannot. Don't add any more, you'll probably go to hell if you do. */
-  _setmode( _fileno( stdin ), _O_BINARY );
-  _setmode( _fileno( stdout ), _O_BINARY );
-#endif
+  /* We need to set stdin/stdout to binary mode. Damn windows. */
+  SET_BINARY(stdin);
+  SET_BINARY(stdout);
 
   /* Process option arguments. */
   while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){
diff -aprNU3 libtheora-1.1.1.orig/examples/encoder_example.c libtheora-1.1.1/examples/encoder_example.c
--- libtheora-1.1.1.orig/examples/encoder_example.c	2009-08-25 19:48:58 +0000
+++ libtheora-1.1.1/examples/encoder_example.c	2022-07-17 09:37:46 +0000
@@ -61,6 +61,50 @@ static double rint(double x)
 }
 #endif
 
+/* We need to set stdin/stdout to binary mode, not only for _WIN32. */
+#if defined(_O_BINARY)
+# define _O_BINARY       O_BINARY
+# define _fileno(f)      fileno(f)
+# define _setmode(f, m)  setmode(f, m)
+#endif /* _O_BINARY */
+
+#if defined(__DJGPP__)
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#   define __gnuc_extension__  __extension__
+# else
+#   define __gnuc_extension__
+# endif
+
+# include <io.h>           /* declares setmode() */
+# include <fcntl.h>        /* defines O_BINARY */
+# 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)                                          \
+  (__gnuc_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__ */
+#if defined(_WIN32)
+# define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+#endif
+#if defined(__CYGWIN__)
+# define SET_BINARY(f)   /* */
+#endif
+
+#if !defined(SET_BINARY)
+# define SET_BINARY(f)   /* */
+#endif
+
 const char *optstring = "b:e:o:a:A:v:V:s:S:f:F:ck:d:z:\1\2\3\4";
 struct option options [] = {
   {"begin-time",required_argument,NULL,'b'},
@@ -705,7 +749,7 @@ static void id_file(char *f){
 
   if(!strcmp(f,"-")){
     /* stdin */
-    test=stdin;
+    SET_BINARY(test = stdin);
   }else{
     test=fopen(f,"rb");
     if(!test){
@@ -1246,14 +1290,10 @@ int main(int argc,char *argv[]){
   int twopass=0;
   int passno;
 
-#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
-  /* if we were reading/writing a file, it would also need to in
-     binary mode, eg, fopen("file.wav","wb"); */
-  /* Beware the evil ifdef. We avoid these where we can, but this one we
-     cannot. Don't add any more, you'll probably go to hell if you do. */
-  _setmode( _fileno( stdin ), _O_BINARY );
-  _setmode( _fileno( stdout ), _O_BINARY );
-#endif
+  /* We need to set stdin/stdout to binary mode. Damn windows. */
+  SET_BINARY(stdin);
+  SET_BINARY(stdout);
+
 
   while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){
     switch(c){
diff -aprNU3 libtheora-1.1.1.orig/examples/player_example.c libtheora-1.1.1/examples/player_example.c
--- libtheora-1.1.1.orig/examples/player_example.c	2009-09-20 17:05:46 +0000
+++ libtheora-1.1.1/examples/player_example.c	2022-07-17 09:37:46 +0000
@@ -76,6 +76,50 @@
 #endif
 #include <sys/ioctl.h>
 
+/* We need to set stdin/stdout to binary mode, not only for _WIN32. */
+#if defined(_O_BINARY)
+# define _O_BINARY       O_BINARY
+# define _fileno(f)      fileno(f)
+# define _setmode(f, m)  setmode(f, m)
+#endif /* _O_BINARY */
+
+#if defined(__DJGPP__)
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#   define __gnuc_extension__  __extension__
+# else
+#   define __gnuc_extension__
+# endif
+
+# include <io.h>           /* declares setmode() */
+# include <fcntl.h>        /* defines O_BINARY */
+# 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)                                          \
+  (__gnuc_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__ */
+#if defined(_WIN32)
+# define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+#endif
+#if defined(__CYGWIN__)
+# define SET_BINARY(f)   /* */
+#endif
+
+#if !defined(SET_BINARY)
+# define SET_BINARY(f)   /* */
+#endif
+
 /* Helper; just grab some more compressed bitstream and sync it for
    page extraction */
 int buffer_data(FILE *in,ogg_sync_state *oy){
@@ -482,11 +526,8 @@ int main(int argc,char *const *argv){
   int frames = 0;
   int dropped = 0;
 
-#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
-  /* Beware the evil ifdef. We avoid these where we can, but this one we
-     cannot. Don't add any more, you'll probably go to hell if you do. */
-  _setmode( _fileno( stdin ), _O_BINARY );
-#endif
+  /* We need to set stdin/stdout to binary mode. Damn windows. */
+  SET_BINARY(stdin);
 
   /* open the input file if any */
   if(argc==2){
