-
Notifications
You must be signed in to change notification settings - Fork 6
/
tell_seek_dir_sample.c
69 lines (61 loc) · 1.77 KB
/
tell_seek_dir_sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*
*/
/*
* 程序清单:保存与设置读取目录位置
*
* 程序会创建一个操作文件的函数并导出到msh命令列表
* 在函数中调用 telldir() 函数
* off_t telldir(DIR *d); 获取目录流的读取位置
* void seekdir(DIR *d, off_t offset); 设置下次读取目录的位置
*/
#include <rtthread.h>
#if RT_VER_NUM >= 0x40100
#include <fcntl.h> /* 当需要使用文件操作时,需要包含这个头文件 */
#else
#include <dfs_posix.h>
#endif /*RT_VER_NUM >= 0x40100*/
/* 假设文件操作是在一个线程中完成 */
static void telldir_sample(void)
{
DIR *dirp;
int save3 = 0;
int cur;
int i = 0;
struct dirent *dp;
/* 打开根目录 */
rt_kprintf("the directory is:\n");
dirp = opendir("/");
for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp))
{
/* 保存第三个目录项的目录指针*/
i++;
if (i == 3)
save3 = telldir(dirp);
rt_kprintf("%s\n", dp->d_name);
}
/* 回到刚才保存的第三个目录项的目录指针*/
seekdir(dirp, save3);
/* 检查当前目录指针是否等于保存过的第三个目录项的指针. */
cur = telldir(dirp);
if (cur != save3)
{
rt_kprintf("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur);
}
/* 从第三个目录项开始打印*/
rt_kprintf("the result of tell_seek_dir is:\n");
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
{
rt_kprintf("%s\n", dp->d_name);
}
/* 关闭目录*/
closedir(dirp);
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(telldir_sample, telldir sample);