I am writing my own basic C shell (for an assignment), and I am required to parse the command line arguments.
I thought to use strtok()
and to use " -|><"
etc. as my delimiters.
For example:
int main ()
{
char str[] = readLine(); // readLine() returns a char* from user input
char * pch;
pch = strtok (str," -|><");
while (pch != NULL) {
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
This will successfully split up the input based on the delimiters, but I won't be able to also store which delimiters were actually used. Since I need to act based on the delimiters provided, I need to also parse the delimiters.
Is there a way to do this with strtok()
? If not, is there another function/way that would best accomplish this?