Export file examples to MSH

also:
- change static buffer to dynamiclly allocated buffer.
- replace Chinese comment to English comment in some files.
- add apache-2 license
This commit is contained in:
majianjia
2020-04-12 23:56:43 +01:00
parent 5b300113f7
commit 2014663660
5 changed files with 203 additions and 82 deletions
+38 -10
View File
@@ -1,22 +1,19 @@
/*
* File : listdir.c
* This file is part of RT-TestCase in RT-Thread RTOS
* COPYRIGHT (C) 2010, RT-Thread Development Team
* Copyright (c) 2006-2020, 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
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-02-10 Bernard first version
* 2020-04-12 Jianjia Ma add msh cmd
*/
#include <rtthread.h>
#include <dfs_posix.h>
static char fullpath[256];
void list_dir(const char* path)
{
char * fullpath;
DIR *dir;
dir = opendir(path);
@@ -25,6 +22,13 @@ void list_dir(const char* path)
struct dirent* dirent;
struct stat s;
fullpath = rt_malloc(256);
if (fullpath == RT_NULL)
{
rt_kprintf("no memory\n");
return;
}
do
{
dirent = readdir(dir);
@@ -35,7 +39,7 @@ void list_dir(const char* path)
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
stat(fullpath, &s);
if ( s.st_mode & DFS_S_IFDIR )
if ( s.st_mode & S_IFDIR )
{
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
}
@@ -47,10 +51,34 @@ void list_dir(const char* path)
closedir(dir);
}
else rt_kprintf("open %s directory failed\n", path);
else
{
rt_kprintf("open %s directory failed\n", path);
}
rt_free(fullpath);
}
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(list_dir, list directory);
#endif
#ifdef FINSH_USING_MSH
static void cmd_list_dir(int argc, char *argv[])
{
char* filename;
if(argc == 2)
{
filename = argv[1];
}
else
{
rt_kprintf("Usage: list_dir [file_path]\n");
return;
}
list_dir(filename);
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_list_dir, __cmd_list_dir, list directory);
#endif /* FINSH_USING_MSH */
#endif /* RT_USING_FINSH */
+31 -8
View File
@@ -1,15 +1,12 @@
/*
* File : readspeed.c
* This file is part of RT-TestCase in RT-Thread RTOS
* COPYRIGHT (C) 2010, RT-Thread Development Team
* Copyright (c) 2006-2020, 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
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-02-10 Bernard first version
* 2020-04-12 Jianjia Ma add msh cmd
*/
#include <rtthread.h>
@@ -34,7 +31,6 @@ void readspeed(const char* filename, int block_size)
{
rt_kprintf("no memory\n");
close(fd);
return;
}
@@ -61,4 +57,31 @@ void readspeed(const char* filename, int block_size)
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
#endif
#ifdef FINSH_USING_MSH
static void cmd_readspeed(int argc, char *argv[])
{
char* filename;
int block_size;
if(argc == 3)
{
filename = argv[1];
block_size = atoi(argv[2]);
}
else if(argc == 2)
{
filename = argv[1];
block_size = 512;
}
else
{
rt_kprintf("Usage:\nreadspeed [file_path] [block_size]\n");
rt_kprintf("readspeed [file_path] with default block size 512\n");
return;
}
readspeed(filename, block_size);
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_readspeed, __cmd_readspeed, test file system read speed);
#endif /* FINSH_USING_MSH */
#endif /* RT_USING_FINSH */
File diff suppressed because it is too large Load Diff
+9 -10
View File
@@ -1,15 +1,12 @@
/*
* File : seekdir.c
* This file is part of RT-TestCase in RT-Thread RTOS
* COPYRIGHT (C) 2011, RT-Thread Development Team
* Copyright (c) 2006-2020, 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
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2011-06-02 Bernard first version
* 2020-04-12 Jianjia Ma add msh cmd
*/
#include <dfs_posix.h>
@@ -17,7 +14,6 @@ void seekdir_test(void)
{
DIR * dirp;
long save3 = 0;
long cur;
int i = 0;
struct dirent *dp;
@@ -27,14 +23,14 @@ void seekdir_test(void)
{
rt_kprintf("direntry: %s\n", dp->d_name);
/* 保存第三个目录项的目录指针 */
/* save the pointer of the third directory */
if (i++ == 3)
{
save3 = telldir(dirp);
}
}
/* 回到刚才保存的第三个目录项的目录指针 */
/* get back to the third directory */
seekdir (dirp, save3);
rt_kprintf("seek dientry to: %d\n", save3);
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
@@ -42,9 +38,12 @@ void seekdir_test(void)
rt_kprintf("direntry: %s\n", dp->d_name);
}
/* 关闭目录 */
/* close the directory */
closedir (dirp);
}
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test);
MSH_CMD_EXPORT(seekdir_test, perform directory seek test);
#endif /* RT_USING_FINSH */
+34 -8
View File
@@ -1,15 +1,12 @@
/*
* File : writespeed.c
* This file is part of RT-TestCase in RT-Thread RTOS
* COPYRIGHT (C) 2010, RT-Thread Development Team
* Copyright (c) 2006-2020, 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
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-02-10 Bernard first version
* 2020-04-12 Jianjia Ma add msh cmd
*/
#include <rtthread.h>
#include <dfs_posix.h>
@@ -32,7 +29,6 @@ void writespeed(const char* filename, int total_length, int block_size)
{
rt_kprintf("no memory\n");
close(fd);
return;
}
@@ -69,4 +65,34 @@ void writespeed(const char* filename, int total_length, int block_size)
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(writespeed, perform file write test);
#endif
#ifdef FINSH_USING_MSH
static void cmd_writespeed(int argc, char *argv[])
{
char* filename;
int length;
int block_size;
if(argc == 4)
{
filename = argv[1];
length = atoi(argv[2]);
block_size = atoi(argv[3]);
}
else if(argc == 2)
{
filename = argv[1];
block_size = 512;
length = 1024*1024;
}
else
{
rt_kprintf("Usage:\nwritespeed [file_path] [length] [block_size]\n");
rt_kprintf("writespeed [file_path] with default length 1MB and block size 512\n");
return;
}
writespeed(filename, length, block_size);
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_writespeed, __cmd_writespeed, test file system write speed);
#endif /* FINSH_USING_MSH */
#endif /* RT_USING_FINSH */