Blame src/helper/mdso_create_asm_source.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,
3e55ee
	const char *			name)
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;
3b42f9
3e55ee
	if ((fdout = openat(ictx->fddst,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
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
3e55ee
	return mdso_create_output(dctx,asmname);
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
3e55ee
	return mdso_create_output(dctx,objname);
3e55ee
}