Archived
1
0
Fork 0

Added dirname() function

This commit is contained in:
Fredric N 2010-04-22 21:37:52 +02:00 committed by Henrik Hautakoski
parent 1e68c71d72
commit efd9d3d312
3 changed files with 44 additions and 0 deletions

View file

@ -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;
}