diff --git a/src/common/path.c b/src/common/path.c index 2d8055e..7eeb3d0 100644 --- a/src/common/path.c +++ b/src/common/path.c @@ -167,3 +167,26 @@ const char* basename(const char *path) { return last; } + + +char* dirname(char *fullpath) { + + char *dirname, *sep; + int pos; + + if (fullpath[strlen(fullpath)-1] == '/') + fullpath[strlen(fullpath)-1] = '\0'; + + sep = strrchr(fullpath,'/'); + pos = sep-fullpath+1; + + if(sep == NULL) + return NULL; + + dirname = malloc ( pos ); + + memset(dirname,0,pos); + memcpy (dirname, fullpath, pos-1 ); + + return dirname; +} diff --git a/test/Makefile b/test/Makefile index 4f24065..7a75497 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,6 +14,9 @@ raw_inotify : path : $(CC) -D__DEBUG__ $(CFLAGS) ../src/common/path.c t_path.c -o test_path +path2 : + $(CC) -D__DEBUG__ $(CFLAGS) ../src/common/path.c t_path2.c -o test_path2 + rbtree : $(CC) -D RB_DEBUG $(CFLAGS) ../src/common/rbtree.c t_rbtree.c -o test_rbtree diff --git a/test/t_path2.c b/test/t_path2.c new file mode 100644 index 0000000..610ca89 --- /dev/null +++ b/test/t_path2.c @@ -0,0 +1,18 @@ + +#include +#include +#include "../src/common/path.h" + + +int main(int argc, char *argv[]) { + + char fp[] = "/this/is/my/path/to/file.pdf/"; + char *d; + printf("%s\n=>\n", fp); + + d = dirname(fp); + + printf("%s\n", d); + + return 0; +}