The code:
#include <string.h>
#include <libgen.h>
#include <stdio.h>
int main()
{
char *token = strtok(basename("this/is/a/path.geo.bin"), ".");
if (token != NULL){
printf( " %s\n", token );
}
return(0);
}
If I extract the filepath and put it into a char array it works perfectly. However, like this I'm getting a segfault.
It seems that function basename simply returns pointer within string literal "this/is/a/path.geo.bin" or even tries to change it.
However string literals may not be changed. Any attempt to change a string literal results in undefined behaviour. And function
strtok
changes the string passed to it as an argument.According to the C Standard (6.4.5 String literals)
Thus the program could look like