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 filepath
basename
returns the last portion of a filepath
Using 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.