Skip to content

Commit 50512e8

Browse files
authored
Merge pull request #3526 from majianjia/master
Export file examples to MSH
2 parents 5b30011 + 2014663 commit 50512e8

File tree

5 files changed

+203
-82
lines changed

5 files changed

+203
-82
lines changed

examples/file/listdir.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
/*
2-
* File : listdir.c
3-
* This file is part of RT-TestCase in RT-Thread RTOS
4-
* COPYRIGHT (C) 2010, RT-Thread Development Team
2+
* Copyright (c) 2006-2020, RT-Thread Development Team
53
*
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
4+
* SPDX-License-Identifier: Apache-2.0
95
*
106
* Change Logs:
117
* Date Author Notes
128
* 2010-02-10 Bernard first version
9+
* 2020-04-12 Jianjia Ma add msh cmd
1310
*/
1411
#include <rtthread.h>
1512
#include <dfs_posix.h>
1613

17-
static char fullpath[256];
1814
void list_dir(const char* path)
1915
{
16+
char * fullpath;
2017
DIR *dir;
2118

2219
dir = opendir(path);
@@ -25,6 +22,13 @@ void list_dir(const char* path)
2522
struct dirent* dirent;
2623
struct stat s;
2724

25+
fullpath = rt_malloc(256);
26+
if (fullpath == RT_NULL)
27+
{
28+
rt_kprintf("no memory\n");
29+
return;
30+
}
31+
2832
do
2933
{
3034
dirent = readdir(dir);
@@ -35,7 +39,7 @@ void list_dir(const char* path)
3539
rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
3640

3741
stat(fullpath, &s);
38-
if ( s.st_mode & DFS_S_IFDIR )
42+
if ( s.st_mode & S_IFDIR )
3943
{
4044
rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
4145
}
@@ -47,10 +51,34 @@ void list_dir(const char* path)
4751

4852
closedir(dir);
4953
}
50-
else rt_kprintf("open %s directory failed\n", path);
54+
else
55+
{
56+
rt_kprintf("open %s directory failed\n", path);
57+
}
58+
59+
rt_free(fullpath);
5160
}
5261

5362
#ifdef RT_USING_FINSH
5463
#include <finsh.h>
5564
FINSH_FUNCTION_EXPORT(list_dir, list directory);
56-
#endif
65+
66+
#ifdef FINSH_USING_MSH
67+
static void cmd_list_dir(int argc, char *argv[])
68+
{
69+
char* filename;
70+
71+
if(argc == 2)
72+
{
73+
filename = argv[1];
74+
}
75+
else
76+
{
77+
rt_kprintf("Usage: list_dir [file_path]\n");
78+
return;
79+
}
80+
list_dir(filename);
81+
}
82+
FINSH_FUNCTION_EXPORT_ALIAS(cmd_list_dir, __cmd_list_dir, list directory);
83+
#endif /* FINSH_USING_MSH */
84+
#endif /* RT_USING_FINSH */

examples/file/readspeed.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/*
2-
* File : readspeed.c
3-
* This file is part of RT-TestCase in RT-Thread RTOS
4-
* COPYRIGHT (C) 2010, RT-Thread Development Team
2+
* Copyright (c) 2006-2020, RT-Thread Development Team
53
*
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
4+
* SPDX-License-Identifier: Apache-2.0
95
*
106
* Change Logs:
117
* Date Author Notes
128
* 2010-02-10 Bernard first version
9+
* 2020-04-12 Jianjia Ma add msh cmd
1310
*/
1411

1512
#include <rtthread.h>
@@ -34,7 +31,6 @@ void readspeed(const char* filename, int block_size)
3431
{
3532
rt_kprintf("no memory\n");
3633
close(fd);
37-
3834
return;
3935
}
4036

@@ -61,4 +57,31 @@ void readspeed(const char* filename, int block_size)
6157
#ifdef RT_USING_FINSH
6258
#include <finsh.h>
6359
FINSH_FUNCTION_EXPORT(readspeed, perform file read test);
64-
#endif
60+
61+
#ifdef FINSH_USING_MSH
62+
static void cmd_readspeed(int argc, char *argv[])
63+
{
64+
char* filename;
65+
int block_size;
66+
67+
if(argc == 3)
68+
{
69+
filename = argv[1];
70+
block_size = atoi(argv[2]);
71+
}
72+
else if(argc == 2)
73+
{
74+
filename = argv[1];
75+
block_size = 512;
76+
}
77+
else
78+
{
79+
rt_kprintf("Usage:\nreadspeed [file_path] [block_size]\n");
80+
rt_kprintf("readspeed [file_path] with default block size 512\n");
81+
return;
82+
}
83+
readspeed(filename, block_size);
84+
}
85+
FINSH_FUNCTION_EXPORT_ALIAS(cmd_readspeed, __cmd_readspeed, test file system read speed);
86+
#endif /* FINSH_USING_MSH */
87+
#endif /* RT_USING_FINSH */

examples/file/readwrite.c

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,168 @@
11
/*
2-
* 代码清单:文件读写例子
2+
* Copyright (c) 2006-2020, RT-Thread Development Team
33
*
4-
* 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2010-02-10 Bernard first version
9+
* 2020-04-12 Jianjia Ma add msh cmd
510
*/
611

712
#include <rtthread.h>
8-
#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
9-
10-
#define TEST_FN "/test.dat"
13+
#include <dfs_posix.h>
1114

12-
/* 测试用的数据和缓冲 */
13-
static char test_data[120], buffer[120];
15+
#define TEST_DATA_LEN 120
1416

15-
/* 文件读写测试 */
17+
/* file read write test */
1618
void readwrite(const char* filename)
1719
{
1820
int fd;
1921
int index, length;
22+
char* test_data;
23+
char* buffer;
24+
int block_size = TEST_DATA_LEN;
2025

21-
/* 只写 & 创建 打开 */
22-
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
26+
/* open with write only & create */
27+
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
2328
if (fd < 0)
2429
{
2530
rt_kprintf("open file for write failed\n");
2631
return;
2732
}
2833

29-
/* 准备写入数据 */
30-
for (index = 0; index < sizeof(test_data); index ++)
34+
test_data = rt_malloc(block_size);
35+
if (test_data == RT_NULL)
36+
{
37+
rt_kprintf("no memory\n");
38+
close(fd);
39+
return;
40+
}
41+
42+
buffer = rt_malloc(block_size);
43+
if (buffer == RT_NULL)
44+
{
45+
rt_kprintf("no memory\n");
46+
close(fd);
47+
rt_free(test_data);
48+
return;
49+
}
50+
51+
/* prepare some data */
52+
for (index = 0; index < block_size; index ++)
3153
{
3254
test_data[index] = index + 27;
3355
}
3456

35-
/* 写入数据 */
36-
length = write(fd, test_data, sizeof(test_data));
37-
if (length != sizeof(test_data))
57+
/* write to file */
58+
length = write(fd, test_data, block_size);
59+
if (length != block_size)
3860
{
3961
rt_kprintf("write data failed\n");
4062
close(fd);
41-
return;
63+
goto __exit;
4264
}
4365

44-
/* 关闭文件 */
66+
/* close file */
4567
close(fd);
4668

47-
/* 只写并在末尾添加打开 */
48-
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
69+
/* reopen the file with append to the end */
70+
fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0);
4971
if (fd < 0)
5072
{
5173
rt_kprintf("open file for append write failed\n");
52-
return;
74+
goto __exit;;
5375
}
5476

55-
length = write(fd, test_data, sizeof(test_data));
56-
if (length != sizeof(test_data))
77+
length = write(fd, test_data, block_size);
78+
if (length != block_size)
5779
{
5880
rt_kprintf("append write data failed\n");
5981
close(fd);
60-
return;
82+
goto __exit;
6183
}
62-
/* 关闭文件 */
84+
/* close the file */
6385
close(fd);
6486

65-
/* 只读打开进行数据校验 */
66-
fd = open(TEST_FN, O_RDONLY, 0);
87+
/* open the file for data validation. */
88+
fd = open(filename, O_RDONLY, 0);
6789
if (fd < 0)
6890
{
6991
rt_kprintf("check: open file for read failed\n");
70-
return;
92+
goto __exit;
7193
}
7294

73-
/* 读取数据(应该为第一次写入的数据) */
74-
length = read(fd, buffer, sizeof(buffer));
75-
if (length != sizeof(buffer))
95+
/* read the data (should be the data written by the first time ) */
96+
length = read(fd, buffer, block_size);
97+
if (length != block_size)
7698
{
7799
rt_kprintf("check: read file failed\n");
78100
close(fd);
79-
return;
101+
goto __exit;
80102
}
81103

82-
/* 检查数据是否正确 */
83-
for (index = 0; index < sizeof(test_data); index ++)
104+
/* validate */
105+
for (index = 0; index < block_size; index ++)
84106
{
85107
if (test_data[index] != buffer[index])
86108
{
87109
rt_kprintf("check: check data failed at %d\n", index);
88110
close(fd);
89-
return;
111+
goto __exit;
90112
}
91113
}
92114

93-
/* 读取数据(应该为第二次写入的数据) */
94-
length = read(fd, buffer, sizeof(buffer));
95-
if (length != sizeof(buffer))
115+
/* read the data (should be the second time data) */
116+
length = read(fd, buffer, block_size);
117+
if (length != block_size)
96118
{
97119
rt_kprintf("check: read file failed\n");
98120
close(fd);
99-
return;
121+
goto __exit;
100122
}
101123

102-
/* 检查数据是否正确 */
103-
for (index = 0; index < sizeof(test_data); index ++)
124+
/* validate */
125+
for (index = 0; index < block_size; index ++)
104126
{
105127
if (test_data[index] != buffer[index])
106128
{
107129
rt_kprintf("check: check data failed at %d\n", index);
108130
close(fd);
109-
return;
131+
goto __exit;
110132
}
111133
}
112134

113-
/* 检查数据完毕,关闭文件 */
135+
/* close the file */
114136
close(fd);
115-
/* 打印结果 */
116-
rt_kprintf("read/write done.\n");
137+
/* print result */
138+
rt_kprintf("read/write test successful!\n");
139+
140+
__exit:
141+
rt_free(test_data);
142+
rt_free(buffer);
117143
}
118144

119145
#ifdef RT_USING_FINSH
120146
#include <finsh.h>
121-
/* 输出函数到finsh shell命令行中 */
147+
/* export to finsh */
122148
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
123-
#endif
149+
150+
#ifdef FINSH_USING_MSH
151+
static void cmd_readwrite(int argc, char *argv[])
152+
{
153+
char* filename;
154+
155+
if(argc == 2)
156+
{
157+
filename = argv[1];
158+
}
159+
else
160+
{
161+
rt_kprintf("Usage: readwrite [file_path]\n");
162+
return;
163+
}
164+
readwrite(filename);
165+
}
166+
FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test);
167+
#endif /* FINSH_USING_MSH */
168+
#endif /* RT_USING_FINSH */

0 commit comments

Comments
 (0)