linux编程实现ls功能,Linux下实现ls--l功能c程序.doc

程序截图

#include

#include

#include

#include

#include

#include

#include

#include

#include

void display_file(char *filename);

void display_dir(char *dirname);

int main(int argc,char *argv[])

{

struct stat buf;

if(argc < 2)

{

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

return 0;

}

if(lstat(argv[1],&buf)== -1)

{

perror("stat");

return -1;

}

if(S_ISDIR(buf.st_mode))//判断是否是文件夹

{

display_dir(argv[1]);

}

else

display_file(argv[1]);//不是文件夹情况

return 0;

}

void display_dir(char *dirname)

{

DIR *dp;

struct dirent *filename;

char dirfilename[100]={0};

int i=0;

dp=opendir(dirname);

if(dp == NULL)

{

perror("opendtr");

}

while((filename=readdir(dp))!=NULL)

{

if(strcmp(".",filename->d_name)==0||strcmp("..",filename->d_name)==0)

continue;

sprintf(dirfilename,"%s/%s",dirname,filename->d_name);//把文件夹中的文件名以字符串型式写到数组中

display_file(dirfilename);//调用文件函数

}

closedir(dp);

}

void display_file(char *filename)

{

int i=8;

struct stat buf;

struct tm *p;

if(lstat(filename,&buf)==-1)

{

perror("stat");

//return -1;

}

switch(buf.st_mode & 0170000)

{

case S_IFLNK: printf("l");break;

case S_IFREG: printf("-");break;

case S_IFBLK: printf("b");break;

case S_IFDIR: printf("d");break;

case S_IFCHR: printf("c");break;

case S_IFIFO: printf("i");break;

case S_IFSOCK: printf("s");break;

}

for(i=8;i>=0;i--)

{

if(buf.st_mode & (1<

{

switch(i%3)

{

case 0:printf("x");break;

case 1:printf("w");break;

case 2:printf("r");break;

}

}

else

printf("-");

}

p=localtime(&buf.st_mtime);

printf(" %d %s %s %ld %d-%0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值