Archived
1
0
Fork 0

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.
This commit is contained in:
Henrik Hautakoski 2011-02-16 16:53:26 +02:00
parent 3c2436867e
commit a1a85267f0
2 changed files with 9 additions and 3 deletions

View file

@ -85,9 +85,11 @@ int path_isparent(const char *path, const char *parent) {
const char* dirname_s(const char *path, int slash) { const char* dirname_s(const char *path, int slash) {
static strbuf_t sb = STRBUF_INIT; static strbuf_t sb = STRBUF_INIT;
strbuf_reduce(&sb, sb.len); if (sb.buf != path) {
strbuf_append_str(&sb, path); strbuf_setlen(&sb, 0);
strbuf_append_str(&sb, path);
}
strbuf_squeeze(&sb, '/'); strbuf_squeeze(&sb, '/');
if (sb.len > 1) { if (sb.len > 1) {

View file

@ -96,6 +96,7 @@ void test_basename() {
void test_dirname() { void test_dirname() {
int i; int i;
const char *path = "/one/two/three/four/";
char data[13][2][64] = { char data[13][2][64] = {
{ "", "." }, { "", "." },
@ -115,6 +116,9 @@ void test_dirname() {
for(i=0; i < 12; i++) for(i=0; i < 12; i++)
assert_string(dirname_s(data[i][0], 0), data[i][1]); 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() { void test_path_isparent() {