0

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?

chepner
  • 497,756
  • 71
  • 530
  • 681
tam5
  • 3,197
  • 5
  • 24
  • 45
  • 1
    Have a look at `strcspn()` which finds the first index of any one of the set of characters passed to it. – Weather Vane Dec 06 '15 at 20:44
  • Don't use `strtok`. It's not safe. http://stackoverflow.com/questions/5999418/why-is-strtok-considered-unsafe . Try `strstr` or `strchr`. Just for the record, I wouldn't use none of them for this purpose. You need something similar to a parser, which I'd better accomplish just with pointers to the string being parsed, or maybe a already done parser library. – emi Dec 06 '15 at 22:11
  • Don't you have to take quoting into account? – Armali Aug 22 '17 at 08:10

0 Answers0