I’m trying to break up a shell command that contains both pipes (|) and the OR symbols (||) represented as characters in an array with strtok, except, well the OR command could also be two pipes next to each other. Specifically, I need to know when |, ;, &&, or || show up in the command.
Is there a way to specify where one delimiter ends and another begins in strtok, since I know usually the delimiters are one character long and you just list them all out with no spaces or anything in between.
Oh and, is a newline a valid delimiter? Or does strtok only do spaces?
For your purpose,
strtok()is not the correct tool to use; it destroys the delimiter, so you can’t tell what was at the end of a token if someone typesls|wc. It could have been a pipe, a semi-colon, and ampersand, or a space. Also, it treats multiple adjacent delimiters as part of a single delimiter.Look at
strspn()andstrcspn(); both are in standard C and are non-destructive relatives ofstrtok().strtok()is quite happy to use newline as a delimiter; in fact, any character except'\0'can be used as one of the delimiters.There are other reasons for being extremely cautious about using
strtok(), such as thread safety and the fact that it is highly unwise to use it in library code.