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

@ -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);