Hi all.
I'm working on an embedded application for OpenSlug (Linksys NSLU2). I'm currently porting some CGI's that already worked for an ARM7 platform but something is wrong.
These cgi's are basically parsing the string POSTED from a precedent cgi and copying the results into different local buffers:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRING_KEY 1
#define STRING_VALUE 2
//*********************************************************************************************
// Main
// main function of the cgi
//*********************************************************************************************
int main(void)
{
char *strLen;
long intTotLen, intReadLen = 0;
char chr;
char strKey[20];
char strValue[120];
short int flgStrType = STRING_KEY;
int i = 0;
// html header
printf("<html>\n");
printf("<head></head>\n");
// Get the legth of the packet sent by the POST method
strLen = getenv("CONTENT_LENGTH");
intTotLen = atoi(strLen);
// Only if received anything
if (intTotLen > 0)
{
// While any character to read from the POST string
while (intReadLen < intTotLen)
{
chr = fgetc(stdin); // Capture the following character
intReadLen++; // Increment the number of characters read
printf("%c",chr); // print character
// Evaluate the captured character
switch(chr)
{
case '&':
strValue[i] = 0; // Terminate string
flgStrType = STRING_KEY; // Ready to find a new value
i = 0; // Go for a new string
break;
case '=':
strKey[i] = 0; // Terminate string
flgStrType = STRING_VALUE; // Ready to find a new value
i = 0; // Go for a new string
break;
default:
if (flgStrType == STRING_KEY)
strKey[i] = chr; // Store the character in the keyword buffer
else
strValue[i] = chr; // Store the character in the value buffer
break;
}
}
strValue[i]= 0; // Terminate the last value string
printf("<br>strKey = %s strValue = %s<br>", strKey, strValue);
}
// End of the html body
printf("</body>\n");
printf("</html>\n");
return(0);
}
This cgi outputs the following result:
action=Create+event
strKey = action strValue = wÿŽ@Create
However, if I declare strvalue as static (static char strValue[100]) then the problem gets solved. I've encountered similar problems on other embedded platforms and most of the times this is due to memory misalignments but NSLU2's processor has a MMU and the global arrays have been manually aligned as well.
Please, I'm open to any kind of technical suggestions.
thank you very much,
Daniel.