I have a function:
void x(const char* array[]) {
int index = 0, j = 0;
int fd[2];
pipe(fd);
while (array[index] != NULL && array[index + 1] != NULL) {
const char* arr[], int i = 0;
while(array[index] != NULL) {
arr[i] = array[index];
i++;
index++;
}
arr[i] = NULL;
if (j == 0 && fork() == 0) {
execv(arr[0], arr);
// store the output in a pipe somehow
break;
} else if (fork() == 0) {
execv(arr[0], //arguments from the output of the prev path
// store this execv's output again
break;
}
j++;
}
}
The array consists of paths to multiple processes, its arguments and each process terminates with a NULL. The entire array also terminates with a NULL. e.g. { path1, arg1, NULL, path2, arg2, NULL, NULL }
I want to use pipes to connect the paths together, e.g. path1 arg1 | path2 arg2
Given a variable number of paths, and arguments how should I go about programming this in C? I have no idea if I am in the right path, and I think I have to do the commented out parts using dup2 but I am not sure how).
Thanks!