Redfoxmoon / cross / slibtool

Forked from cross/slibtool 10 months ago
Clone

Blame src/internal/slibtool_spawn_impl.h

9fc046
/*******************************************************************/
9fc046
/*  slibtool: a skinny libtool implementation, written in C        */
9fc046
/*  Copyright (C) 2016  Z. Gilboa                                  */
9fc046
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
9fc046
/*******************************************************************/
9fc046
22fe25
#include <limits.h>
9fc046
#include <unistd.h>
9fc046
#include <stdbool.h>
9fc046
#include <sys/wait.h>
9fc046
22fe25
#ifndef PATH_MAX
22fe25
#define PATH_MAX (_XOPEN_PATH_MAX < 4096) ? 4096 : _XOPEN_PATH_MAX
22fe25
#endif
22fe25
9fc046
#ifndef SLBT_USE_FORK
9fc046
#ifndef SLBT_USE_VFORK
9fc046
#ifndef SLBT_USE_POSIX_SPAWN
9fc046
#define SLBT_USE_POSIX_SPAWN
9fc046
#endif
9fc046
#endif
9fc046
#endif
9fc046
9fc046
#ifdef  SLBT_USE_POSIX_SPAWN
9fc046
#include <spawn.h>
9fc046
#endif
9fc046
9fc046
extern char ** environ;
9fc046
9fc046
static inline int slbt_spawn(
9fc046
	struct slbt_exec_ctx *	ectx,
9fc046
	bool			fwait)
9fc046
{
9fc046
	pid_t	pid;
9fc046
9fc046
9fc046
#ifdef SLBT_USE_POSIX_SPAWN
9fc046
9fc046
	if (posix_spawnp(
9fc046
			&pid,
9fc046
			ectx->program,
9fc046
			0,0,
9fc046
			ectx->argv,
9fc046
			ectx->envp ? ectx->envp : environ))
9fc046
		pid = -1;
9fc046
9fc046
#else
9fc046
9fc046
#ifdef SLBT_USE_FORK
9fc046
	pid = fork();
9fc046
#else
9fc046
	pid = vfork();
9fc046
#endif
9fc046
9fc046
#endif
9fc046
9fc046
	if (pid < 0)
9fc046
		return -1;
9fc046
9fc046
	if (pid == 0)
9fc046
		return execvp(
9fc046
			ectx->program,
9fc046
			ectx->argv);
9fc046
9fc046
	ectx->pid = pid;
9fc046
9fc046
	if (fwait)
9fc046
		return waitpid(
9fc046
			pid,
9fc046
			&ectx->exitcode,
9fc046
			0);
9fc046
9fc046
	return 0;
9fc046
}