#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>

/*
 * 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;
}

linux readdir相关推荐

  1. linux readdir对结果排序,c-readdir()是否保证顺序?

    c-readdir()是否保证顺序? 我正在使用opendir / readdir在类似Linux的系统上获取文件列表. 看来目录条目是按文件名的字母顺序返回的. 但是,我在手册页中看不到任何关于此顺 ...

  2. linux readdir对结果排序,Perl readdir按顺序排列

    有没有办法保证readdir返回的列表中的订单? 我有代码: opendir(my $DIR, $src) or die "Error opening $src"; # Loop ...

  3. linux进程隐藏 hook readdir函数 挂载覆盖/proc/pid 目录

    前言 上篇介绍了如何在有源码的情况下,通过 argv[] 及 prctl 对进程名及参数进行修改,整篇围绕/proc/pid/目录和 ps.top 命令进行分析,做到了初步隐藏,即修改了 /proc/ ...

  4. linux opendir readdir closedir 的使用

    2012-06-04 10:27 linux opendir readdir closedir 的使用 在Linux下opendir().readdir()和closedir()这三个函数主要用来遍历 ...

  5. Linux库函数之opendir/closedir/readdir

    在Linux环境下,有时候需要编写一些实用的工具,比如检索功能,最近在做病毒查杀应用开发,涉及到批量扫描指定目录下文件, 因为要测试大量病毒文件,该部分功能又是要通过API集成到其他应用软件中,设计时 ...

  6. linux C 遍历目录及其子目录 opendir -> readdir -> closedir

    在 linux 下遍历某一目录下内容 LINUX 下历遍目录的方法一般是这样的: 打开目录->读取->关闭目录 相关函数是 opendir -> readdir -> clos ...

  7. Linux下 C 遍历目录(opendir,readdir函数)

    opendir()函数: 头文件: #include <sys/types.h> #include <dirent.h> 函数原型: Dir* opendir(const ch ...

  8. linux c readdir 顺序,Linux读取目录函数readdir以及inode结构简介

    readdir 语法: struct dirent* readdir(DIR* dir_handle); 返回值: dirent结构 函数种类: 文件存取 内容说明 本函数用来读取目录.返回目录中的文 ...

  9. linux的readir函数,Linux编程--readdir

    作用 在Linux中, readdir是常用来遍历文件夹下的文件 使用方法 通常readdir都是与opendir配合使用. 通过opendir打开的目录, 使用readdir来进行遍历读取 #inc ...

最新文章

  1. 列表表格以及媒体元素
  2. TensorFlow 2.0 极简教程,不到 20 行代码带你入门
  3. 上下文管理、线程池、redis订阅和发布
  4. 分类算法之决策树CART算法
  5. java输错重新输入_java程序在dos界面运行时输入错误后返回重新输入的方法
  6. PCA对特征点描述子降维
  7. java 接口隔离_《Java设计模式及实践》—1.5.4 接口隔离原则
  8. Jmeter读取Excel,BeanShell取样器调用rt.jar和jxl.jar
  9. [BZOJ1877][SDOI2009]SuperGCD
  10. C++查缺补漏,赶紧的
  11. 高通的快充协议_高通QC5.0快充发布:百瓦级时代,高通被国产厂商牵着鼻子走了?...
  12. kubernetes安装_在 Kubernetes 上安装 Gitlab CI Runner
  13. Xshell连接centOS7与CentOS7联网——一步到位
  14. 基于matlab的中值滤波算法浅析
  15. 详细解读行人重识别的k-reciprocal Encoding(k个相互近邻编码方法) re-ranking方法及其实现代码解读
  16. 三维建筑动画让你看懂真实的设计图
  17. 小红书关键词搜索商品API接口(商品列表数据接口)
  18. 【Leetcode】1324. Print Words Vertically
  19. 为什么我们不能坚持?
  20. java学习笔记(8) 第8章(下)- 面向对象编程(中级部分) - 练习题

热门文章

  1. 手机自动化安装、配置详解
  2. go protobuf v1败给了gogo protobuf,那v2呢?
  3. mysql分库分表 tddl,阿里巴巴中间件TDDL用于连接数据库,分表分库查询
  4. ioctl_cfg80211.c:9477:18: error: ‘WIPHY_FLAG_SUPPORTS_SCHED_SCAN’ undeclared
  5. 如何证明自己的软件著作权
  6. Persistence.xml 配置说明
  7. android镜像分析
  8. 共享文件突然不能访问了
  9. ios企业签名在线签名网站有哪些?
  10. 东芝L730-T21N 升级小记