| 783 | #include <finsh.h> |
| 784 | |
| 785 | void ls(const char *pathname) |
| 786 | { |
| 787 | struct dfs_file fd; |
| 788 | struct dirent dirent; |
| 789 | struct stat stat; |
| 790 | int length; |
| 791 | char *fullpath, *path; |
| 792 | |
| 793 | fullpath = NULL; |
| 794 | if (pathname == NULL) |
| 795 | { |
| 796 | #ifdef DFS_USING_WORKDIR |
| 797 | /* open current working directory */ |
| 798 | path = rt_strdup(working_directory); |
| 799 | #else |
| 800 | path = rt_strdup("/"); |
| 801 | #endif |
| 802 | if (path == NULL) |
| 803 | return ; /* out of memory */ |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | path = (char *)pathname; |
| 808 | } |
| 809 | |
| 810 | fd_init(&fd); |
| 811 | /* list directory */ |
| 812 | if (dfs_file_open(&fd, path, O_DIRECTORY) == 0) |
| 813 | { |
| 814 | rt_kprintf("Directory %s:\n", path); |
| 815 | do |
| 816 | { |
| 817 | rt_memset(&dirent, 0, sizeof(struct dirent)); |
| 818 | length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent)); |
| 819 | if (length > 0) |
| 820 | { |
| 821 | rt_memset(&stat, 0, sizeof(struct stat)); |
| 822 | |
| 823 | /* build full path for each file */ |
| 824 | fullpath = dfs_normalize_path(path, dirent.d_name); |
| 825 | if (fullpath == NULL) |
| 826 | break; |
| 827 | |
| 828 | if (dfs_file_stat(fullpath, &stat) == 0) |
| 829 | { |
| 830 | rt_kprintf("%-20s", dirent.d_name); |
| 831 | if (S_ISDIR(stat.st_mode)) |
| 832 | { |
| 833 | rt_kprintf("%-25s\n", "<DIR>"); |
| 834 | } |
| 835 | else |
| 836 | { |
| 837 | rt_kprintf("%-25lu\n", (unsigned long)stat.st_size); |
| 838 | } |
| 839 | } |
| 840 | else |
| 841 | rt_kprintf("BAD file: %s\n", dirent.d_name); |
| 842 | rt_free(fullpath); |
no test coverage detected