Home > Back-end >  Create arguments from command line and save in an array
Create arguments from command line and save in an array

Time:12-08

I'm trying to save my arguments and their parameters from the command line as follows

./run cat hello.txt : grep left : wc -c

I want to seperate each argument in an array as follows withouth knowing the number of :

char *cat_args[] = {"cat", "tests/nevermind", NULL};
char *grep_args[] = {"grep", "left", NULL};
char *cut_args[] = {"wc", "-c", NULL};

How can I achieve this ?

int nbProc = 2;
for (int i = 0; i < argc; i  ){
        if (strcmp(argv[i], ":") == 0){
            nbProc  ;
        }
    }

int indice_debut[nbProc-2];
int j = 0;
for (int i = 1; i < argc; i  ){
        if (strcmp(argv[i], ":") == 0){
            argv[i] = NULL;
            indice_debut[j] = i   1;
            j  ;
        }
    }

With this i'm able to get indice_debut = {4,7} because there is : in 4th and 7th position.

I tried to run it as this but no luck, i'm doing this so i can use execvp.

execvp(argv[indice_debut[0]], argv indice_debut[0]);

Thanks

CodePudding user response:

Allocate an array of pointers dynamically with malloc(). There can be at most argc/2 commands, since the worst case is alternating word : word : word : ..., so allocate that many elements. The array elements can point to the elements of argv, and you can replace the : argument with a null pointer to end each subcommand.

int main(int argc, char **argv) {
    if (argc < 2) {
        printf("At least one command is required\n");
        exit(1);
    }

    char **arg_arrays = malloc(argc/2 * sizeof *arg_arrays);
    int array_index = 0;
    int arg_index = 0;
    while (arg_index < argc) {
        arg_arrays[array_index  ] = &argv[arg_index];
        for (; arg_index < argc && strcmp(argv[arg_index], ":") != 0; arg_index  ) {}
        argv[arg_index] = NULL;
    }

    // Execute each of the subcommands
    for (int i = 0; i < array_index; i  ) {
        pid_t pid = fork();
        if (pid == 0) {
            execvp(arg_arrays[i][0], arg_arrays[i]);
        } else if (pid < 0) {
            perror("fork");
            exit(1);
        }
    }
    // wait for all the subcommands to finish
    while (wait(NULL) > 0) {}
}

This is just a simple example of how to parse the subcommands and execute them. If you want to pipe from one command to the next one you'll need to add that code.

  • Related