listdir.c 1.2 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * File      : listdir.c
 * This file is part of RT-TestCase in RT-Thread RTOS
 * COPYRIGHT (C) 2010, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2010-02-10     Bernard      first version
 */
#include <rtthread.h>
#include <dfs_posix.h>

B
bernard.xiong 已提交
17
static char fullpath[256];
B
bernard.xiong 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
void list_dir(const char* path)
{
	DIR *dir;

	dir = opendir(path);
	if (dir != RT_NULL)
	{
		struct dfs_dirent* dirent;
		struct dfs_stat s;

		do
		{
			dirent = readdir(dir);
			if (dirent == RT_NULL) break;
			rt_memset(&s, 0, sizeof(struct dfs_stat));

			/* build full path for each file */
			rt_sprintf(fullpath, "/%s", dirent->d_name);

			stat(fullpath, &s);
			if ( s.st_mode & DFS_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);
}

#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(list_dir, list directory);
#endif