I receive TFTP packets (read requests) using recvfrom with a 512 byte buffer.
The string (file name to be transfered) starts at the third byte of the buffer. The first two are reserverd for the opcode.
I wrote the following function:
char * parseFileName(char * buffer){
char * filename;
for(int i = 0; buffer[i] != '\0'; i++)
*(filename + i) = buffer[i];
*(filename + i) = '\0';
return filename;
}
and I call it considering the opcode offset:
char * filename = parseFileName(buffer + 2);
However, this doesn't work and the program stops:
printf("%s", filename);
Unless I add this:
printf("\n");
right before the return statement in the parseFileName function. Only then I can print the filename string on the calling function.
Why is that happening ?