Reduce libc.so's memory footprint by lazily allocating memory used internally
by basename() and dirname(). Reviewed by: eric
This commit is contained in:
parent
2226ce021a
commit
5fb691beab
@ -42,9 +42,15 @@ char *
|
||||
basename(path)
|
||||
const char *path;
|
||||
{
|
||||
static char bname[MAXPATHLEN];
|
||||
static char *bname = NULL;
|
||||
const char *endp, *startp;
|
||||
|
||||
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, ".");
|
||||
@ -67,7 +73,7 @@ basename(path)
|
||||
while (startp > path && *(startp - 1) != '/')
|
||||
startp--;
|
||||
|
||||
if (endp - startp + 2 > sizeof(bname)) {
|
||||
if (endp - startp + 2 > MAXPATHLEN) {
|
||||
errno = ENAMETOOLONG;
|
||||
return(NULL);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user