I’m trying to program the tar function but i have some problem.
I want to read my tar file and parse header data.
here is my structure
struct header {
char nom[100];
char droits[3];
char taille[12];
char date_modif[12];
char type[1];
};
And now my code to parse header data :
while ( fread(head.nom, 100, 1, archive) != 0 ){
printf("NOM : %s\n", head.nom);
//droits
fread(head.droits, 3, 1, archive);
printf("DROITS : %s\n", head.droits);
//taille
fread(head.taille, 12, 1, archive);
printf("TAILLE : %s\n", head.taille);
//last modif
fread(head.date_modif, 12, 1, archive);
printf("MODIF : %s\n", head.date_modif);
//type
fread(head.type, 1, 1, archive);
printf("TYPE : %s\n", head.type);
printf("NOM : %s\n", head.nom);
exit(1);
// on se place a la fin du premier fichier
fseek(archive, oct2dec(atoi(head.taille)), SEEK_CUR);
//fprintf(stdout, "%s %d \n", head.nom, oct2dec(atoi(head.taille)));
i++;
}
And the result is :
NOM : a.txt...............................................................................................
DROITS : 664
TAILLE : 936.........
MODIF : 1352910882..
TYPE : 1
NOM : a.txt...............................................................................................664936.........1352910882..1
My problem is that the last line display all my data and I want to display only a.txt…. (like in the first line).
How can I do to fix this problem ?
Thanks
This is a buffer overflow issue. You should increase the size of
nomto be able to hold the null character. I don’t know about the tar function but an example,You should always declare the size of a char string to be 1 more than the maximum expected length to allow for the null character.