You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/10/19 13:20:44 UTC

[incubator-nuttx] 03/03: Implement fdopendir

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit f100a4bb921c609998507aa6e9354b899758b84f
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Wed Oct 19 19:16:08 2022 +0900

    Implement fdopendir
---
 include/dirent.h                 |  1 +
 libs/libc/dirent/Make.defs       |  3 +-
 libs/libc/dirent/lib_fdopendir.c | 87 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/include/dirent.h b/include/dirent.h
index 54ea3aeb7c..1ac8cf2b58 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -141,6 +141,7 @@ extern "C"
 
 int        closedir(FAR DIR *dirp);
 FAR DIR   *opendir(FAR const char *path);
+FAR DIR   *fdopendir(int fd);
 FAR struct dirent *readdir(FAR DIR *dirp);
 int        readdir_r(FAR DIR *dirp, FAR struct dirent *entry,
                      FAR struct dirent **result);
diff --git a/libs/libc/dirent/Make.defs b/libs/libc/dirent/Make.defs
index 440a3d0477..5814ab8071 100644
--- a/libs/libc/dirent/Make.defs
+++ b/libs/libc/dirent/Make.defs
@@ -21,7 +21,8 @@
 # Add the dirent C files to the build
 
 CSRCS += lib_readdirr.c lib_telldir.c lib_alphasort.c lib_scandir.c
-CSRCS += lib_ftw.c lib_nftw.c lib_opendir.c lib_closedir.c lib_readdir.c
+CSRCS += lib_ftw.c lib_nftw.c
+CSRCS += lib_opendir.c lib_fdopendir.c lib_closedir.c lib_readdir.c
 CSRCS += lib_rewinddir.c lib_seekdir.c lib_dirfd.c
 
 # Add the dirent directory to the build
diff --git a/libs/libc/dirent/lib_fdopendir.c b/libs/libc/dirent/lib_fdopendir.c
new file mode 100644
index 0000000000..80107ddefd
--- /dev/null
+++ b/libs/libc/dirent/lib_fdopendir.c
@@ -0,0 +1,87 @@
+/****************************************************************************
+ * libs/libc/dirent/lib_fdopendir.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/stat.h>
+
+#include <dirent.h>
+#include <errno.h>
+
+#include "libc.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdopendir
+ *
+ * Description:
+ *   An equivalent of opendir() except that this takes an open descriptor
+ *   instead of a pathname.
+ *
+ * Input Parameters:
+ *   fd -- the directory to open
+ *
+ * Returned Value:
+ *   The fdopendir() function returns a pointer to the directory stream.
+ *   On error, NULL is returned, and errno is set appropriately.
+ *
+ *   ENOTDIR - 'fd' is not a directory.
+ *
+ *   See opendir() for other errors.
+ *
+ ****************************************************************************/
+
+FAR DIR *fdopendir(int fd)
+{
+  struct stat st;
+  FAR DIR *dir;
+  int ret;
+
+  ret = fstat(fd, &st);
+  if (ret == -1)
+    {
+      return NULL;
+    }
+
+  if (!S_ISDIR(st.st_mode))
+    {
+      set_errno(ENOTDIR);
+      return NULL;
+    }
+
+  dir = lib_malloc(sizeof(*dir));
+  if (dir == NULL)
+    {
+      set_errno(ENOMEM);
+      return NULL;
+    }
+
+  dir->fd = fd;
+  return dir;
+}