Skip to content

Commit

Permalink
Add path_dirname()
Browse files Browse the repository at this point in the history
  • Loading branch information
tihirvon committed Apr 26, 2014
1 parent 5143fb7 commit e0e8b34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ char *filename_to_utf8(const char *filename)
return str;
}

char *path_dirname(const char *filename)
{
char *slash = strrchr(filename, '/');
if (slash == NULL) {
return xstrdup(".");
}
if (slash == filename) {
return xstrdup("/");
}
return xstrcut(filename, slash - filename);
}

// filename must not contain trailing slashes (but it can be "/")
const char *path_basename(const char *filename)
{
Expand Down
1 change: 1 addition & 0 deletions path.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ char *relative_filename(const char *f, const char *cwd);
char *short_filename_cwd(const char *absolute, const char *cwd);
char *short_filename(const char *absolute);
char *filename_to_utf8(const char *filename);
char *path_dirname(const char *filename);
const char *path_basename(const char *filename);

#endif

0 comments on commit e0e8b34

Please sign in to comment.