Blame src/logic/slbt_exec_execute.c

e956c8
/*******************************************************************/
e956c8
/*  slibtool: a skinny libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
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"
7ae5c1
#include "slibtool_driver_impl.h"
19022e
#include "slibtool_snprintf_impl.h"
a47c7a
#include "slibtool_errinfo_impl.h"
e956c8
2e30eb
int slbt_exec_execute(const struct slbt_driver_ctx * dctx)
e956c8
{
7ae5c1
	int			fdcwd;
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;
2e30eb
	struct slbt_exec_ctx *	ectx;
e956c8
c4a389
	/* dry run */
c4a389
	if (dctx->cctx->drvflags & SLBT_DRIVER_DRY_RUN)
c4a389
		return 0;
c4a389
e956c8
	/* context */
2e30eb
	if (slbt_ectx_get_exec_ctx(dctx,&ectx) < 0)
2e30eb
		return SLBT_NESTED_ERROR(dctx);
2e30eb
2e30eb
	slbt_disable_placeholders(ectx);
e956c8
e956c8
	/* script, program */
e956c8
	program = ectx->cargv[0];
e956c8
	script  = ectx->cargv[1];
e956c8
Lucio Andrés Illanes Albornoz f27e85
	if (script) {
Lucio Andrés Illanes Albornoz f27e85
		/* exeref */
Lucio Andrés Illanes Albornoz f27e85
		if ((base = strrchr(script,'/')))
Lucio Andrés Illanes Albornoz f27e85
			base++;
Lucio Andrés Illanes Albornoz f27e85
		else
Lucio Andrés Illanes Albornoz f27e85
			base = script;
e956c8
Lucio Andrés Illanes Albornoz f27e85
		strcpy(exeref,script);
Lucio Andrés Illanes Albornoz f27e85
		mark = exeref + (base - script);
Lucio Andrés Illanes Albornoz f27e85
		sprintf(mark,".libs/%s",base);
e956c8
70949a
		/* wrapper */
19022e
		if (slbt_snprintf(wrapper,sizeof(wrapper),
70949a
					"%s.exe.wrapper",
19022e
					exeref) < 0) {
2e30eb
			slbt_ectx_free_exec_ctx(ectx);
70949a
			return SLBT_BUFFER_ERROR(dctx);
70949a
		}
70949a
7ae5c1
		/* fdcwd */
7ae5c1
		fdcwd = slbt_driver_fdcwd(dctx);
7ae5c1
Lucio Andrés Illanes Albornoz f27e85
		/* swap vector */
7ae5c1
		if (!fstatat(fdcwd,script,&st,0) && !fstatat(fdcwd,wrapper,&st,0)) {
Lucio Andrés Illanes Albornoz f27e85
			ectx->cargv[0] = wrapper;
Lucio Andrés Illanes Albornoz f27e85
			ectx->cargv[1] = program;
Lucio Andrés Illanes Albornoz f27e85
			ectx->cargv[2] = exeref;
Lucio Andrés Illanes Albornoz f27e85
		} else {
Lucio Andrés Illanes Albornoz f27e85
			script = program;
Lucio Andrés Illanes Albornoz f27e85
		}
Lucio Andrés Illanes Albornoz f27e85
Lucio Andrés Illanes Albornoz f27e85
		/* execute mode */
Lucio Andrés Illanes Albornoz f27e85
		ectx->program = script;
Lucio Andrés Illanes Albornoz f27e85
		ectx->argv    = ectx->cargv;
1ed71a
	} else {
Lucio Andrés Illanes Albornoz f27e85
		ectx->program = program;
Lucio Andrés Illanes Albornoz f27e85
		ectx->argv    = ectx->cargv;
1ed71a
	}
e956c8
e956c8
	/* step output */
e956c8
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
33a569
		if (slbt_output_execute(ectx)) {
2e30eb
			slbt_ectx_free_exec_ctx(ectx);
a47c7a
			return SLBT_NESTED_ERROR(dctx);
e956c8
		}
e956c8
1ed71a
	execvp(ectx->cargv[0],ectx->argv);
e956c8
2e30eb
	slbt_ectx_free_exec_ctx(ectx);
6beda1
	return SLBT_SYSTEM_ERROR(dctx,0);
e956c8
}