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
3d38ce
static int mdso_create_output(
3b42f9
	const struct mdso_driver_ctx *	dctx,
512db4
	const char *			name)
3b42f9
{
3d38ce
	int	fddst;
3d38ce
	int	fdout;
3b42f9
512db4
	fddst = mdso_driver_fddst(dctx);
3b42f9
512db4
	if ((fdout = openat(fddst,name,
3b42f9
                        O_CREAT|O_TRUNC|O_WRONLY|O_NOCTTY|O_NOFOLLOW,
3d38ce
                        S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3d38ce
		return MDSO_SYSTEM_ERROR(dctx);
3d38ce
3d38ce
	return fdout;
3d38ce
}
3d38ce
3d38ce
static FILE * mdso_create_output_stream(
3d38ce
	const struct mdso_driver_ctx *	dctx,
3d38ce
	const char *			name)
3d38ce
{
3d38ce
	int	fdout;
3d38ce
	FILE *	fout;
3d38ce
3d38ce
	if ((fdout = mdso_create_output(dctx,name)) < 0)
3b42f9
		return 0;
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
{
3d38ce
	return mdso_create_output_stream(dctx,arname);
c70c0a
}
c70c0a
19f61b
FILE * mdso_create_asmsrc(
3e55ee
	const struct mdso_driver_ctx *	dctx,
3e55ee
	const char *			asmname)
3e55ee
{
512db4
	return dctx->cctx->dstdir
3d38ce
		? mdso_create_output_stream(dctx,asmname)
512db4
		: stdout;
3e55ee
}
3e55ee
3e55ee
FILE * mdso_create_object(
3e55ee
	const struct mdso_driver_ctx *	dctx,
3e55ee
	const char *			objname)
3e55ee
{
3d38ce
	return mdso_create_output_stream(dctx,objname);
3e55ee
}