Added dirname() function
This commit is contained in:
parent
1e68c71d72
commit
efd9d3d312
3 changed files with 44 additions and 0 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
18
test/t_path2.c
Normal file
18
test/t_path2.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
Reference in a new issue