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);
}
struct flat_hdr hdr;
+ int gf_is_pipe = 0;
program = argv[0];
progname = argv[0];
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) */
if (reloc)
fwrite(reloc, reloc_len * 4, 1, gf);
+ if(gf_is_pipe)
+ pclose(gf);
+ else
fclose(gf);
exit(0);
{
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;
}
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);
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);
}
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);
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 */
}
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);