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
malloc()argc/2arrays. So allocate that many.