From f4794f0b484ea7f0f680fafa2cdb649e11065a43 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Thu, 10 Sep 2020 12:10:52 +0800 Subject: [PATCH] libc: Implement access function correctly Signed-off-by: Xiang Xiao Change-Id: I6ae3abf79bd9aa8cfb54b8bbe302d69c4d9cb8ff --- libs/libc/unistd/lib_access.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/libs/libc/unistd/lib_access.c b/libs/libc/unistd/lib_access.c index e06f29befd8..42b6bd27cd1 100644 --- a/libs/libc/unistd/lib_access.c +++ b/libs/libc/unistd/lib_access.c @@ -39,6 +39,7 @@ #include +#include #include /**************************************************************************** @@ -71,8 +72,8 @@ * Description: * The access() function shall check the file named by the pathname pointed * to by the path argument for accessibility according to the bit pattern - * contained in amode, using the real user ID in place of the effective user - * ID and the real group ID in place of the effective group ID. + * contained in amode, using the real user ID in place of the effective + * user ID and the real group ID in place of the effective group ID. * As there are no users in NuttX, the function always succeeds. * * Input Parameters: @@ -80,7 +81,8 @@ * amode - the access mode * * Returned Value: - * Always OK (zero) + * Return OK (zero) if the caller can access the file with the required + * permission, otherwise return -1. * * Assumptions: * @@ -88,5 +90,27 @@ int access(FAR const char *path, int amode) { + struct stat s; + + if (stat(path, &s)) + { + return -1; + } + + if (s.st_mode & S_IFDIR) + { + return 0; + } + + if (amode & W_OK) + { + if (s.st_mode & S_IWUSR) + { + return 0; + } + + return -1; + } + return 0; }