1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:17:35 +00:00

LibC: Implement scandir(...) to enumerate directories.

I ran into a need for this when running  stress-ng against the system.
This change implements the full functionality of scandir, where it
accepts a selection callback, as well as a comparison callback.
These can be used to trim and sort the entries from the directory
that we are being asked to enumerate. A test was also included to
validate the new functionality.
This commit is contained in:
Brian Gianforcaro 2021-05-02 02:36:59 -07:00 committed by Andreas Kling
parent d4d988532a
commit 331ab52318
4 changed files with 95 additions and 0 deletions

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <dirent.h>
#include <string.h>
TEST_CASE(scandir_basic_scenario)
{
struct dirent** namelist = nullptr;
auto entries = scandir("/etc", &namelist, nullptr, nullptr);
EXPECT(entries > 0);
EXPECT_NE(namelist, nullptr);
bool found_passwd = false;
for (auto i = 0; i < entries; i++) {
if (strcmp(namelist[i]->d_name, "passwd") == 0) {
found_passwd = true;
break;
}
free(namelist[i]);
}
EXPECT(found_passwd);
free(namelist);
}