`
linguanghuan
  • 浏览: 3557 次
社区版块
存档分类
最新评论

myls.c 读目录

 
阅读更多

 

#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

 

 

 

0
0
分享到:
评论

相关推荐

    linux C 编程 myls.c

    在linux系统下,用C编程实现自己的ls功能

    implemetation of myls -al

    myls using C. option is -a , -l. colors are added for readability. how to run : $./myls [destination]

    MyLS.rar_MATLAB编程_最小二乘法

    最小二乘法Matlab编程实现//最小二乘法Matlab编程实现

    linux C实现ls命令

    用与目录操作相关的系统调用及库函数实现Linux下的ls 命令。

    自己写的linux的ls指令

    例如:当前目录包含文件home1.c、home2.c、.home3.c,输入myls -a后,列出所有的文件名为home1.c, home2.c, .home3.c. (3) myls –l: 列出当前文件夹下普通文件的详细信息,包括文件模式,文件链接数,文件所属用户...

    Unix编程编写myls(-alrt)命令

    在Unix下编写类似于ls的命令myls,实现参数-a,-l,-r,-t

    高级环境编程之myls

    自己写的高仿终端的ls,比较粗糙,但是可以作为参考用。

    myls浪子提供的一个聊天室很不错的聊天室.rar

    myls浪子提供的一个聊天室很不错的聊天室

    myls:实现linux下的ls命令部分功能

    myls实现linux下的ls命令部分功能命令的不完整实现,可以用于学习linux文件编程

    实验五 复制文件

    完成一个目录复制命令mycp,包括目录下的文件和子目录, 运行结果如下: 说明: Linux: creat,read,write等系统调用,要求支持软链接 Windows: CreateFile(), ReadFile(), WriteFile(), CloseHandle()等函数

    目录与文件属性ls

    a对输出的文件、目录进行排序(如按文件名排、按修改时间排、按类型排), b分栏(输出支持一行多栏), c“.”开头文件(默认隐藏这类文件), d支持参数(如可以执行./myls /tmp); e支持更多选项(阅读man ls,...

    unixListCommand

    unixListCommand 试图模仿UNIX的./ls命令,仅支持-i,-l,-R及其组合-ilR如何使用:./myls [选项] [文件列表] Examples: ./myls -i . ./myls -i testDirectory ./myls -i -l .. ./myls -il ~

    LinuxProgramming:Linux编程ls,chmod,touch实现

    Linux编程Linux编程ls,chmod,touch实现实现功能迈尔斯基本选项实现,例如)myls fileName,ls目录myls -l的实现,例如)myls -l,myls -l fileName,myls -l目录myls -t实施ex)myls -t,myls -t文件名,myls -t...

    MyLS++-crx插件

    语言:English (United States) 有用的调整,可以使MyLearningSpace变得更好!...当前MyLS ++:计算您在课程中达到的当前成绩,并在“成绩”选项卡中显示它! 还会有更多很棒的功能,给我发送电子邮件并提供建议!

    U880 Bin包制作工具

    ZTE U880 Bin Tool (by myls) 软件功能: 1、解包/打包 U880 bin 文件 2、打包时修改分区大小 3、打包时解锁 bootloader 权限和 ramdisk 权限(ramdisk 权限仅适用于 2.3 版本 bin) 4、转换 bmp 文件为 logo.img ...

    错误代码:0x800704cf 不能访问网络位置\解决办法

    而您的工作组的其他机器却可以正常打开和访问这个共享文件夹,出现这种情况主要是Win7的一个已知问题引起的,解决办法,下载后解压有一个 devcon.exe文件,双击该文件后解压至某个目录,例如“E:\myls”,其中会有...

    unix ls命令实现

    c实现unix ls -l命令,里边注释不是很多,但是每个函数都不难

    球蛋白

    启用NeoVim + Coc 启动服务器... RUST_LOG="trace" cargo run... "myls": { "host": "0.0.0.0", "port": 3030, "filetypes": ["puml"] } }, "codeLens.enable": true } 打开文件并使用:set ft=puml更改文件类型

    runnable:用于执行和控制系统命令的 Ruby gem

    class MyLs include Runnable executes :ls end 现在你可以创建一个这样的实例: my_command = LS.new 并按如下方式运行命令 my_command.run 许多其他选项可用; 您可以停止该命令、终止它或查找有关该命令...

Global site tag (gtag.js) - Google Analytics