listdir.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. static char fullpath[256];
  17. void list_dir(const char* path)
  18. {
  19. DIR *dir;
  20. dir = opendir(path);
  21. if (dir != RT_NULL)
  22. {
  23. struct dirent* dirent;
  24. struct stat s;
  25. do
  26. {
  27. dirent = readdir(dir);
  28. if (dirent == RT_NULL) break;
  29. rt_memset(&s, 0, sizeof(struct stat));
  30. /* build full path for each file */
  31. rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
  32. stat(fullpath, &s);
  33. if ( s.st_mode & DFS_S_IFDIR )
  34. {
  35. rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
  36. }
  37. else
  38. {
  39. rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
  40. }
  41. } while (dirent != RT_NULL);
  42. closedir(dir);
  43. }
  44. else rt_kprintf("open %s directory failed\n", path);
  45. }
  46. #ifdef RT_USING_FINSH
  47. #include <finsh.h>
  48. FINSH_FUNCTION_EXPORT(list_dir, list directory);
  49. #endif