Skip to content

Commit

Permalink
libc: Make getcwd() work even CONFIG_DISABLE_ENVIRON is enabled
Browse files Browse the repository at this point in the history
since getcwd() can be implemented correctly without using environ

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
  • Loading branch information
xiaoxiang781216 committed Oct 16, 2024
1 parent 499de61 commit 017266e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion libs/libc/stdlib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ config LIBC_RAND_ORDER
config LIBC_HOMEDIR
string "Home directory"
default "/"
depends on !DISABLE_ENVIRON
---help---
The home directory to use with operations like such as 'cd ~'

Expand Down
3 changes: 2 additions & 1 deletion libs/libc/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(SRCS
lib_swab.c
lib_pathconf.c
lib_sysconf.c
lib_getcwd.c
lib_getentropy.c
lib_getopt_common.c
lib_getopt.c
Expand Down Expand Up @@ -82,7 +83,7 @@ if(NOT CONFIG_SCHED_USER_IDENTITY)
endif()

if(NOT CONFIG_DISABLE_ENVIRON)
list(APPEND SRCS lib_chdir.c lib_fchdir.c lib_getcwd.c lib_restoredir.c)
list(APPEND SRCS lib_chdir.c lib_fchdir.c lib_restoredir.c)
endif()

if(CONFIG_LIBC_EXECFUNCS)
Expand Down
6 changes: 3 additions & 3 deletions libs/libc/unistd/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
# Add the unistd C files to the build

CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_pathconf.c lib_sysconf.c
CSRCS += lib_getentropy.c lib_getopt_common.c lib_getopt.c lib_getopt_long.c
CSRCS += lib_getopt_longonly.c lib_getoptvars.c lib_getoptargp.c
CSRCS += lib_getcwd.c lib_getentropy.c lib_getopt_common.c lib_getopt.c
CSRCS += lib_getopt_long.c lib_getopt_longonly.c lib_getoptvars.c lib_getoptargp.c
CSRCS += lib_getopterrp.c lib_getoptindp.c lib_getoptoptp.c lib_times.c
CSRCS += lib_alarm.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c lib_nice.c
CSRCS += lib_setreuid.c lib_setregid.c lib_getrusage.c lib_utime.c lib_utimes.c
Expand All @@ -40,7 +40,7 @@ CSRCS += lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c
endif

ifneq ($(CONFIG_DISABLE_ENVIRON),y)
CSRCS += lib_chdir.c lib_fchdir.c lib_getcwd.c lib_restoredir.c
CSRCS += lib_chdir.c lib_fchdir.c lib_restoredir.c
endif

ifeq ($(CONFIG_LIBC_EXECFUNCS),y)
Expand Down
6 changes: 3 additions & 3 deletions libs/libc/unistd/lib_getcwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

#include "libc.h"

#ifndef CONFIG_DISABLE_ENVIRON

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -95,10 +93,13 @@ FAR char *getcwd(FAR char *buf, size_t size)
size = PATH_MAX + 1;
}

#ifndef CONFIG_DISABLE_ENVIRON

/* If no working directory is defined, then default to the home directory */

pwd = getenv("PWD");
if (pwd == NULL)
#endif /* !CONFIG_DISABLE_ENVIRON */
{
pwd = CONFIG_LIBC_HOMEDIR;
}
Expand Down Expand Up @@ -126,4 +127,3 @@ FAR char *getcwd(FAR char *buf, size_t size)
strlcpy(buf, pwd, size);
return buf;
}
#endif /* !CONFIG_DISABLE_ENVIRON */

0 comments on commit 017266e

Please sign in to comment.