libc/chdir: replace heap alloc to path buffer to improve performance

realloc path buffer to avoid alloc from realpath()

Signed-off-by: chao an <anchao@lixiang.com>
This commit is contained in:
chao an 2024-12-11 09:18:53 +08:00 committed by Xiang Xiao
parent bf27e4d75d
commit 2ccdd947a1

View file

@ -72,9 +72,7 @@
int chdir(FAR const char *path)
{
#ifndef CONFIG_DISABLE_ENVIRON
FAR char *oldpwd;
#endif /* !CONFIG_DISABLE_ENVIRON */
FAR char *oldpwd = NULL;
FAR char *abspath;
struct stat buf;
int ret;
@ -99,9 +97,15 @@ int chdir(FAR const char *path)
* Remove any trailing '/' characters from the path
*/
abspath = realpath(path, NULL);
if (abspath == NULL)
abspath = lib_get_pathbuffer();
if (abspath != NULL)
{
oldpwd = realpath(path, abspath);
}
if (abspath == NULL || oldpwd == NULL)
{
lib_put_pathbuffer(abspath);
return ERROR;
}
@ -124,7 +128,7 @@ int chdir(FAR const char *path)
ret = setenv("PWD", abspath, TRUE);
#endif /* !CONFIG_DISABLE_ENVIRON */
lib_free(abspath);
lib_put_pathbuffer(abspath);
return ret;
}