linux中my_ls命令,在linux上完成自己的my_ls命令的编写(对dirent.h的研究)

用惯了,ls,想看看ls是如何实现的或者自己写一个自己的ls脚本命令。

偶然在c++沉思录上看到这个问题。关于dirent.h的库。

首先看看dirent是个什么gui,来自无所不知谷歌的解释

Dirent is an application programming interface (API) that enables programmers to list files and directories. Dirent API is commonly available in UNIX systems but not all compilers in Windows provide it. In particular, Microsoft Visual Studio lacks a dirent interface.

Thus, if you are porting software to Windows, you may find yourself rewriting code for Microsoft specific APIs. In order to reduce this work, I have create a clone of the dirent interface for Microsoft Visual Studio. The interface attempts to mimic the genuine UNIX API so that you could use the familiar UNIX data structures and function calls in both Windows and UNIX without modifying source code for one platform or the other.

还有一部分很详细的说明在万能wiki,连接 http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/dirent.h

先实践起来吧,一段源代码

* A simpler and shorter implementation of ls(1)

* ls(1) is very similar to the DIR command on DOS and Windows.

**************************************************************/

#include

#include

int listdir(const char *path) {

struct dirent *entry;

DIR *dp;

dp = opendir(path);

if (dp == NULL) {

perror("opendir");

return -1;

}

while((entry = readdir(dp)))

puts(entry->d_name);

closedir(dp);

return 0;

}

int main(int argc, char **argv) {

int counter = 1;

if (argc == 1)

listdir(".");

while (++counter <= argc) {

printf("\nListing %s...\n", argv[counter-1]);

listdir(argv[counter-1]);

}

return 0;

}

gcc dirent.c -o dirent

然后编写shell脚本

cat > myls

#! bin/sh

./home/gff/cplusplus/dirent/dirent

在home目录下兴建bin文件夹,将此文件夹加入到环境变量中

export PATH=$PATH:$home/bin

ok

实验一下

mkdir test

cd test

cat > a.txt

ctrl d

mkdir box

cd box

cat >b.txt

ctrl d

cd ../

基本目录已经完成

gff@ubuntu:~/test$ls

a.txt box

这是系统ls,只能显示一级目录

看看myls

gff@ubuntu:~/test$myls

..

.

box

a.txt

可以达到同样的效果

下面对于dirent明天再研究吧。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
myls命令包含9个选项: (1) myls : 在缺省选项的情况下,列出当前文件夹下的普通文件(不包含隐藏文件)。 例如:当前目录包含文件home1.c, home2.c, .home3.c,输入myls后,列出的文件名为home1.c, home2.c. (2) myls –a: 列出当前文件夹下的所有文件(包含隐藏文件)。 例如:当前目录包含文件home1.c、home2.c、.home3.c,输入myls -a后,列出所有的文件名为home1.c, home2.c, .home3.c. (3) myls –l: 列出当前文件夹下普通文件的详细信息,包括文件模式,文件链接数,文件所属用户,文件所属用户组,文件大小,文件最后修改时间,文件名。并且在最后一行显示该目录下所显示的文件的文件块数。 例如:显示drwxr-xr-x 3 eli eli 4096 Nov 16 23:07 Desktop (4) myls -R 递归列出文件及其子文件。 例如:当前目录为home, 其包含文件home1, home2, home3. 其home1是目录文件,包含文件home11, home12, home2不是目录文件,home3是目录文件,包含文件home31, home32。 输入myls –R后,列出的文件名为 ./home: home1 home2 home3 ./home/home1: home11 home12 ./home/home3: home31 home32. (5) myls –u: 列出当前文件夹下用户x的普通文件,若输入myls -u bb,则显示所属bb的普通文件。 例如:文件home1, home2, home3属于aa,文件tmp1, tmp2, tmp3属于bb, 则若输入myls –u aa, 则显示home1,home2,home3,若输入myls -u bb, 则显示tmp1,tmp2,tmp3。 (6) myls –S: 对文件进行排序,需要输入比较参数。 myls –S 的参数包括: time——按最近修改时间排序 name——按文件名的字典序排序 size——按文件的大小从小到大排序 link——按文件链接数从少到多排序 (7) myls -1: 将当前文件夹下的文件按照一行一个的方式显示。 (8) myls –s: 在各个文件开头显示这个文件的文件块大小。 (9) myls /dirname: 显示/dirname下的文件。 编译 gcc main.c -o myls 执行 ./myls 可加若干参数,具体见上描述 程序并不完整,可能会有BUG,希望广大网友指点,交流~

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值