Reputation: 6515
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.
Upvotes: 1
Views: 224
Reputation: 32512
From the documentation of basename()
:
char *basename(char *path);
Both
dirname()
andbasename()
may modify the contents ofpath
, so it may be desirable to pass a copy when calling one of these functions.These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. Alternatively, they may return a pointer to some part of path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required.
In addition strtok()
is known to modify the string passed to it:
Be cautious when using these functions. If you do use them, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
This is likely the source of your problem as string literals must not be modified:
If the program attempts to modify such an array, the behavior is undefined.
You should work around that.
For reference:
Upvotes: 2
Reputation: 310980
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)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
Thus the program could look like
int main()
{
char name[] = "this/is/a/path.geo.bin";
char *token = strtok(basename( name ), ".");
if (token != NULL){
printf( " %s\n", token );
}
return(0);
}
Upvotes: 2