diff options
author | midipix <writeonce@midipix.org> | 2015-11-23 15:35:04 -0500 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2016-11-10 23:35:27 -0500 |
commit | 7cd8bca5bbc93ed1352e02089d03c34a23609444 (patch) | |
tree | 3edea4ea54e62f9730b4c025950fffad5ad9393e | |
parent | 8fd1f503fdfc593aed4b296cfa7024a4a09d540c (diff) | |
download | perk-7cd8bca5bbc93ed1352e02089d03c34a23609444.tar.bz2 perk-7cd8bca5bbc93ed1352e02089d03c34a23609444.tar.xz |
pretty printer: use common prolog/epilog for output stream selection.
-rw-r--r-- | project/headers.mk | 3 | ||||
-rw-r--r-- | src/internal/perk_output.h | 31 | ||||
-rw-r--r-- | src/output/pe_output_export_symbols.c | 26 |
3 files changed, 40 insertions, 20 deletions
diff --git a/project/headers.mk b/project/headers.mk index 33eef8d..b961045 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -8,6 +8,7 @@ API_HEADERS = \ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/argv/argv.h \ - $(PROJECT_DIR)/src/internal/$(PACKAGE)_impl.h + $(PROJECT_DIR)/src/internal/$(PACKAGE)_impl.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_output.h ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/src/internal/perk_output.h b/src/internal/perk_output.h new file mode 100644 index 0000000..d60d78d --- /dev/null +++ b/src/internal/perk_output.h @@ -0,0 +1,31 @@ +#include <perk/perk.h> + +static inline FILE * pe_output_prolog(const struct pe_common_ctx * cctx, FILE * fout, FILE ** ftmp) +{ + int fdout; + + *ftmp = (FILE *)0; + + if (fout) + return fout; + else if (!cctx) + return 0; + else if (cctx->fdout < 0) + return stdout; + + if ((fdout = dup(cctx->fdout)) < 0) + return 0; + + if ((*ftmp = fdopen(fdout,"a"))) + return *ftmp; + + close(fdout); + return 0; +} + +static inline int pe_output_epilog(int ret, FILE * f) +{ + if (f) + fclose(f); + return ret; +} diff --git a/src/output/pe_output_export_symbols.c b/src/output/pe_output_export_symbols.c index fde10c5..529cb9e 100644 --- a/src/output/pe_output_export_symbols.c +++ b/src/output/pe_output_export_symbols.c @@ -5,38 +5,26 @@ #include <errno.h> #include <perk/perk.h> +#include "perk_output.h" -int pe_output_export_symbols (const struct pe_image_meta * m, const struct pe_common_ctx * cctx, FILE * f) +int pe_output_export_symbols (const struct pe_image_meta * m, const struct pe_common_ctx * cctx, FILE * fout) { + FILE * ftmp; uint32_t offset; uint32_t * symrva; - int fdout; int i; if (!m->hedata) return 0; - else if (f) - fdout = -1; - else if (!cctx) - return -1; - else if (cctx->fdout < 0) { - f = stdout; - fdout = -1; - } else if ((fdout = dup(cctx->fdout)) < 0) - return -1; - else if (!(f = fdopen(fdout,"a"))) { - close(fdout); + + if (!(fout = pe_output_prolog(cctx,fout,&ftmp))) return -1; - } offset = m->hedata->virtual_addr - m->hedata->ptr_to_raw_data; symrva = (uint32_t *)((uintptr_t)m->image.addr + (m->edata.name_ptr_rva - offset)); for (i=0; i<m->edata.num_of_name_ptrs; i++) - fprintf(f,"%s\n",(char *)((uintptr_t)m->image.addr + symrva[i] - offset)); - - if (fdout >= 0) - fclose(f); + fprintf(fout,"%s\n",(char *)((uintptr_t)m->image.addr + symrva[i] - offset)); - return 0; + return pe_output_epilog(0,ftmp); } |