Redfoxmoon / cross / slibtool

Forked from cross/slibtool a year ago
Clone

9fc046 slbt_spawn(): initial implementation.

Authored and Committed by midipix 8 years ago
    slbt_spawn(): initial implementation.
    
        
file modified
+1 -0
project/headers.mk CHANGED
@@ -5,5 +5,6 @@ API_HEADERS = \
5
5
INTERNAL_HEADERS = \
6
6
$(PROJECT_DIR)/src/internal/argv/argv.h \
7
7
$(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \
8
+ $(PROJECT_DIR)/src/internal/$(PACKAGE)_spawn_impl.h \
8
9
9
10
ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS)
src/internal/slibtool_spawn_impl.h ADDED
@@ -0,0 +1,69 @@
1
+ /*******************************************************************/
2
+ /* slibtool: a skinny libtool implementation, written in C */
3
+ /* Copyright (C) 2016 Z. Gilboa */
4
+ /* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
5
+ /*******************************************************************/
6
+
7
+ #include <unistd.h>
8
+ #include <stdbool.h>
9
+ #include <sys/wait.h>
10
+
11
+ #ifndef SLBT_USE_FORK
12
+ #ifndef SLBT_USE_VFORK
13
+ #ifndef SLBT_USE_POSIX_SPAWN
14
+ #define SLBT_USE_POSIX_SPAWN
15
+ #endif
16
+ #endif
17
+ #endif
18
+
19
+ #ifdef SLBT_USE_POSIX_SPAWN
20
+ #include <spawn.h>
21
+ #endif
22
+
23
+ extern char ** environ;
24
+
25
+ static inline int slbt_spawn(
26
+ struct slbt_exec_ctx * ectx,
27
+ bool fwait)
28
+ {
29
+ pid_t pid;
30
+
31
+
32
+ #ifdef SLBT_USE_POSIX_SPAWN
33
+
34
+ if (posix_spawnp(
35
+ &pid,
36
+ ectx->program,
37
+ 0,0,
38
+ ectx->argv,
39
+ ectx->envp ? ectx->envp : environ))
40
+ pid = -1;
41
+
42
+ #else
43
+
44
+ #ifdef SLBT_USE_FORK
45
+ pid = fork();
46
+ #else
47
+ pid = vfork();
48
+ #endif
49
+
50
+ #endif
51
+
52
+ if (pid < 0)
53
+ return -1;
54
+
55
+ if (pid == 0)
56
+ return execvp(
57
+ ectx->program,
58
+ ectx->argv);
59
+
60
+ ectx->pid = pid;
61
+
62
+ if (fwait)
63
+ return waitpid(
64
+ pid,
65
+ &ectx->exitcode,
66
+ 0);
67
+
68
+ return 0;
69
+ }