Question

Im struggling with finding the start of a substring in a u_char*, im trying to get the position of "q=" to parse google searches from URL This is driving me insane, I would Really appreciate if anybody could help me out. my C knowledge is limited.

This is the line im stuck at: if(g_start = strstr(final_url, "q=") != NULL){

Here is parts of my code:

int  url_length,host_length, search_length;
u_char *url, *end_url, *final_url, *host, *end_host, *final_host, *ghostUrl, *google, *g_end, *g_start;


/* copy  host + url to a null terminated c string */
final_url = (u_char*)malloc(url_length + 1); //malloc ( length )  +1 for \0
final_host = (u_char*)malloc(host_length +1);
strncpy((char*)final_host,(char*)host, host_length);
strncpy((char*)final_url, (char*)url, url_length);
final_url[url_length] = '\0'; //null terminerer
final_host[host_length] = '\0';

    /* Everything above works fine.. */
   /* final_url looks like this: /search?biw=320&bih=570&tbm=isch&sa=1&ei=M0eFUsXvHOO54AS644GYCQ&q=dogs&oq=dogs&gs_l=mobile-gws-serp.12...0.0.0.1991659.0.0.0.0.0.0.0.0 */

if(strcmp("www.google.no", final_host) == 0){ /* host: www.google.no */
    if(g_start = strstr(final_url, "q=") != NULL){ /* This fails.. */       
printf("debug: search starts at %s\n", g_start); /* I have no idea.. how to get integer position from strstr()  */
    g_end = strchr((char*)g_start,'&'); // ?q=dogs[&]
    search_length = g_end - g_start;

    printf("DEBUG:  search_length: %i\n",search_length);
    google = (u_char*)malloc(search_length +1);     
    strncpy((char*)google, (char*)g_start, search_length);
    google[search_length] = '\0'; //null terminator
Was it helpful?

Solution

Operator precedence means that

if(g_start = strstr(final_url, "q=") != NULL)

is evaluated as

if(g_start = (strstr(final_url, "q=") != NULL))

which will assign 1 or 0 to g_start.

You need to use parenthesis () around the start of the line to get the order of evaluation you need:

if((g_start = strstr(final_url, "q=")) != NULL)
// ^                                 ^

Note that your compiler should have warned you about this. e.g. MSVC gives

warning C4047: '=' : 'char *' differs in levels of indirection from 'int'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top