Archived
1
0
Fork 0

path.c: added dirname_s function.

This commit is contained in:
Henrik Hautakoski 2011-02-11 19:17:38 +01:00
parent 79f18e6671
commit 546e7c50f1
4 changed files with 41 additions and 5 deletions

View file

@ -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.
`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.
--

View file

@ -1,6 +1,6 @@
/* 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
* 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 :)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
@ -67,6 +68,29 @@ int is_dir(const char *path) {
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, ...) {
static strbuf_t sb = STRBUF_INIT;

View file

@ -1,6 +1,6 @@
/* 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
* 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);
const char* dirname_s(const char *path, int slash);
const char* mkpath(const char *fmt, ...);
char* path_normalize(const char *base, const char *name, unsigned dir);

View file

@ -97,22 +97,24 @@ void test_dirname() {
int i;
char data[11][2][64] = {
char data[13][2][64] = {
{ "", "." },
{ "/", "/" },
{ "///", "/" },
{ ".", "."},
{ "..", "." },
{ "../", "." },
{ "../../rel", "../.." },
{ "./rel", "." },
{ "x", "." },
{ "justsomestring", "." },
{ "/usr/src/", "/usr" },
{ "/usr/src///", "/usr" },
{ "/usr/src/linux-2.6.30-r5/drivers", "/usr/src/linux-2.6.30-r5" }
};
for(i=0; i < 11; i++)
assert_string(dirname(data[i][0]), data[i][1]);
for(i=0; i < 12; i++)
assert_string(dirname_s(data[i][0], 0), data[i][1]);
}
int main(int argc, char *argv[]) {