I made a INI file parser, it works well on Windows but not with Linux, the problem comes from the memcmp function, it doesn't return 0 when it should, I already checked with printf and strlen, (I also tried to use strncmp instead, it returned different value but still different than 0) but couldn't find where the problem comes from.
Here is the code :
#define INI_KEY_LENGTH 128
#define BEGIN '['
#define SEP '='
#define COMMENT ';'
static char configfilename[255];
static char * str_dup (const char * str){
char * dup = NULL;
if (str != NULL){
size_t size = strlen (str) + 1;
dup = malloc (size);
if (dup != NULL){
memcpy (dup, str, size);
}
}
return dup;
}
static void str_finalize (char * str){
char * p = strchr (str, '\n');
if (p != NULL){
*p = '\0';
}
}
static char * get (const char * filename, const char * section, const char * key){
char * ret = NULL;
char buff [INI_KEY_LENGTH];
FILE * file = NULL;
int opened = 0;
file = fopen (filename, "r");
if (file != NULL){
while ((fgets (buff, INI_KEY_LENGTH, file)) != NULL){
str_finalize (buff);
if (! opened && buff [0] == BEGIN){
char * p = buff;
// Don't work here
if (memcmp (p + 1, section, strlen (buff) - 2) == 0){
opened = 1;
continue;
}
}
else if (opened){
if (buff [0] == BEGIN){
opened = 0;
break;
}
if(buff [0] != COMMENT){
if (strstr (buff, key) != NULL){
char * p = strchr (buff, SEP);
if (p != NULL){
p++;
ret = str_dup (p);
break;
}
}
}
}
}
fclose (file);
}
return ret;
}
int init_iniFile (const char * filename){
int ret = 0;
FILE * file;
if ((file = fopen(filename, "r")) != NULL){
fclose(file);
strcpy(configfilename, filename);
ret = 1;
}
return ret;
}
char * get_string (const char * section, const char * key){
return get (configfilename, section, key);
}
I suspect the error to be stupid, but I'm a noob in C, sorry for the inconvenience.