From fca8bba289665715495b5962a5227efb2315b734 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 20 Nov 2010 08:06:05 +0100 Subject: [PATCH] path.c: Added is_dir function --- src/path.c | 11 +++++++++++ src/path.h | 2 ++ test/t_path.c | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/src/path.c b/src/path.c index 5c7f3b8..fd43bd1 100644 --- a/src/path.c +++ b/src/path.c @@ -14,6 +14,8 @@ #include #include +#include +#include #include "debug.h" #include "strbuf.h" @@ -56,6 +58,15 @@ int is_abspath(const char *path) { return 1; } +int is_dir(const char *path) { + + struct stat st; + + if (path && stat(path, &st) >= 0) + return S_ISDIR(st.st_mode); + return 0; +} + static char* expand_home() { char *home = getenv("HOME"); diff --git a/src/path.h b/src/path.h index 483e86b..5874e35 100644 --- a/src/path.h +++ b/src/path.h @@ -16,6 +16,8 @@ int is_abspath(const char *path); +int is_dir(const char *path); + char* path_normalize(const char *base, const char *name, unsigned char dir); #endif /* __PATH_H */ diff --git a/test/t_path.c b/test/t_path.c index f9c5186..456713b 100644 --- a/test/t_path.c +++ b/test/t_path.c @@ -51,6 +51,13 @@ void test_isabspath() { assert(is_abspath("/ab/..xy/file") ==1); } +void test_isdir() { + + assert(is_dir("t_path.c") == 0); + assert(is_dir("../src") == 1); + assert(is_dir("/") == 1); +} + void test_basename() { int i; @@ -98,6 +105,7 @@ void test_dirname() { int main(int argc, char *argv[]) { test_isabspath(); + test_isdir(); test_normalize(); test_basename(); test_dirname();