From 0fe21742f61cac9256daf9774d1eb815ede058e9 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Mon, 13 Dec 2010 05:20:56 +0000 Subject: [PATCH] add dirname() function SVN revision: 55530 --- legacy/evil/ChangeLog | 6 +++ legacy/evil/src/lib/evil_libgen.c | 83 ++++++++++++++++++++++++++++--- legacy/evil/src/lib/evil_libgen.h | 4 ++ 3 files changed, 86 insertions(+), 7 deletions(-) diff --git a/legacy/evil/ChangeLog b/legacy/evil/ChangeLog index a4176f80bf..483fda5f77 100644 --- a/legacy/evil/ChangeLog +++ b/legacy/evil/ChangeLog @@ -1,3 +1,9 @@ +2010-12-12 Vincent Torri + + * src/lib/evil_libgen.c: + * src/lib/evil_libgen.h: + add dirname() function + 2010-12-12 Vincent Torri * src/lib/evil_unistd.c: diff --git a/legacy/evil/src/lib/evil_libgen.c b/legacy/evil/src/lib/evil_libgen.c index dd43d46e61..089bdb5f8f 100644 --- a/legacy/evil/src/lib/evil_libgen.c +++ b/legacy/evil/src/lib/evil_libgen.c @@ -7,6 +7,7 @@ #include "Evil.h" char _evil_basename_buf[PATH_MAX]; +char _evil_dirname_buf[PATH_MAX]; char * evil_basename(char *path) @@ -15,28 +16,96 @@ evil_basename(char *path) char *p2; size_t length; - if (!path || !*path) + /* path must begin by "?:\" or "?:/" */ + if ((!path) || + (strlen(path) <= 3) || + ((path[0] < 'a' || path[0] > 'z') && (path[0] < 'A' || path[0] > 'Z')) || + (path[1] != ':') || + ((path[2] != '/') && (path[2] != '\\'))) { - memcpy(_evil_basename_buf, ".", 2); + memcpy(_evil_basename_buf, "C:\\", 4); return _evil_basename_buf; } - + + /* '/' --> '\\' */ length = strlen(path); p1 = strdup(path); - if (!p1) return NULL; + if (!p1) + { + memcpy(_evil_basename_buf, "C:\\", 4); + return _evil_basename_buf; + } p2 = p1; while (p2) { if (*p2 == '/') *p2 = '\\'; p2++; } - if (p1[length - 1] =='\\') p1[--length] = '\0'; + + /* remove trailing backslashes */ + p2 = p1 + (length - 1); + if (*p2 == '\\') + { + while (*p2 == '\\') + p2--; + } + *(p2 + 1) = '\0'; p2 = strrchr(p1, '\\'); - if (!p2) memcpy(_evil_basename_buf, p1, length + 1); - else memcpy(_evil_basename_buf, p2 + 1, (p1 + length + 1) - p2); + memcpy(_evil_basename_buf, p2 + 1, (p1 + length + 1) - p2); free(p1); return _evil_basename_buf; } + +char * +evil_dirname(char *path) +{ + char *p1; + char *p2; + size_t length; + + /* path must begin by "?:\" or "?:/" */ + if ((!path) || + (strlen(path) <= 3) || + ((path[0] < 'a' || path[0] > 'z') && (path[0] < 'A' || path[0] > 'Z')) || + (path[1] != ':') || + ((path[2] != '/') && (path[2] != '\\'))) + { + memcpy(_evil_dirname_buf, "C:\\", 4); + return _evil_dirname_buf; + } + + /* '/' --> '\\' */ + length = strlen(path); + p1 = strdup(path); + if (!p1) + { + memcpy(_evil_dirname_buf, "C:\\", 4); + return _evil_dirname_buf; + } + p2 = p1; + while (p2) + { + if (*p2 == '/') *p2 = '\\'; + p2++; + } + + /* remove trailing backslashes */ + p2 = p1 + (length - 1); + if (*p2 == '\\') + { + while (*p2 == '\\') + p2--; + } + *(p2 + 1) = '\0'; + + p2 = strrchr(p1, '\\'); + *p2 = '\0'; + memcpy(_evil_dirname_buf, p1, strlen(p1) + 1); + + free(p1); + + return _evil_dirname_buf; +} diff --git a/legacy/evil/src/lib/evil_libgen.h b/legacy/evil/src/lib/evil_libgen.h index c252b2fa3e..48a3d82693 100644 --- a/legacy/evil/src/lib/evil_libgen.h +++ b/legacy/evil/src/lib/evil_libgen.h @@ -16,6 +16,10 @@ EAPI char *evil_basename(char *path); #define basename(p) evil_basename(p) +EAPI char *evil_dirname(char *path); + +#define dirname(p) evil_dirname(p) + /** * @}