path.c: added dirname_s function.
This commit is contained in:
parent
79f18e6671
commit
546e7c50f1
4 changed files with 41 additions and 5 deletions
|
|
@ -38,3 +38,11 @@ The function performs this type of checks/manipulation:
|
||||||
+
|
+
|
||||||
Returns `NULL` if 'base' is not an absolute path or 'name' contains a delimiter.
|
Returns `NULL` if 'base' is not an absolute path or 'name' contains a delimiter.
|
||||||
|
|
||||||
|
`dirname_s`::
|
||||||
|
|
||||||
|
Safe dirname implementation. works like POSIX dirname with some modifications: +
|
||||||
|
+
|
||||||
|
--
|
||||||
|
* 'slash' may be used to terminate the string with a \'/' character. +
|
||||||
|
* The function will never modify 'path' instead the function will *always* return a static buffer that will be overwritten by subsequent calls.
|
||||||
|
--
|
||||||
|
|
|
||||||
26
src/path.c
26
src/path.c
|
|
@ -1,6 +1,6 @@
|
||||||
/* path.c - path handling routines
|
/* path.c - path handling routines
|
||||||
*
|
*
|
||||||
* Copyright (C) 2010 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
* Copyright (C) 2010-2011 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
* so we use funky heap memory based algorithms instead :)
|
* so we use funky heap memory based algorithms instead :)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
@ -67,6 +68,29 @@ int is_dir(const char *path) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
strbuf_squeeze(&sb, '/');
|
||||||
|
|
||||||
|
if (sb.len > 1) {
|
||||||
|
if (sb.buf[sb.len-1] == '/')
|
||||||
|
strbuf_reduce(&sb, 1);
|
||||||
|
|
||||||
|
if (!strbuf_rchop(&sb, '/'))
|
||||||
|
strbuf_reduce(&sb, sb.len-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sb.buf[0] != '/')
|
||||||
|
sb.buf[0] = '.';
|
||||||
|
if (slash)
|
||||||
|
strbuf_term(&sb, '/');
|
||||||
|
return sb.buf;
|
||||||
|
}
|
||||||
|
|
||||||
const char *mkpath(const char *fmt, ...) {
|
const char *mkpath(const char *fmt, ...) {
|
||||||
|
|
||||||
static strbuf_t sb = STRBUF_INIT;
|
static strbuf_t sb = STRBUF_INIT;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/* path.h - path handling routines
|
/* path.h - path handling routines
|
||||||
*
|
*
|
||||||
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
|
* Copyright (C) 2010-2011 Henrik Hautakoski <henrik@fiktivkod.org>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
@ -20,6 +20,8 @@ int is_file(const char *path);
|
||||||
|
|
||||||
int is_dir(const char *path);
|
int is_dir(const char *path);
|
||||||
|
|
||||||
|
const char* dirname_s(const char *path, int slash);
|
||||||
|
|
||||||
const char* mkpath(const char *fmt, ...);
|
const char* mkpath(const char *fmt, ...);
|
||||||
|
|
||||||
char* path_normalize(const char *base, const char *name, unsigned dir);
|
char* path_normalize(const char *base, const char *name, unsigned dir);
|
||||||
|
|
|
||||||
|
|
@ -97,22 +97,24 @@ void test_dirname() {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
char data[11][2][64] = {
|
char data[13][2][64] = {
|
||||||
{ "", "." },
|
{ "", "." },
|
||||||
{ "/", "/" },
|
{ "/", "/" },
|
||||||
{ "///", "/" },
|
{ "///", "/" },
|
||||||
{ ".", "."},
|
{ ".", "."},
|
||||||
{ "..", "." },
|
{ "..", "." },
|
||||||
|
{ "../", "." },
|
||||||
{ "../../rel", "../.." },
|
{ "../../rel", "../.." },
|
||||||
{ "./rel", "." },
|
{ "./rel", "." },
|
||||||
|
{ "x", "." },
|
||||||
{ "justsomestring", "." },
|
{ "justsomestring", "." },
|
||||||
{ "/usr/src/", "/usr" },
|
{ "/usr/src/", "/usr" },
|
||||||
{ "/usr/src///", "/usr" },
|
{ "/usr/src///", "/usr" },
|
||||||
{ "/usr/src/linux-2.6.30-r5/drivers", "/usr/src/linux-2.6.30-r5" }
|
{ "/usr/src/linux-2.6.30-r5/drivers", "/usr/src/linux-2.6.30-r5" }
|
||||||
};
|
};
|
||||||
|
|
||||||
for(i=0; i < 11; i++)
|
for(i=0; i < 12; i++)
|
||||||
assert_string(dirname(data[i][0]), data[i][1]);
|
assert_string(dirname_s(data[i][0], 0), data[i][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
|
||||||
Reference in a new issue