提交 2b7cf6db 编写于 作者: R Rich Felker

optimize get_current_dir_name to reduce stack bloat

our getcwd already (as an extension) supports allocation of a buffer
when the buffer argument is a null pointer, so there's no need to
duplicate the allocation logic in this wrapper function. duplicating
it is actually harmful in that it doubles the stack usage from
PATH_MAX to 2*PATH_MAX.
上级 a7dbcf5c
...@@ -7,11 +7,9 @@ ...@@ -7,11 +7,9 @@
char *get_current_dir_name(void) { char *get_current_dir_name(void) {
struct stat a, b; struct stat a, b;
char buf[PATH_MAX];
char *res = getenv("PWD"); char *res = getenv("PWD");
if (res && *res && !stat(res, &a) && !stat(".", &b) if (res && *res && !stat(res, &a) && !stat(".", &b)
&& (a.st_dev == b.st_dev) && (a.st_ino == b.st_ino)) && (a.st_dev == b.st_dev) && (a.st_ino == b.st_ino))
return strdup(res); return strdup(res);
if(!getcwd(buf, sizeof(buf))) return NULL; return getcwd(0, 0);
return strdup(buf);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册