PINE64
< and > as characters in C? - Printable Version

+- PINE64 (https://forum.pine64.org)
+-- Forum: Pinebook Pro (https://forum.pine64.org/forumdisplay.php?fid=111)
+--- Forum: General Discussion on Pinebook Pro (https://forum.pine64.org/forumdisplay.php?fid=112)
+--- Thread: < and > as characters in C? (/showthread.php?tid=12824)



< and > as characters in C? - ab1jx - 01-18-2021

I was trying to write a filter to locate some things in HTML and I was quite surprised by this.  If I do strstr() to search for <label> it's not found, but grep or search in the mc file viewer can find it.
I even tried putting them in as ASCII codes: snprintf(lbl,20,"%clabel%c",0x3c,0x3e); and it doesn't find them in the HTML.  If I look at the HTML in hex mode of mc's file viewer they seem to be normal ASCII, not unicode.

This is nothing specific to a PBP except that I'm using gcc under Daniel Thompson's Bullseye on one.  I tried escaping them by putting a \ in front of each one, that doesn't work.  I'm pretty sure I've searched for HTML tags in C years ago, I don't remember this strangeness.


RE: < and > as characters in C? - ab1jx - 01-18-2021

Well, even clang works the same way. Or doesn't work.


RE: < and > as characters in C? - barray - 01-18-2021

@ab1jx This is a coding problem, not a Pinebook Pro problem.

In the case you want help, you should post a small snippet of your code that demonstrates the problem. I highly doubt both gcc and clang have some problem, especially given all the programs running on your machine were compiled with one of those and all seem to work.

Regarding the search function strstr() [1]:

1. Sanity check that the example given in tutorialspoint works [1] - if it doesn't, you really have massive problems.

2. Does the haystack contain the full string? Try printing it... Perhaps you are not getting the result you think you are from the GET request.

3. Is there a NULL character `\0` early on in the string?

4. Remember that strstr() is going to return a pointer to the offset where the string was found - not the offset in the string, so you need to check it against NULL. If you try to do something like `haystack[strstr(haystack, needle)]` you will almost certainly segmentation fault.

[1] https://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm


RE: < and > as characters in C? - ab1jx - 01-19-2021

Yeah, I just wrote this little test and it works OK.
Code:
/*
  Show the bug
*/

#include <string.h>
#include <stdio.h>

char testdata[] = "foo xyzzy <tag> nonsense <label> junk more junk";

int main(void) {
  if (strstr(testdata,"<tag>") == NULL)
    printf("tag not found\n");
  else
    printf("tag found\n");
  if (strstr(testdata,"<label>") == NULL)
    printf("label not found\n");
  else
    printf("label found\n");
  return 0;
}
The real thing has 5 MB of data, too much to post.


RE: < and > as characters in C? - barray - 01-20-2021

@ab1jx If the small example works, then the problem is something in your code most likely.