1 #include <errno.h> 2 #include <spawn.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 7 8 int main() 9 { 10 11 char* _args[4]; 12 char* _env[] = { "myenv=5", NULL }; 13 14 _args[0] = "bash"; 15 _args[1] = "-c"; 16 _args[2] = "exit $myenv"; 17 _args[3] = NULL; 18 19 pid_t pid; 20 int err = posix_spawnp(&pid, _args[0], NULL, NULL, _args, _env); 21 printf("posix_spawnp: %d, %d\n", err, pid); 22 23 int status; 24 pid_t waitpid_res = waitpid(pid, &status, 0); 25 if (waitpid_res != pid) 26 printf("posix_spawnp: waitpid didn't return pid\n"); 27 28 printf("posix_spawnp: WIFEXITED(): %d, WEXITSTATUS() %d => 5\n", 29 WIFEXITED(status), WEXITSTATUS(status)); 30 31 _args[0] = "/tmp/toto"; 32 _args[1] = NULL; 33 34 err = posix_spawn(&pid, _args[0], NULL, NULL, _args, _env); 35 printf("posix_spawn: %d, %d\n", err, pid); 36 37 if (err == 0) { 38 waitpid_res = waitpid(pid, &status, 0); 39 if (waitpid_res != pid) 40 printf("posix_spawn: waitpid didn't return pid\n"); 41 printf("posix_spawn: WIFEXITED(): %d, WEXITSTATUS() %d => 127\n", 42 WIFEXITED(status), WEXITSTATUS(status)); 43 } else { 44 waitpid_res = waitpid(-1, NULL, 0); 45 printf("posix_spawn: waitpid %d, %d\n", waitpid_res, errno); 46 } 47 48 return 0; 49 } 50 51