* path.cc (basename): Return pointer into the path argument itself.

Shrink buf to 4 bytes.  Use buf only for border cases.
	(dirname): Ditto.
This commit is contained in:
Corinna Vinschen 2007-10-11 16:26:19 +00:00
parent 044b62c767
commit 2da0e37519
2 changed files with 27 additions and 15 deletions

View File

@ -1,3 +1,9 @@
2007-10-11 Corinna Vinschen <corinna@vinschen.de>
* path.cc (basename): Return pointer into the path argument itself.
Shrink buf to 4 bytes. Use buf only for border cases.
(dirname): Ditto.
2007-10-10 Corinna Vinschen <corinna@vinschen.de>
* path.cc (struct symlink_info): Change size of contents member to

View File

@ -4694,15 +4694,14 @@ etc::file_changed (int n)
extern "C" char *
basename (char *path)
{
static char buf[CYG_MAX_PATH];
char *c, *d, *bs = buf;
static char buf[4];
char *c, *d, *bs = path;
if (!path || !*path)
return strcpy (buf, ".");
strncpy (buf, path, CYG_MAX_PATH);
if (isalpha (buf[0]) && buf[1] == ':')
if (isalpha (path[0]) && path[1] == ':')
bs += 2;
else if (strspn (buf, "/\\") > 1)
else if (strspn (path, "/\\") > 1)
++bs;
c = strrchr (bs, '/');
if ((d = strrchr (c ?: bs, '\\')) > c)
@ -4720,9 +4719,13 @@ basename (char *path)
if (c && (c > bs || c[1]))
return c + 1;
}
else if (!bs[0])
strcpy (bs, ".");
return buf;
else if (!bs[0])
{
stpncpy (buf, path, bs - path);
stpcpy (buf + (bs - path), ".");
return buf;
}
return path;
}
/* No need to be reentrant or thread-safe according to SUSv3.
@ -4732,15 +4735,14 @@ basename (char *path)
extern "C" char *
dirname (char *path)
{
static char buf[CYG_MAX_PATH];
char *c, *d, *bs = buf;
static char buf[4];
char *c, *d, *bs = path;
if (!path || !*path)
return strcpy (buf, ".");
strncpy (buf, path, CYG_MAX_PATH);
if (isalpha (buf[0]) && buf[1] == ':')
if (isalpha (path[0]) && path[1] == ':')
bs += 2;
else if (strspn (buf, "/\\") > 1)
else if (strspn (path, "/\\") > 1)
++bs;
c = strrchr (bs, '/');
if ((d = strrchr (c ?: bs, '\\')) > c)
@ -4767,6 +4769,10 @@ dirname (char *path)
c[1] = '\0';
}
else
strcpy (bs, ".");
return buf;
{
stpncpy (buf, path, bs - path);
stpcpy (buf + (bs - path), ".");
return buf;
}
return path;
}