summaryrefslogtreecommitdiffhomepage
path: root/src/internal/perk_output_impl.h
blob: cb8b304ad21be1dc3969273e2482ea65a0693bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#ifndef PE_OUTPUT_IMPL_H
#define PE_OUTPUT_IMPL_H

#include <unistd.h>
#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->ioctx->fdout < 0)
		return stdout;

	if ((fdout = dup(cctx->ioctx->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;
}

#endif