[libc-commits] [libc] [libc] Implement scandir and its unit tests (PR #223198)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Tue Sep 29 05:25:18 PDT 2026
================
@@ -0,0 +1,136 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Templated implementation of the scandir logic for dependency injection.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_FILE_SCAN_IMPL_H
+#define LLVM_LIBC_SRC___SUPPORT_FILE_SCAN_IMPL_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/func/free.h"
+#include "hdr/func/malloc.h"
+#include "hdr/func/realloc.h"
+#include "hdr/types/struct_dirent.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/error_or.h"
+#include "src/stdlib/qsort_util.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+template <typename DirType>
+ErrorOr<int> scan_impl(const char *name, struct dirent ***namelist,
+ int (*filter)(const struct dirent *),
+ int (*compare)(const struct dirent **,
+ const struct dirent **)) {
+ auto res_open = DirType::open(name);
+ if (!res_open) {
+ return LIBC_NAMESPACE::Error(res_open.error());
+ }
----------------
labath wrote:
```suggestion
if (!res_open)
return LIBC_NAMESPACE::Error(res_open.error());
```
https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements (and elsewhere in this patch)
https://github.com/llvm/llvm-project/pull/223198
More information about the libc-commits
mailing list