diff options
author | bbraun <bbraun@FreeBSD.org> | 2002-12-21 07:12:35 +0000 |
---|---|---|
committer | bbraun <bbraun@FreeBSD.org> | 2002-12-21 07:12:35 +0000 |
commit | 6e7d1bfe4c30516dd1341e91dfc0a7af500fdecc (patch) | |
tree | 9c962dee5055fb86b745dbe0a830289cfeb0419f /lib/libc/gen/dirname.c | |
parent | 4d5ffd678248c59219ce8dcca46e2b5ff680fcfc (diff) | |
download | FreeBSD-src-6e7d1bfe4c30516dd1341e91dfc0a7af500fdecc.zip FreeBSD-src-6e7d1bfe4c30516dd1341e91dfc0a7af500fdecc.tar.gz |
Reduce libc.so's memory footprint by lazily allocating memory used internally
by basename() and dirname().
Reviewed by: eric
Diffstat (limited to 'lib/libc/gen/dirname.c')
-rw-r--r-- | lib/libc/gen/dirname.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/libc/gen/dirname.c b/lib/libc/gen/dirname.c index 8e287af..4e5ecb4 100644 --- a/lib/libc/gen/dirname.c +++ b/lib/libc/gen/dirname.c @@ -42,9 +42,15 @@ char * dirname(path) const char *path; { - static char bname[MAXPATHLEN]; + static char *bname = NULL; const char *endp; + if (bname == NULL) { + bname = (char *)malloc(MAXPATHLEN); + if (bname == NULL) + return(NULL); + } + /* Empty or NULL string gets treated as "." */ if (path == NULL || *path == '\0') { (void)strcpy(bname, "."); @@ -70,7 +76,7 @@ dirname(path) } while (endp > path && *endp == '/'); } - if (endp - path + 2 > sizeof(bname)) { + if (endp - path + 2 > MAXPATHLEN) { errno = ENAMETOOLONG; return(NULL); } |