0

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!

Tess
  • 19
  • 2
  • Duplication of [this](https://stackoverflow.com/questions/15784729/an-example-of-use-of-varargs-in-c) & [This](https://stackoverflow.com/questions/2421062/c-dealing-with-variable-argument-functions) – ntshetty Sep 14 '20 at 03:17
  • Show us what you have so far. StackOverflow is not a coding service. ;-) You might like to fresh up your understanding by taking the [tour] and reading "[ask]". -- @ntshetty I don't think so. Where do you see variadic arguments in the OP's question? – the busybee Sep 14 '20 at 06:13
  • Hi, I have updated my question accordingly. Am I going in the right direction? – Tess Sep 15 '20 at 01:33

0 Answers0