tags:

views:

55

answers:

1

Hi All,

I am using an .ini file to store some values and retrieve values from it using the iniparser.

When I give (hardcode) the query and retrive the value through the command line, I am able to retrive the ini file and do some operation.

But when I pass the query through http, then I am getting an error (file not found), i.e., the ini file couldn't be loaded.


  • Command line :

int main(void)
{
   printf("Content-type: text/html; charset=utf-8\n\n");

   char* data = "/cgi-bin/set.cgi?pname=x&value=700&url=http://IP/home.html";

   //perform some operation
}

  • Through http:

.html

function SetValue(id)
{
    var val;
    var URL = window.location.href;
    if(id =="set")
    {
        document.location = "/cgi-bin/set.cgi?pname="+rwparams+"&value="+val+"&url="+URL;
    }
}

  • .c

int * Value(char* pname)
{
    dictionary * ini ;
    char *key1 = NULL;
    char *key2 =NULL;
    int i =0;

    int val;

    ini = iniparser_load("file.ini");
    if(ini != NULL)
    {
        //key for fetching the value
        key1 = (char*)malloc(sizeof(char)*50);
        if(key1 != NULL)
        {                   
                strcpy(key1,"ValueList:");
                key2 = (char*)malloc(sizeof(char)*50);
                if(key2 != NULL)
                {
                    strcpy(key2,pname);
                    strcat(key1,key2);                  
                    val = iniparser_getint(ini, key1, -1);
                    if(-1 == val || 0 > val)
                    {
                        return 0;                       
                    }
                }
                else
                {
                    //error
                    free(key1);                     
                    return;
                }           
        }       
        else
        {   
            printf("ERROR : Memory Allocation Failure ");
            return;
        }

    }
    else
    {
        printf("ERROR : .ini File Missing");
        return;
    }
    iniparser_freedict(ini);
    free(key1);
    free(key2);
    return (int *)val;
}

void get_Value(char* pname,char* value)
{
        int result =0;                          
        result = Value(pname);
        printf("Result : %d",result);           
}

int main(void)
{
    printf("Content-type: text/html; charset=utf-8\n\n");

    char* data = getenv("QUERY_STRING");    
    //char* data = "/cgi-bin/set.cgi?pname=x&value=700&url=http://10.50.25.40/home.html";

    //Parse to get the values seperately as parameter name, parameter value, url

    //Calling get_Value method to set the value
    get_Value(final_para,final_val);

}

*

  • file.ini

*

[ValueList]

x   = 100;
y   = 70;

When the request is sent through html page, I am always getting .ini file missing. If directly the request is sent from C file them it works fine.

How to resolve this?

A: 

Perhaps you have a problem with encoding of the URL parameters? You can't just pass any arbitrary string through a URL - there are some characters that must be encoded. Read this page about URL encoding.

Showing the value of the data string in your C program could be of great help with solving your problem.


Update:

There could be a difference as to where your program executes when called by the web server or directly by you. Are you sure it's being executed with the same "current directory". Chances are it's different, and thus when you attempt to open the ini file you fail. Try to print out the current directory (i.e. using the getcwd function) and compare both cases.

Eli Bendersky
The same kind of stuff works with another .ini file for different operation. Whether, it really matters with the url encoding?
MalarN
@MalarN: I don't know - it's possible. You don't provide enough information to help you here. Produce the smallest example that shows the problem and give us the C code and .ini file, and .html page
Eli Bendersky
I have added the additional information.
MalarN
@MalarN: OK, I've updated the answer to reflect the new information
Eli Bendersky
I tried to print the current directory, it is printing correctly.When I run in command prompt, I will place all the files in /var/www/html folder. Now when using html, cgi I use /var/www/cgi-bin directory. I have placed the .cgi, .ini files here. I am able to retrieve one more ini file from the same location but not this file.
MalarN