mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 13:51:31 +00:00
Avoid unneeded memory allocation in xdirname()
This commit is contained in:
parent
e27af6f1db
commit
fa6b45a96d
12
noice.c
12
noice.c
|
@ -158,21 +158,17 @@ xstrdup(const char *s)
|
||||||
char *
|
char *
|
||||||
xdirname(const char *path)
|
xdirname(const char *path)
|
||||||
{
|
{
|
||||||
char *p, *tmp;
|
char tmp[PATH_MAX], *p;
|
||||||
|
|
||||||
/* Some implementations of dirname(3) may modify `path' and some
|
/* Some implementations of dirname(3) may modify `path' and some
|
||||||
* return a pointer inside `path' and we cannot free(3) the
|
* return a pointer inside `path' and we cannot free(3) the
|
||||||
* original string if we lose track of it. */
|
* original string if we lose track of it. */
|
||||||
tmp = xstrdup(path);
|
strlcpy(tmp, path, sizeof(tmp));
|
||||||
p = dirname(tmp);
|
p = dirname(tmp);
|
||||||
if (p == NULL) {
|
if (p == NULL)
|
||||||
free(tmp);
|
|
||||||
printerr(1, "dirname");
|
printerr(1, "dirname");
|
||||||
}
|
|
||||||
/* Make sure this is a malloc(3)-ed string */
|
/* Make sure this is a malloc(3)-ed string */
|
||||||
p = xstrdup(p);
|
return xstrdup(p);
|
||||||
free(tmp);
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Reference in a new issue