From ef6723af9e7649d32b540aa39737fb27d1072ab6 Mon Sep 17 00:00:00 2001 From: guoshichao Date: Tue, 26 Nov 2024 10:43:46 +0800 Subject: [PATCH] nuttx: add get_current_dir_name implementation Signed-off-by: guoshichao --- include/unistd.h | 1 + libs/libc/libc.csv | 1 + libs/libc/unistd/lib_getcwd.c | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/include/unistd.h b/include/unistd.h index 7557655fd7..a0a04253dc 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -389,6 +389,7 @@ unsigned int alarm(unsigned int seconds); int chdir(FAR const char *path); int fchdir(int fd); FAR char *getcwd(FAR char *buf, size_t size); +FAR char *get_current_dir_name(void); /* File path operations */ diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv index 699726c13d..d9cf6d0655 100644 --- a/libs/libc/libc.csv +++ b/libs/libc/libc.csv @@ -97,6 +97,7 @@ "getaddrinfo","netdb.h","defined(CONFIG_LIBC_NETDB)","int","FAR const char *","FAR const char *","FAR const struct addrinfo *","FAR struct addrinfo **" "getc","stdio.h","","int","FAR FILE *" "getcwd","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char *","FAR char *","size_t" +"get_current_dir_name","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char *","void" "getegid","unistd.h","","gid_t" "geteuid","unistd.h","","uid_t" "gethostbyname","netdb.h","defined(CONFIG_LIBC_NETDB)","FAR struct hostent *","FAR const char *" diff --git a/libs/libc/unistd/lib_getcwd.c b/libs/libc/unistd/lib_getcwd.c index 41091fd1a1..8c6ce20651 100644 --- a/libs/libc/unistd/lib_getcwd.c +++ b/libs/libc/unistd/lib_getcwd.c @@ -127,3 +127,28 @@ FAR char *getcwd(FAR char *buf, size_t size) strlcpy(buf, pwd, size); return buf; } + +/**************************************************************************** + * Name: get_current_dir_name + * + * Description: + * get_current_dir_name() will allocate an buffer to hold the current + * working directory info, and this buffer will be returned to user. + * the user will be responsible to free the buffer. + * + * Input Parameters: + * None + * + * Returned Value: + * On success, get_current_dir_name() returns a pointer to a string + * containing the pathname of the current working directory. + * Otherwise, get_current_dir_name() returns a null pointer and + * sets errno to indicate the error. + * the errno can refer to: getcwd() function's comments. + * + ****************************************************************************/ + +FAR char *get_current_dir_name(void) +{ + return getcwd(NULL, 0); +}