This might be a start, if you really need it to be inlined.
Code:
//outer.c
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
int pipes[2] = { -1, -1 };
pipe(pipes);
pid_t new_process = fork();
if (new_process < 0) return 1;
if (new_process == 0)
{
dup2(pipes[1], 3);
if (pipes[0] != 3) close(pipes[0]);
if (pipes[1] != 3) close(pipes[1]);
execvp(argv[1], argv + 1);
}
else
{
close(pipes[1]);
waitpid(new_process, NULL, 0x00);
int outcome = 0;
read(pipes[0], &outcome, sizeof(outcome));
return outcome;
}
return 1;
}
Code:
//inner.c
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int outcome = WEXITSTATUS( system(argv[1]) );
write(3, &outcome, sizeof(outcome));
return 0;
}
Code:
bash> gcc outer.c -o outer
bash> gcc inner.c -o inner
bash> ./outer xterm -e "./inner 'ls nothere'" || echo "'nothere' isn't here"
'nothere' isn't here
bash> ./outer xterm -e "./inner 'ls outer'" && echo "'outer' is here"
'outer' is here
It doesn't have the normal error checks, but it's a general idea of how you might do it.
ta0kira