From a1a85267f0f59c2c81c692fed278c7c49e649010 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 16 Feb 2011 16:53:26 +0200 Subject: [PATCH] path.c: dirname_s: allow 'path' to point to the static buffer. feeding the function a pointer to sb.buf results in path also being cleared by the call to strbuf_reduce and the information is lost. --- src/path.c | 8 +++++--- test/t_path.c | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/path.c b/src/path.c index 622ac43..bd78ee2 100644 --- a/src/path.c +++ b/src/path.c @@ -85,9 +85,11 @@ int path_isparent(const char *path, const char *parent) { const char* dirname_s(const char *path, int slash) { static strbuf_t sb = STRBUF_INIT; - - strbuf_reduce(&sb, sb.len); - strbuf_append_str(&sb, path); + + if (sb.buf != path) { + strbuf_setlen(&sb, 0); + strbuf_append_str(&sb, path); + } strbuf_squeeze(&sb, '/'); if (sb.len > 1) { diff --git a/test/t_path.c b/test/t_path.c index e488f8b..bfb824b 100644 --- a/test/t_path.c +++ b/test/t_path.c @@ -96,6 +96,7 @@ void test_basename() { void test_dirname() { int i; + const char *path = "/one/two/three/four/"; char data[13][2][64] = { { "", "." }, @@ -115,6 +116,9 @@ void test_dirname() { for(i=0; i < 12; i++) assert_string(dirname_s(data[i][0], 0), data[i][1]); + + path = dirname_s(path, 1); + assert_string(dirname_s(path, 1), "/one/two/"); } void test_path_isparent() {