* 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> 2007-10-10 Corinna Vinschen <corinna@vinschen.de>
* path.cc (struct symlink_info): Change size of contents member to * 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 * extern "C" char *
basename (char *path) basename (char *path)
{ {
static char buf[CYG_MAX_PATH]; static char buf[4];
char *c, *d, *bs = buf; char *c, *d, *bs = path;
if (!path || !*path) if (!path || !*path)
return strcpy (buf, "."); return strcpy (buf, ".");
strncpy (buf, path, CYG_MAX_PATH); if (isalpha (path[0]) && path[1] == ':')
if (isalpha (buf[0]) && buf[1] == ':')
bs += 2; bs += 2;
else if (strspn (buf, "/\\") > 1) else if (strspn (path, "/\\") > 1)
++bs; ++bs;
c = strrchr (bs, '/'); c = strrchr (bs, '/');
if ((d = strrchr (c ?: bs, '\\')) > c) if ((d = strrchr (c ?: bs, '\\')) > c)
@ -4721,8 +4720,12 @@ basename (char *path)
return c + 1; return c + 1;
} }
else if (!bs[0]) else if (!bs[0])
strcpy (bs, "."); {
stpncpy (buf, path, bs - path);
stpcpy (buf + (bs - path), ".");
return buf; return buf;
}
return path;
} }
/* No need to be reentrant or thread-safe according to SUSv3. /* No need to be reentrant or thread-safe according to SUSv3.
@ -4732,15 +4735,14 @@ basename (char *path)
extern "C" char * extern "C" char *
dirname (char *path) dirname (char *path)
{ {
static char buf[CYG_MAX_PATH]; static char buf[4];
char *c, *d, *bs = buf; char *c, *d, *bs = path;
if (!path || !*path) if (!path || !*path)
return strcpy (buf, "."); return strcpy (buf, ".");
strncpy (buf, path, CYG_MAX_PATH); if (isalpha (path[0]) && path[1] == ':')
if (isalpha (buf[0]) && buf[1] == ':')
bs += 2; bs += 2;
else if (strspn (buf, "/\\") > 1) else if (strspn (path, "/\\") > 1)
++bs; ++bs;
c = strrchr (bs, '/'); c = strrchr (bs, '/');
if ((d = strrchr (c ?: bs, '\\')) > c) if ((d = strrchr (c ?: bs, '\\')) > c)
@ -4767,6 +4769,10 @@ dirname (char *path)
c[1] = '\0'; c[1] = '\0';
} }
else else
strcpy (bs, "."); {
stpncpy (buf, path, bs - path);
stpcpy (buf + (bs - path), ".");
return buf; return buf;
}
return path;
} }