* @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
| 2535 | * @param[in] pathname The directory path to list (NULL for current directory) |
| 2536 | */ |
| 2537 | void 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); |
nothing calls this directly
no test coverage detected