Good day everyone.
I just want to know if I'm doing everything right in 2 of my functions. First off, the function inserttolist just connects the node created by parse_file. The linked list contains parts of a file, which I send to another PC. I didn't put this into the networking section because I think that the networking part of my code is already working.
There are two problems that I encounter when using these functions. First, when I try to allocate a small memory size for my chunksize(say 100bytes), a segmentation fault usually happens.
Another problem that I encounter is when I try to traverse the list, a segmentation fault SOMETIMES happen. If for example, I traversed it in one go(meaning, traverse the first node until the last), it runs fine. But when I traverse it when I need to resend a part of the file which is lost(since this is used in a file transfer program), it sometimes encounter a segmentation fault.
For the linked list:
Code:
struct flist{ //linked list for file chunk
char *chunk; //file segment
int chunksize; //size of chunk of this node
int isn; //segment number of packet
int res;
int last;
int acked;
struct flist *next; //pointer to the next node
};
typedef struct flist Flist;
Flist *fileparse = NULL; //define pointer of struct flist
For the parse_file:
Code:
void parse_file(Flist* sPtr, int chunksize, int isn, char filename[]){
FILE *ptr; //file pointer
char *buffer1, *buffer2; //pointer to memory holding each chunk
int quotient; //counter for f
int modulo;
size_t res; //for fread
int counter = 0; //just a counter
/*open file*/
if ((ptr = fopen(filename, "r+b")) == NULL) //if file cannot be opened..
{
printf("File open error\n");
goto end;
}
else{
/*obtain file size*/
fseek (ptr , 0 , SEEK_END);
filesize = ftell (ptr);
rewind (ptr);
}
/*obtain number of chunks to be made*/
if(filesize<chunksize){
printf("File size is too small for chunk\n");
quotient = 0;
modulo = filesize;
goto start;
}
quotient = filesize/chunksize;
modulo = filesize%chunksize;
printf("Modulo is %i\n",modulo);
start:
buffer1 = (char *)malloc(sizeof(char)*chunksize); //allocate memory for chunk
buffer2 = (char *)malloc(sizeof(char)*modulo); //allocate memory for chunk
/*start getting the chunks*/
while(1){
if(counter!=quotient){
memset(buffer1,0,chunksize);
if(buffer1 == NULL){
printf("Allocation failed\n");
goto end;
}
res = fread(buffer1,sizeof(char),chunksize,ptr); //get the chunk from file pointer
if (res != chunksize){
goto end;
}
inserttolist(sPtr,res,isn,buffer1,0,res,modulo);
counter++; //increment counter
isn = isn + res; //increment segment number
}
else if((counter == quotient) && (modulo != 0)){
memset(buffer2,0,modulo);
if(buffer2 == NULL){
printf("Allocation failed\n");
goto end;
}
res = fread (buffer2,sizeof(char),modulo,ptr); //get the chunk from file pointer
inserttolist(sPtr,res,isn,buffer2,1, res,modulo);
break;
}
}
end:
fclose(ptr);
}
For the inserttolist:
Code:
void inserttolist(Flist *sPtr, int chunksize, int isn, char *buffer, int last, int res, int modulo){
while(sPtr->next != NULL){
sPtr = sPtr->next; //find end of list
}
sPtr->next = (struct flist *)malloc(sizeof(struct flist)); //allocate memory for node
sPtr->next-> res = res;
sPtr->next->chunksize = chunksize; //size of the chunk pointed by sptr->chunk
sPtr->next->isn = isn; //segment number of this chunk
sPtr->next->next = NULL;
sPtr->next->acked = 0;
if(last == 0){
sPtr->next->last = 0;
sPtr->next->chunk = (char *)malloc(sizeof(char)*chunksize); //set sptr->chunk to point to the actual chunk
memcpy(sPtr->next->chunk,buffer,chunksize);
}
else{
sPtr->next->last = 1;
sPtr->next->chunk = (char *)malloc(sizeof(char)*modulo); //set sptr->chunk to point to the actual chunk
memcpy(sPtr->next->chunk,buffer,modulo);
}
}
Thank you for taking the time in reading this and I hope you can help