#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Ireland
    Posts
    1
    Rep Power
    0

    Question help with CGI script in C


    I have the following code written to search a document for occurances of a string ( eventually will be searching a folder of documents, but thats another problem)
    This code works perfect when compiled in Visual C++ but when I try to convert to cgi, the first strstr line lets me down......absolutely lost on this one......
    Cheers............................PJG
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAXLEN 80
    #define EXTRA 5 /* 4 for field name "data", 1 for "=" */
    #define MAXINPUT MAXLEN+EXTRA+2 /* 1 for added line break, 1 for trailing NUL */


    void unencode(char *src, char *last, char *dest)
    {
    for(; src != last; src++, dest++)
    if(*src == '+')
    *dest = ' ';
    else if(*src == '%') {
    int code;
    if(sscanf(src+1, "%2x", &code) != 1) code = '?';
    *dest = code;
    src +=2; }
    else
    *dest = *src;
    *dest = '\n';
    *++dest = '\0';
    }


    int main(void)
    { /* begin program */



    char line[250];
    const char *delimiters = {" \t \n "};
    char *token=NULL;
    char* pStr = NULL;
    int count = 0, line_count=0;
    char *test = "";
    FILE *essay;
    char *lenstr;
    char input[MAXINPUT], data[MAXINPUT];
    long len;


    printf("%s%c%c\n",
    "Content-Type:text/html;charset=iso-8859-1",13,10);
    printf("<TITLE>Response</TITLE>\n");


    lenstr = getenv("CONTENT_LENGTH"); /* Getting string from HTML page */

    if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN) /*verifying that string is valid*/
    printf("<P>Error in invocation - wrong FORM probably.");


    else { /* begin ELSE */

    fgets(input, len+1, stdin);
    unencode(input+EXTRA, input+len, data);



    if ((essay = fopen("C:/Inetpub/wwwroot/cgi-bin/stroke.txt", "r")) == NULL )
    {
    printf("<h2>File could not be opened - ask yourself WHYYYYY?");
    }
    else



    while (!feof(essay))
    {
    if( fgets(line, 250, essay) != NULL )
    {


    pStr = line;
    line_count++;


    if (( test = strtok(pStr, delimiters )) != NULL )
    {

    if(strstr(test,data) !=NULL)



    {

    count++;
    printf("<P>Entry %d on line %d: %s<br>", count, line_count, data);
    }

    while (( test = strtok((char *)NULL, delimiters )) != NULL)
    {
    if ( strstr(test,data) != NULL)
    {
    count++;
    printf("<P> Entry %d on line %d: %s<br>", count, line_count, data);
    }
    }
    }
    }


    }

    printf("<H3>Files were searched successfully.... how about that!");
    printf("<P>The string <H4>%s</H4> occurs %d times in %s <BR>", data, count, essay);

    fclose(essay);

    count =0;


    }


    return 0;
    } /* End Program */
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    Does it work if you do not call unencode() ?

IMN logo majestic logo threadwatch logo seochat tools logo