diff options
author | Corinna Vinschen <corinna@vinschen.de> | 2007-10-11 20:26:19 +0400 |
---|---|---|
committer | Corinna Vinschen <corinna@vinschen.de> | 2007-10-11 20:26:19 +0400 |
commit | 2da0e375199d43e545eb445765c11527cbaa6ad3 (patch) | |
tree | d6771d4eeb74896f2f5d976c30f678934da1f3b9 /winsup/cygwin/path.cc | |
parent | 044b62c7676d56bcabea5956cae38771eb5b2179 (diff) |
* path.cc (basename): Return pointer into the path argument itself.
Shrink buf to 4 bytes. Use buf only for border cases.
(dirname): Ditto.
Diffstat (limited to 'winsup/cygwin/path.cc')
-rw-r--r-- | winsup/cygwin/path.cc | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index 82be4df3e..aacf0578e 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -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; } |