Blame src/helper/mdso_create_output.c

3b42f9
/****************************************************************/
3b42f9
/*  mdso: midipix dso scavenger                                 */
78ab3c
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
3b42f9
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
3b42f9
/****************************************************************/
3b42f9
3b42f9
#include <stdint.h>
3b42f9
#include <stdlib.h>
3b42f9
#include <stdio.h>
3b42f9
#include <stddef.h>
3b42f9
#include <unistd.h>
3b42f9
#include <fcntl.h>
3b42f9
3b42f9
#include <mdso/mdso.h>
3b42f9
#include "mdso_driver_impl.h"
8cb533
#include "mdso_errinfo_impl.h"
3b42f9
3e55ee
static FILE * mdso_create_output(
3b42f9
	const struct mdso_driver_ctx *	dctx,
c70c0a
	const char *			name,
c70c0a
	int				fdat)
3b42f9
{
3b42f9
	struct mdso_driver_ctx_impl *	ictx;
3b42f9
	uintptr_t			addr;
3b42f9
	int				fdout;
b023b7
	FILE *				fout;
3b42f9
3b42f9
	addr = (uintptr_t)dctx - offsetof(struct mdso_driver_ctx_impl,ctx);
3b42f9
	ictx = (struct mdso_driver_ctx_impl *)addr;
c70c0a
	fdat = (fdat == AT_FDCWD) ? AT_FDCWD : ictx->fddst;
3b42f9
c70c0a
	if ((fdout = openat(fdat,name,
3b42f9
                        O_CREAT|O_TRUNC|O_WRONLY|O_NOCTTY|O_NOFOLLOW,
8cb533
                        S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
8cb533
		MDSO_SYSTEM_ERROR(dctx);
3b42f9
		return 0;
8cb533
	}
3b42f9
b023b7
	if (!(fout = fdopen(fdout,"w"))) {
b023b7
		close(fdout);
8cb533
		MDSO_SYSTEM_ERROR(dctx);
b023b7
		return 0;
b023b7
	}
b023b7
b023b7
	return fout;
3b42f9
}
3e55ee
c70c0a
FILE * mdso_create_archive(
c70c0a
	const struct mdso_driver_ctx *	dctx,
c70c0a
	const char *			arname)
c70c0a
{
c70c0a
	return mdso_create_output(dctx,arname,AT_FDCWD);
c70c0a
}
c70c0a
3e55ee
FILE * mdso_create_asm_source(
3e55ee
	const struct mdso_driver_ctx *	dctx,
3e55ee
	const char *			asmname)
3e55ee
{
3e55ee
	if (!dctx->cctx->dstdir)
3e55ee
		return stdout;
3e55ee
c70c0a
	return mdso_create_output(dctx,asmname,-1);
3e55ee
}
3e55ee
3e55ee
FILE * mdso_create_object(
3e55ee
	const struct mdso_driver_ctx *	dctx,
3e55ee
	const char *			objname)
3e55ee
{
3e55ee
	if (!dctx->cctx->dstdir) {
3e55ee
		MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_DSTDIR);
3e55ee
		return 0;
3e55ee
	}
3e55ee
c70c0a
	return mdso_create_output(dctx,objname,-1);
3e55ee
}