0

I am using this :

  if(strstr(inData, respond) != NULL)

to search for the string CMD inside inData .

problem is that when new data looks like this :

Reboot
CMD

strstr will not find it, because it stops searching where is a new line. Is there an elegant way (not using fancy functions since I can't) to get over it ?

Curnelious
  • 1
  • 16
  • 76
  • 150
  • 3
    `strstr` can find newline(include). There is an illegal character in front of the `CMD`. (maybe `'\0'`) – BLUEPIXY Oct 02 '16 at 12:37
  • Thanks, how I don't see it ? what I show here is the inData print. – Curnelious Oct 02 '16 at 12:37
  • hex dump `inData`. – BLUEPIXY Oct 02 '16 at 12:41
  • 1
    I agree with @BLUEPIXY, `strstr()` treats newlines like any other character. However, I would also expand your debugging to `respond` as well. Are you sure `respond` points to the characters `C`, `M`, `D`, `\0`? If `respond` contains a newline, or does not contain a NUL delimiter (`\0`), `strstr()` will similarly fail. – type_outcast Oct 02 '16 at 12:44
  • Thanks guys, I am loosing my mind with it, I guess there is something I can not see even if I hex dump it. – Curnelious Oct 02 '16 at 13:18

1 Answers1

4

As pointed out in the comments, strstr does not stop at newline boundaries, but only at string terminators (\0)

Here is the relevant section from the man page:

DESCRIPTION The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared.

So the assumption is, that there are null bytes within one of CMD or inData. You should investigate the string inData and CMD in this way or hexdump it (as suggested by @BLUEPIXY)

Community
  • 1
  • 1
Gernot
  • 384
  • 1
  • 3
  • 11