美文网首页深入理解计算机系统
dirent.h解析和ls命令实现

dirent.h解析和ls命令实现

作者: MachinePlay | 来源:发表于2020-01-13 15:25 被阅读0次

前段时间美团面试让写非递归遍历文件夹里的所有文件(文件夹中可能有文件夹),当时用了个树层次遍历写了代码,面试官好像不熟悉C的库,当时也没解释,在这里复习一下C标准库,以后面试面试官不熟悉就直接看博客把

dirent

dirent.h是用于目录操作的头文件,linux 默认在/usr/include目录下(会自动包含其他文件),常见的方法如下:

The <dirent.h> header shall define the following type:

DIR //结构体
A type representing a directory stream.

It shall also define the structure dirent which shall include the following members:

ino_t  d_ino       File serial number. //inode号
char   d_name[]    Name of entry.  //文件名

The type **ino_t** shall be defined as described in [*<sys/types.h>*]
The character array d_name is of unspecified size, but the number of bytes preceding the terminating null byte shall not exceed {NAME_MAX}.

The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided.

int            closedir(DIR *); //关闭句柄
DIR           *opendir(const char *); //打开名为xxx的文件,返回句柄
struct dirent *readdir(DIR *); //读取文件 返回dirent结构体

void           rewinddir(DIR *);
void           seekdir(DIR *, long);
long           telldir(DIR *); //返回当前指针的位置,表示第几个元素

不同平台下的dirent 结构体各异,如mac:

struct dirent {
        ino_t d_ino;                    /* file number of entry */
        __uint16_t d_reclen;            /* length of this record */
        __uint8_t  d_type;              /* file type, see below */
        __uint8_t  d_namlen;            /* length of string in d_name */
        char d_name[__DARWIN_MAXNAMLEN + 1];    /* name must be no longer than this */
};

这里我们写一个简单的ls命令实现
all.h即apue.h

#include "../all.h"
#include "dirent.h"

int main(int argc, char * argv[]) {
    DIR * dp;
    dirent * dirp;
   
    printf("this is a simple ls \n");
    if (argc != 2) {
        printf("argument not enough\n");
        exit(-1);
    }

    if ((dp = opendir(argv[1])) == NULL) {
        printf("can't open file %s. \n");
        exit(-1);
    }
    
    while((dirp = readdir(dp)) != NULL) {
        printf("filename : %s    ino : %lld\n",dirp->d_name, dirp->d_ino);
    }

    closedir(dp);

    return 0;


}
效果

读者可以尝试使用非递归方法遍历一个文件夹内所有的文件(提示:队列,树层次遍历)

相关文章

  • dirent.h解析和ls命令实现

    前段时间美团面试让写非递归遍历文件夹里的所有文件(文件夹中可能有文件夹),当时用了个树层次遍历写了代码,面试官好像...

  • C/C++ 参数解析 getopt_long 详解

    疑问? 在linux使用中会发现很多命令行程序都要参数如ls -a、ls -a /,参数解析是怎么实现的? 解惑 ...

  • Linxu学习笔记(三)常用命令(* ̄︶ ̄)沉入B站学习

    四、Linux常用命令 1.1 命令格式和目录处理命令ls 命令格式:命令 [-选项][参数]ls -a ...

  • ffmpeg 操作目录

    实现ls命令 #include #include intmain(intargc,char*argv[]) { a...

  • Linux初步之常用命令

    1、管理文件和目录的命令 ls命令:ls命令用来查看目录的内容。功能:列出目录或者文件名称。用法:ls 【选项】【...

  • Go-flag包解析

    简介 godoc-flag 标准库-命令行参数解析flag cobra-解析命令 功能:flag实现命令行解析 使...

  • 常用Linux基础命令学习总结

    文件和目录相关命令 文件系统层次结构标准FHS pwd命令 ls列目录内容 man ls:显示ls命令的帮助说明文...

  • Lunix常用命令行

    常用命令 1.ls命令 ls命令用于显示指定工作目录下之内容ls 命令显示目录。ls -a 命令列出当前目录下所有...

  • 深入理解 Unix / Linux 命令

    1. 命令的剖析 Unix 的命令由2部分组成,命令本身和附加的参数。例如 ls 命令,如果直接执行 ls 命令,...

  • Linux 下常用shell命令

    Linux Shell常用shell命令 一、文件、目录操作命令 1、ls命令 功能:显示文件和目录的信息 ls以...

网友评论

    本文标题:dirent.h解析和ls命令实现

    本文链接:https://www.haomeiwen.com/subject/qsagactx.html