MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / list_dir

Function list_dir

examples/file/listdir.c:18–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16#include <sys/statfs.h>
17
18void list_dir(const char* path)
19{
20 char * fullpath = RT_NULL;
21 DIR *dir;
22
23 dir = opendir(path);
24 if (dir != RT_NULL)
25 {
26 struct dirent* dirent;
27 struct stat s;
28
29 fullpath = rt_malloc(256);
30 if (fullpath == RT_NULL)
31 {
32 closedir(dir);
33 rt_kprintf("no memory\n");
34 return;
35 }
36
37 do
38 {
39 dirent = readdir(dir);
40 if (dirent == RT_NULL) break;
41 rt_memset(&s, 0, sizeof(struct stat));
42
43 /* build full path for each file */
44 rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
45
46 stat(fullpath, &s);
47 if ( s.st_mode & S_IFDIR )
48 {
49 rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
50 }
51 else
52 {
53 rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
54 }
55 } while (dirent != RT_NULL);
56
57 closedir(dir);
58 }
59 else
60 {
61 rt_kprintf("open %s directory failed\n", path);
62 }
63
64 if (RT_NULL != fullpath)
65 {
66 rt_free(fullpath);
67 }
68}
69
70#ifdef RT_USING_FINSH
71#include <finsh.h>

Callers 1

cmd_list_dirFunction · 0.85

Calls 9

rt_mallocFunction · 0.85
rt_kprintfFunction · 0.85
rt_memsetFunction · 0.85
rt_sprintfFunction · 0.85
rt_freeFunction · 0.85
statClass · 0.70
opendirFunction · 0.50
closedirFunction · 0.50
readdirFunction · 0.50

Tested by

no test coverage detected