Given a path
/level1/level2/level3/level4
I want to be able to split this string such that I can retrieve each individual entry,
i.e "level1", "level2", "level3", "level4".
Now my first idea was using strtok
, but apparently most people recommend against using this function. What is another approach so that I can pass in a string (char* path)
and get each entry split at "/".
Splitting Unix paths is more than just splitting on /
. These all refer to the same path...
/foo/bar/baz/
/foo/bar/baz
/foo//bar/baz
As with many complex tasks, it's best not to do it yourself, but to use existing functions. In this case there are the POSIX dirname
and basename
functions.
dirname
returns the parent path in a filepathbasename
returns the last portion of a filepathUsing these together, you can split Unix paths.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
int main(void) {
char filepath[] = "/foo/bar//baz/";
char *fp = filepath;
while( strcmp(fp, "/") != 0 && strcmp(fp, ".") != 0 ) {
char *base = basename(fp);
puts(base);
fp = dirname(fp);
}
// Differentiate between /foo/bar and foo/bar
if( strcmp(fp, "/") == 0 ) {
puts(fp);
}
}
// baz
// bar
// foo
// /
It's not the most efficient, it does multiple passes through the string, but it is correct.