From: Bernardo Innocenti <bernie@develer.com>
Date: Wed, 23 Feb 2005 01:31:04 +0000 (+0000)
Subject: Cygwin fixes.
X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=ca6e6d04c74bcd9848a13e4d3f787b7ffc795d7d;p=elf2flt.git

Cygwin fixes.

Patch submitted by Stig Petter Olsrød <stigpo@users.sourceforge.net>.
---

diff --git a/elf2flt.c b/elf2flt.c
index b0ef6ca..19ae196 100644
--- a/elf2flt.c
+++ b/elf2flt.c
@@ -1291,7 +1291,7 @@ output_offset_table(int fd, char *ename, bfd *abfd, asymbol **symbol_table, long
 
   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);
   }
@@ -1418,6 +1418,7 @@ int main(int argc, char *argv[])
   
   struct flat_hdr hdr;
 
+  int gf_is_pipe = 0;
 
   program = argv[0];
   progname = argv[0];
@@ -1682,11 +1683,16 @@ int main(int argc, char *argv[])
   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) */
@@ -1715,6 +1721,9 @@ int main(int argc, char *argv[])
   if (reloc)
     fwrite(reloc, reloc_len * 4, 1, gf);
 
+  if(gf_is_pipe)
+    pclose(gf);
+  else
   fclose(gf);
 
   exit(0);
diff --git a/flthdr.c b/flthdr.c
index 12f7dcd..08ac8b5 100644
--- a/flthdr.c
+++ b/flthdr.c
@@ -60,13 +60,14 @@ process_file(char *ifile, char *ofile)
 {
 	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 @@ process_file(char *ifile, char *ofile)
 
 	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 @@ process_file(char *ifile, char *ofile)
 		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 @@ process_file(char *ifile, char *ofile)
 		}
 
 		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 @@ process_file(char *ifile, char *ofile)
 		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,7 +278,10 @@ process_file(char *ifile, char *ofile)
 	}
 
 	fclose(ifp);
-	fclose(ofp);
+	if (ofp_is_pipe)
+		pclose(ofp);
+	else
+		fclose(ofp);
 
 	/* cheat a little here to preserve file permissions */
 	sprintf(cmd, "cp %s %s", tfile, ofile);