add dirname() function

SVN revision: 55530
This commit is contained in:
Vincent Torri 2010-12-13 05:20:56 +00:00
parent b493685f89
commit 0fe21742f6
3 changed files with 86 additions and 7 deletions

View File

@ -1,3 +1,9 @@
2010-12-12 Vincent Torri <doursse at users dot sf dot net>
* src/lib/evil_libgen.c:
* src/lib/evil_libgen.h:
add dirname() function
2010-12-12 Vincent Torri <doursse at users dot sf dot net>
* src/lib/evil_unistd.c:

View File

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

View File

@ -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)
/**
* @}