Redfoxmoon / cross / slibtool

Forked from cross/slibtool 10 months ago
Clone

Blame src/logic/slbt_exec_execute.c

e956c8
/*******************************************************************/
e956c8
/*  slibtool: a skinny libtool implementation, written in C        */
6803d8
/*  Copyright (C) 2016--2018  Z. Gilboa                            */
e956c8
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
e956c8
/*******************************************************************/
e956c8
e956c8
#include <string.h>
e956c8
#include <stdbool.h>
e956c8
#include <fcntl.h>
e956c8
#include <errno.h>
e956c8
#include <sys/stat.h>
e956c8
e956c8
#include <slibtool/slibtool.h>
e956c8
#include "slibtool_spawn_impl.h"
a47c7a
#include "slibtool_errinfo_impl.h"
e956c8
e956c8
int  slbt_exec_execute(
e956c8
	const struct slbt_driver_ctx *	dctx,
e956c8
	struct slbt_exec_ctx *		ectx)
e956c8
{
e956c8
	int			ret;
e956c8
	char *			program;
e956c8
	char *			script;
e956c8
	char *			base;
e956c8
	char *			mark;
e956c8
	char			exeref [PATH_MAX];
e956c8
	char			wrapper[PATH_MAX];
1ed71a
	struct stat		st;
e956c8
	struct slbt_exec_ctx *	actx = 0;
e956c8
c4a389
	/* dry run */
c4a389
	if (dctx->cctx->drvflags & SLBT_DRIVER_DRY_RUN)
c4a389
		return 0;
c4a389
e956c8
	/* context */
e956c8
	if (ectx)
e956c8
		slbt_disable_placeholders(ectx);
e956c8
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
e956c8
		return ret;
e956c8
	else {
e956c8
		actx = ectx;
e956c8
		slbt_disable_placeholders(ectx);
e956c8
	}
e956c8
e956c8
	/* script, program */
e956c8
	program = ectx->cargv[0];
e956c8
	script  = ectx->cargv[1];
e956c8
e956c8
	/* wrapper */
431c99
	if ((size_t)snprintf(wrapper,sizeof(wrapper),"%s%s.exe.wrapper",
431c99
				(script[0] == '/') ? "" : "./",
e956c8
				script)
e956c8
			>= sizeof(wrapper)) {
e956c8
		slbt_free_exec_ctx(actx);
a47c7a
		return SLBT_BUFFER_ERROR(dctx);
e956c8
	}
e956c8
e956c8
	/* exeref */
e956c8
	if ((base = strrchr(script,'/')))
e956c8
		base++;
e956c8
	else
e956c8
		base = script;
e956c8
e956c8
	strcpy(exeref,script);
e956c8
	mark = exeref + (base - script);
e956c8
	sprintf(mark,".libs/%s",base);
e956c8
e956c8
	/* swap vector */
1ed71a
	if (!(stat(script,&st)) && !(stat(wrapper,&st))) {
1ed71a
		ectx->cargv[0] = wrapper;
1ed71a
		ectx->cargv[1] = program;
1ed71a
		ectx->cargv[2] = exeref;
1ed71a
	} else {
1ed71a
		script = program;
1ed71a
	}
e956c8
e956c8
	/* execute mode */
e956c8
	ectx->program = script;
e956c8
	ectx->argv    = ectx->cargv;
e956c8
e956c8
	/* step output */
e956c8
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
e956c8
		if (slbt_output_execute(dctx,ectx)) {
e956c8
			slbt_free_exec_ctx(actx);
a47c7a
			return SLBT_NESTED_ERROR(dctx);
e956c8
		}
e956c8
1ed71a
	execvp(ectx->cargv[0],ectx->argv);
e956c8
e956c8
	slbt_free_exec_ctx(actx);
a47c7a
	return SLBT_SYSTEM_ERROR(dctx);
e956c8
}