#include
#include
#include
#include
#include
#include
/*
* readdir()函数返回一个指向目录结构的指针,该结构表示目录流中由p_dir指向的下一个目录条目。
* 当到达目录流的末尾或发生错误时,它将返回NULL。
*/
#if 0
struct dirent *readdir(DIR * p_dir);
struct dirent {
ino_t d_ino; // 文件的inode
off_t d_off; // 该文件相对于文件夹的偏移量
unsigned short d_reclen; // d_name的长度
unsigned char d_type; // 文件类型, 例如管道, Socket , Block等
char d_name[256]; // 文件名
};
The only fields in the dirent structure that are mandated by
POSIX.1 are d_name and d_ino. The other fields are
unstandardized, and not present on all systems; see NOTES below
for some further details.
The fields of the dirent structure are as follows:
d_ino This is the inode number of the file.
d_off The value returned in d_off is the same as would be
returned by calling telldir(3) at the current position in
the directory stream. Be aware that despite its type and
name, the d_off field is seldom any kind of directory
offset on modern filesystems. Applications should treat
this field as an opaque value, making no assumptions about
its contents; see also telldir(3).
d_reclen
This is the size (in bytes) of the returned record. This
may not match the size of the structure definition shown
above; see NOTES.
d_type This field contains a value indicating the file type,
making it possible to avoid the expense of calling
lstat(2) if further actions depend on the type of the
file.
When a suitable feature test macro is defined
(_DEFAULT_SOURCE on glibc versions since 2.19, or
_BSD_SOURCE on glibc versions 2.19 and earlier), glibc
defines the following macro constants for the value
returned in d_type:
DT_BLK This is a block device.
DT_CHR This is a character device.
DT_DIR This is a directory.
DT_FIFO
This is a named pipe (FIFO).
DT_LNK This is a symbolic link.
DT_REG This is a regular file.
DT_SOCK
This is a UNIX domain socket.
DT_UNKNOWN
The file type could not be determined.
Currently, only some filesystems (among them: Btrfs, ext2,
ext3, and ext4) have full support for returning the file
type in d_type. All applications must properly handle a
return of DT_UNKNOWN.
d_name This field contains the null terminated filename. See
NOTES.
The data returned by readdir() may be overwritten by subsequent
calls to readdir() for the same directory stream.
#endif
static int test()
{
DIR * p_dir = NULL;
struct dirent * dp = NULL;
int len = -1;
char *name = "aaaaa";
// 通过opendir打开目录
p_dir = opendir("/proc");
if (p_dir == NULL) {
return (-1);
}
len = strlen(name);
// 开始遍历p_dir文件夹
while ((dp = readdir(p_dir)) != NULL) {
printf("name:%s:%d\n", dp->d_name, dp->d_type);
// 比较dp->d_name与name的值, 如果匹配则返回
if (dp->d_reclen == len && strcmp(dp->d_name, name) == 0) {
(void)closedir(p_dir);
return 0;
}
}
// 关闭dir
(void)closedir(p_dir);
return 0;
}
int main()
{
test();
return 0;
}