* @brief Display file contents to standard output * * This function reads and prints the contents of the specified file to the console. * It handles both text and binary files by reading in chunks and printing the output. * * @param[in] filename Path to the file to be displayed */
| 2709 | * @param[in] filename Path to the file to be displayed |
| 2710 | */ |
| 2711 | void cat(const char *filename) |
| 2712 | { |
| 2713 | int length = 0; |
| 2714 | char buffer[81]; |
| 2715 | struct dfs_file file; |
| 2716 | |
| 2717 | if (filename && dfs_file_isdir(filename) == 0) |
| 2718 | { |
| 2719 | rt_kprintf("cat: %s Is a directory\n", filename); |
| 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | dfs_file_init(&file); |
| 2724 | |
| 2725 | DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_RDONLY, 0)", filename); |
| 2726 | if (dfs_file_open(&file, filename, O_RDONLY, 0) < 0) |
| 2727 | { |
| 2728 | rt_kprintf("Open %s failed\n", filename); |
| 2729 | dfs_file_deinit(&file); |
| 2730 | return; |
| 2731 | } |
| 2732 | |
| 2733 | do |
| 2734 | { |
| 2735 | rt_memset(buffer, 0x0, sizeof(buffer)); |
| 2736 | DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_read(fd, buffer, %d)", sizeof(buffer) - 1); |
| 2737 | length = dfs_file_read(&file, (void *)buffer, sizeof(buffer) - 1); |
| 2738 | if (length > 0) |
| 2739 | { |
| 2740 | buffer[length] = '\0'; |
| 2741 | rt_kprintf("%s", buffer); |
| 2742 | } |
| 2743 | } while (length > 0); |
| 2744 | rt_kprintf("\n"); |
| 2745 | |
| 2746 | DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()"); |
| 2747 | dfs_file_close(&file); |
| 2748 | dfs_file_deinit(&file); |
| 2749 | } |
| 2750 | |
| 2751 | #define BUF_SZ 4096 |
| 2752 | /** |
nothing calls this directly
no test coverage detected