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