listdir.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * File : listdir.c
  3. * This file is part of RT-TestCase in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-02-10 Bernard first version
  13. */
  14. #include <rtthread.h>
  15. #include <dfs_posix.h>
  16. void list_dir(const char* path)
  17. {
  18. DIR *dir;
  19. dir = opendir(path);
  20. if (dir != RT_NULL)
  21. {
  22. struct dfs_dirent* dirent;
  23. struct dfs_stat s;
  24. do
  25. {
  26. dirent = readdir(dir);
  27. if (dirent == RT_NULL) break;
  28. rt_memset(&s, 0, sizeof(struct dfs_stat));
  29. /* build full path for each file */
  30. rt_sprintf(fullpath, "/%s", dirent->d_name);
  31. stat(fullpath, &s);
  32. if ( s.st_mode & DFS_S_IFDIR )
  33. {
  34. rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
  35. }
  36. else
  37. {
  38. rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
  39. }
  40. } while (dirent != RT_NULL);
  41. closedir(dir);
  42. }
  43. else rt_kprintf("open %s directory failed\n", path);
  44. }
  45. #ifdef RT_USING_FINSH
  46. #include <finsh.h>
  47. FINSH_FUNCTION_EXPORT(list_dir, list directory);
  48. #endif