MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / ls

Function ls

components/dfs/dfs_v2/src/dfs_file.c:2537–2701  ·  view source on GitHub ↗

* @brief List directory contents with colored output * * This function lists all entries in the specified directory with colored output * that distinguishes different file types. It handles: * - Directories (blue) * - Symbolic links (cyan with target path) * - Executable files (green) * - Character devices (yellow) * - Regular files (default color) * * @param[in] pathname The directory p

Source from the content-addressed store, hash-verified

2535 * @param[in] pathname The directory path to list (NULL for current directory)
2536 */
2537void ls(const char *pathname)
2538{
2539 struct dirent dirent;
2540 struct stat stat;
2541 int length;
2542 char *fullpath, *path;
2543 struct dfs_file file;
2544
2545 if (pathname == NULL)
2546 {
2547#ifdef DFS_USING_WORKDIR
2548 /* open current working directory */
2549 path = rt_strdup(working_directory);
2550#else
2551 path = rt_strdup("/");
2552#endif
2553 if (path == NULL)
2554 {
2555 return; /* out of memory */
2556 }
2557 }
2558 else
2559 {
2560 path = dfs_normalize_path(NULL, (char *)pathname);
2561 if (path == NULL)
2562 {
2563 return; /* out of memory */
2564 }
2565 }
2566
2567 dfs_file_init(&file);
2568
2569 /* list directory */
2570 DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_DIRECTORY, 0)", path);
2571 if (dfs_file_open(&file, path, O_DIRECTORY, 0) >= 0)
2572 {
2573 char *link_fn = (char *)rt_malloc(DFS_PATH_MAX);
2574 if (link_fn)
2575 {
2576 rt_kprintf("Directory %s:\n", path);
2577 do
2578 {
2579 memset(&dirent, 0, sizeof(struct dirent));
2580
2581 DLOG(group, "foreach_item");
2582 DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_getdents(&dirent)");
2583 length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
2584 if (length > 0)
2585 {
2586 DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "dirent.d_name=%s", dirent.d_name);
2587 memset(&stat, 0, sizeof(struct stat));
2588
2589 /* build full path for each file */
2590 fullpath = dfs_normalize_path(path, dirent.d_name);
2591 if (fullpath == NULL)
2592 break;
2593
2594 DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_lstat(%s, &stat)", fullpath);

Callers

nothing calls this directly

Calls 15

rt_strdupFunction · 0.85
dfs_file_initFunction · 0.85
rt_mallocFunction · 0.85
rt_kprintfFunction · 0.85
dfs_file_lstatFunction · 0.85
dfs_file_readlinkFunction · 0.85
dfs_mnt_lookupFunction · 0.85
dfs_file_realpathFunction · 0.85
rt_memcpyFunction · 0.85
rt_strncmpFunction · 0.85
rt_freeFunction · 0.85
dfs_file_deinitFunction · 0.85

Tested by

no test coverage detected