diff options
| author | Bernard Xiong <[email protected]> | 2020-10-18 14:10:09 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-10-18 14:10:09 +0800 |
| commit | fe66001bd9da353f8e2395467ad83a6c421114b3 (patch) | |
| tree | e834e48fd17bb4317901b0d682d7048a053eb422 /examples | |
| parent | 917beb74624921c586bbd0bf01c7a2aaf01d4056 (diff) | |
| parent | adc5dab5b23c0b9d623142ad9252312476cd7717 (diff) | |
Merge pull request #3969 from DavidLin1577/patch-12
Update listdir.c
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/file/listdir.c | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 98419db873..d8ea7af82a 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -14,49 +14,50 @@ void list_dir(const char* path) { char * fullpath; - DIR *dir; + DIR *dir; - dir = opendir(path); - if (dir != RT_NULL) - { - struct dirent* dirent; - struct stat s; + dir = opendir(path); + if (dir != RT_NULL) + { + struct dirent* dirent; + struct stat s; - fullpath = rt_malloc(256); - if (fullpath == RT_NULL) - { - rt_kprintf("no memory\n"); - return; - } + fullpath = rt_malloc(256); + if (fullpath == RT_NULL) + { + closedir(dir); + rt_kprintf("no memory\n"); + return; + } - do - { - dirent = readdir(dir); - if (dirent == RT_NULL) break; - rt_memset(&s, 0, sizeof(struct stat)); + do + { + dirent = readdir(dir); + if (dirent == RT_NULL) break; + rt_memset(&s, 0, sizeof(struct stat)); - /* build full path for each file */ - rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); + /* build full path for each file */ + rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); - stat(fullpath, &s); - if ( s.st_mode & S_IFDIR ) - { - rt_kprintf("%s\t\t<DIR>\n", dirent->d_name); - } - else - { - rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size); - } - } while (dirent != RT_NULL); + stat(fullpath, &s); + if ( s.st_mode & S_IFDIR ) + { + rt_kprintf("%s\t\t<DIR>\n", dirent->d_name); + } + else + { + rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size); + } + } while (dirent != RT_NULL); - closedir(dir); - } - else - { - rt_kprintf("open %s directory failed\n", path); - } + closedir(dir); + } + else + { + rt_kprintf("open %s directory failed\n", path); + } - rt_free(fullpath); + rt_free(fullpath); } #ifdef RT_USING_FINSH |
