In some CGI code, I need to encode rarely-occurring ‘&’, ‘<‘, and ‘>’ chars. In the encoding function, I want to get out right away if there are no such chars in the input string. So, at entry, I try to use strtok( ) to find that out:
char *
encode_amp_lt_gt ( char *in ) {
...
if ( NULL == strtok( in, "&<>" )) {
return in;
}
...
}
But, even in the absence of any of the delimiters, strtok( ) returns a pointer to the first character of in.
I expected it to return NULL if no delims in the string.
Is my code wrong, or is my expectation wrong? I don’t want to call strchr( ) three times just to eliminate the usual case.
Thanks!
The function you want is
strpbrk, notstrtok. The bigger question is – how is the string that is being returned being allocated when you’re replacing things, and how does the calling function know if it should free it or not?