From 9fc04630472ab18b2338050714ee0419281227e1 Mon Sep 17 00:00:00 2001 From: midipix Date: Wed, 9 Mar 2016 07:19:29 -0500 Subject: slbt_spawn(): initial implementation. --- project/headers.mk | 1 + src/internal/slibtool_spawn_impl.h | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/internal/slibtool_spawn_impl.h diff --git a/project/headers.mk b/project/headers.mk index 1a210ff..119055a 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -5,5 +5,6 @@ API_HEADERS = \ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/argv/argv.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_spawn_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/src/internal/slibtool_spawn_impl.h b/src/internal/slibtool_spawn_impl.h new file mode 100644 index 0000000..b376ad8 --- /dev/null +++ b/src/internal/slibtool_spawn_impl.h @@ -0,0 +1,69 @@ +/*******************************************************************/ +/* slibtool: a skinny libtool implementation, written in C */ +/* Copyright (C) 2016 Z. Gilboa */ +/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */ +/*******************************************************************/ + +#include +#include +#include + +#ifndef SLBT_USE_FORK +#ifndef SLBT_USE_VFORK +#ifndef SLBT_USE_POSIX_SPAWN +#define SLBT_USE_POSIX_SPAWN +#endif +#endif +#endif + +#ifdef SLBT_USE_POSIX_SPAWN +#include +#endif + +extern char ** environ; + +static inline int slbt_spawn( + struct slbt_exec_ctx * ectx, + bool fwait) +{ + pid_t pid; + + +#ifdef SLBT_USE_POSIX_SPAWN + + if (posix_spawnp( + &pid, + ectx->program, + 0,0, + ectx->argv, + ectx->envp ? ectx->envp : environ)) + pid = -1; + +#else + +#ifdef SLBT_USE_FORK + pid = fork(); +#else + pid = vfork(); +#endif + +#endif + + if (pid < 0) + return -1; + + if (pid == 0) + return execvp( + ectx->program, + ectx->argv); + + ectx->pid = pid; + + if (fwait) + return waitpid( + pid, + &ectx->exitcode, + 0); + + return 0; +} -- cgit v1.2.3