sscanf and memory allocation
Hello All,
I have the following code:
Code:
if(f_peri) {
c = 0;
k = 0;
char *ptr_periodicidade = periodicidade;
printf("f_peri: [%d]\n",f_peri);
strcpy(field_second,"");
printf("1->field_second: [%s]\n",field_second);
printf("c: [%d]\n",c);
while ( sscanf(ptr_periodicidade, "%50[^,]%*c%n", field_second, &k) == 1 )
{
printf("k: [%d]\n",k);
printf("2->field_second: [%s]\n",field_second);
ptr_periodicidade += k;
switch (c) {
default:
strcpy(data[c_line].periodicidade,field_second);
printf("second[%d]: [%s]\n",c,field_second);
break;
}
strcpy(data[c_line].path,path);
c++;
c_line++;
printf("c_line: [%d]\n",c_line);
}
strcpy(periodicidade,"");
strcpy(path,"");
} else {
c_line++;
}
That generate's this:
field[0]: [1,10,20,30,40,50]
default->field[1]: [/tmp/echo2.sh]
f_peri: [1]
1->field_second: []
c: [0]
k: [2]
2->field_second: [1]
second[0]: [1]
c_line: [1]
k: [3]
2->field_second: [10]
second[1]: [10]
c_line: [2]
k: [3]
2->field_second: [20]
second[2]: [20]
c_line: [3]
k: [3]
2->field_second: [30]
second[3]: [30]
c_line: [4]
k: [3]
2->field_second: [40]
second[4]: [40]
c_line: [5]
k: [3]
2->field_second: [50]
second[5]: [50]
c_line: [6]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&
field[0]: [25,55]
default->field[1]: [/usr/bin/php]
default->field[2]: [/html/echo4.php]
f_peri: [1]
1->field_second: []
c: [0]
k: [3]
2->field_second: [25]
second[0]: [25]
c_line: [7]
k: [3]
2->field_second: [55]
second[1]: [55]
c_line: [8]
k: [2]
2->field_second: [0]
second[2]: [0]
c_line: [9]
k: [3]
2->field_second: [30]
second[3]: [30]
c_line: [10]
k: [3]
2->field_second: [40]
second[4]: [40]
c_line: [11]
k: [3]
2->field_second: [50]
second[5]: [50]
c_line: [12]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
data[0].periodicidade: [1]
data[0].path: [/tmp/echo2.sh ]
data[1].periodicidade: [10]
data[1].path: [/tmp/echo2.sh ]
data[2].periodicidade: [20]
data[2].path: [/tmp/echo2.sh ]
data[3].periodicidade: [30]
data[3].path: [/tmp/echo2.sh ]
data[4].periodicidade: [40]
data[4].path: [/tmp/echo2.sh ]
data[5].periodicidade: [50]
data[5].path: [/tmp/echo2.sh ]
data[6].periodicidade: [25]
data[6].path: [/usr/bin/php /html/echo4.php ]
data[7].periodicidade: [55]
data[7].path: [/usr/bin/php /html/echo4.php ]
data[8].periodicidade: [0]
data[8].path: [/usr/bin/php /html/echo4.php ]
data[9].periodicidade: [30]
data[9].path: [/usr/bin/php /html/echo4.php ]
data[10].periodicidade: [40]
data[10].path: [/usr/bin/php /html/echo4.php ]
data[11].periodicidade: [50]
data[11].path: [/usr/bin/php /html/echo4.php ]
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
All that info is stored in the following struct:
Code:
struct MyData {
char periodicidade[50];
char path[400];
int controle;
};
The two lines beeing processed are:
1,10,20,30,40,50 /tmp/echo2.sh
25,55 /usr/bin/php /html/echo4.php
As we can see in the output the first line is splited ok but the second line that has only two args (25 55) is completed with args 0, 30, 40 and 50 at the end. I think that last 3 numbers are on memory for pointer ptr_periodicidade.
I've tryed to free the memory for that pointer with out sucess. Can any one help out ?
Thanks alot,