diff -ubr elf2flt-20040326/elf2flt.c elf2flt-20040326/elf2flt.c
--- elf2flt-20040326/elf2flt.c	2004-03-23 22:51:15.000000000 +0100
+++ elf2flt-20040326/elf2flt.c	2004-12-21 19:46:03.801883200 +0100
@@ -1287,7 +1287,7 @@
 
   asection *text_section = bfd_get_section_by_name (abfd, ".text");
 
-  if (!(ef = fopen(ename, "r"))) {
+  if (!(ef = fopen(ename, "rb"))) {
     fprintf (stderr,"Can't open %s\n",ename);
     exit(1);
   }
@@ -1414,6 +1414,7 @@
   
   struct flat_hdr hdr;
 
+  int gf_is_pipe = 0;
 
   program = argv[0];
   progname = argv[0];
@@ -1678,11 +1679,16 @@
   sprintf(cmd, "gzip -f -9 >> %s", ofile);
 
 #define	START_COMPRESSOR do { \
-		if (gf) fclose(gf); \
-		if (!(gf = popen(cmd, "w"))) { \
+		if (gf) \
+			if (gf_is_pipe) \
+				pclose(gf); \
+			else \
+				fclose(gf); \
+		if (!(gf = popen(cmd, "wb"))) { \
 			fprintf(stderr, "Can't run cmd %s\n", cmd); \
 			exit(4); \
 		} \
+		gf_is_pipe = 1; \
 	} while (0)
 
   gf = fopen(ofile, "ab");	/* Add 'b' to support non-posix (ie windows) */
@@ -1711,6 +1717,9 @@
   if (reloc)
     fwrite(reloc, reloc_len * 4, 1, gf);
 
+  if(gf_is_pipe)
+    pclose(gf);
+  else
   fclose(gf);
 
   exit(0);
diff -ubr elf2flt-20040326/flthdr.c elf2flt-20040326/flthdr.c
--- elf2flt-20040326/flthdr.c	2003-05-29 05:17:56.000000000 +0200
+++ elf2flt-20040326/flthdr.c	2004-12-21 19:49:06.754956800 +0100
@@ -60,13 +60,14 @@
 {
 	int old_flags, old_stack, new_flags, new_stack;
 	FILE *ifp, *ofp;
+	int ofp_is_pipe = 0;
 	struct flat_hdr old_hdr, new_hdr;
 	char tfile[256];
 	char tfile2[256];
 
 	*tfile = *tfile2 = '\0';
 
-	if ((ifp = fopen(ifile, "r")) == NULL) {
+	if ((ifp = fopen(ifile, "rb")) == NULL) {
 		fprintf(stderr, "Cannot open %s\n", ifile);
 		return;
 	}
@@ -189,7 +190,7 @@
 
 	strcpy(tfile, "/tmp/flatXXXXXX");
 	mkstemp(tfile);
-	if ((ofp = fopen(tfile, "w")) == NULL) {
+	if ((ofp = fopen(tfile, "wb")) == NULL) {
 		fprintf(stderr, "Failed to open %s for writing\n", tfile);
 		unlink(tfile);
 		unlink(tfile2);
@@ -214,7 +215,7 @@
 		mkstemp(tfile2);
 		
 		if (old_flags & FLAT_FLAG_GZDATA) {
-			tfp = fopen(tfile2, "w");
+			tfp = fopen(tfile2, "wb");
 			if (!tfp) {
 				fprintf(stderr, "Failed to open %s for writing\n", tfile2);
 				exit(1);
@@ -225,12 +226,16 @@
 		}
 
 		sprintf(cmd, "gunzip >> %s", tfile2);
-		tfp = popen(cmd, "w");
+		tfp = popen(cmd, "wb");
+		if(!tfp) {
+			perror("popen");
+			exit(1);
+		}
 		transferr(ifp, tfp, -1);
-		fclose(tfp);
+		pclose(tfp);
 
 		fclose(ifp);
-		ifp = fopen(tfile2, "r");
+		ifp = fopen(tfile2, "rb");
 		if (!ifp) {
 			fprintf(stderr, "Failed to open %s for reading\n", tfile2);
 			unlink(tfile);
@@ -243,14 +248,16 @@
 		printf("zflat %s --> %s\n", ifile, ofile);
 		fclose(ofp);
 		sprintf(cmd, "gzip -9 -f >> %s", tfile);
-		ofp = popen(cmd, "w");
+		ofp = popen(cmd, "wb");
+		ofp_is_pipe = 1;
 	} else if (new_flags & FLAT_FLAG_GZDATA) {
 		printf("zflat-data %s --> %s\n", ifile, ofile);
 		transferr(ifp, ofp, ntohl(new_hdr.data_start) -
 				sizeof(struct flat_hdr));
 		fclose(ofp);
 		sprintf(cmd, "gzip -9 -f >> %s", tfile);
-		ofp = popen(cmd, "w");
+		ofp = popen(cmd, "wb");
+		ofp_is_pipe = 1;
 	}
 
 	if (!ofp) { /* can only happen if using gzip/gunzip */
@@ -271,6 +278,9 @@
 	}
 
 	fclose(ifp);
+	if(ofp_is_pipe)
+		pclose(ofp);
+	else
 	fclose(ofp);
 
 	/* cheat a little here to preserve file permissions */
