myls.c 读目录

本文介绍了一个使用C语言实现的简单目录遍历程序。通过调用opendir(), readdir()等函数,程序能够列出指定目录下的所有文件及子目录。文章还详细解释了dirent结构体的组成部分,并对比了myls程序与标准ll命令的输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        DIR *dp;
        struct dirent *dirp;
        if (argc != 2)
        {
                printf("usage: ls directory_name\n");
                exit(1);
        }
        if ((dp = opendir(argv[1])) == NULL)
        {
                printf("can't open %s\n", argv[1]);
                exit(1);
        }
        while ((dirp = readdir(dp)) != NULL)
        {
                printf("%s\n", dirp->d_name);
        }
        closedir(dp);
        exit(0);
}

 

 

编译 
[lingh@test advanced_unix]$ gcc -o myls myls.c
[lingh@test advanced_unix]$ ls
myls myls.c

 

myls运行结果 

[lingh@test advanced_unix]$ myls
usage: ls directory_name
[lingh@test advanced_unix]$ myls .
..
myls
myls.c
.
[lingh@test advanced_unix]$ myls /
root
tmp
selinux
bin
sbin
proc
home
misc
ora
net
usr
etc
var
dev
media
opt
lib
.
lost+found
lib64
..
.autofsck
boot
mnt
sys
srv
cgroup

 

 知识点整理:

 

  1. DIR, struct dirent, opendir(), readdir()  需要包含头文件 dirent.h

  2. exit() 需要包含头文件stdlib.h

  3  DIR *opendir(const char *pathname);   

         成功返回指针,失败返回NULL

  4  struct dirent * readdir(DIR *dp);

         成功返回指针,若在目录结尾或者出错返回NULL, 参数dp为opendir函数的返回值

      opendir执行初始化操作,使第一个readdir读取目录中的第一个目录项。目录中的各目录项的顺序与实现有关。它们通常并不按字母顺序排列。

 

 5.

     struct dirent{

          ino_t d_ino;

          char d_name[NAME_MAX+1];

      }

dirent 至少包含以下2项:

      d_ino: i-node值

      d_name: 文件名

 

实际这个结构体的定义是在bits/dirent.h文件中

[lingh@test include]$ grep -rn "struct dirent" *

bits/dirent.h:23:struct dirent

内容如下:

 

struct dirent
  {
#ifndef __USE_FILE_OFFSET64
    __ino_t d_ino;
    __off_t d_off;
#else
    __ino64_t d_ino;
    __off64_t d_off;
#endif
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];           /* We must not include limits.h! */
  };

 

struct dirent
{
long d_ino; /* inode number  索引节点号 */
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
unsigned short d_reclen; /* length of this d_name 文件名长 */
unsigned char d_type; /* the type of d_name 文件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长256字符 */
}

 

 

 

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        DIR *dp;
        struct dirent *dirp;
        if (argc != 2)
        {
                printf("usage: %s directory_name\n", argv[0]);
                exit(1);
        }
        if ((dp = opendir(argv[1])) == NULL)
        {
                printf("can't open %s\n", argv[1]);
                exit(1);
        }
        while ((dirp = readdir(dp)) != NULL)
        {
                printf("d_name:%s\td_ino:%d\td_reclen:%d\td_type[d]:%d\td_type[c]:%c\n", dirp->d_name, dirp->d_ino, dirp->d_reclen, dirp->d_type, dirp->d_type);
        }
        closedir(dp);
        exit(0);
}

 

 printf("usage: %s directory_name\n", argv[0]);

不写死提示,argv[0]代表可执行文件本身,这样可以编译结果可以灵活指定可执行文件的名称

ll -a 和 myls结果比较 
[lingh@test advanced_unix]$ ll -a
总用量 20
drwxrwxr-x. 2 lingh lingh 4096 4月 23 21:01 .
drwxr-xr-x. 7 lingh lingh 4096 4月 23 21:01 ..
-rwxrwxr-x. 1 lingh lingh 7406 4月 23 21:01 myls
-rw-rw-r--. 1 lingh lingh 511 4月 23 21:01 myls.c

 

myls结果 
[lingh@test advanced_unix]$ myls .
d_name:.. d_ino:1569793 d_off:2261691002226484046 d_reclen:24 d_type[d]:4 d_type[c]:
d_name:myls d_ino:1569795 d_off:4147461195343482675 d_reclen:24 d_type[d]:8 d_type[c]:
d_name:myls.c d_ino:1571816 d_off:7804648911474585207 d_reclen:32 d_type[d]:8 d_type[c]:
d_name:. d_ino:1571813 d_off:9223372036854775807 d_reclen:24 d_type[d]:4 d_type[c]:

 

参考:

1. unix 高级环境编程 第二版  p4,p120

2. 百度百科:dirent

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值