实现linux ls-l,实现linux ls -l的功能(简易模仿 欢迎指正 粗糙勿喷)

#include

#include

#include

#include

#include

#include

#include

char* file_time(time_t t,char* str)

{

struct tm* it = localtime(&t);

sprintf(str,"%4d-%02d-%02d %02d:%02d:%02d",it->tm_year+1900,it->tm_mon+1,it->tm_mday,it->tm_hour,it->tm_min,it->tm_sec);

return str;

}

char* file_mode(mode_t m,char* str)

{

if(S_ISREG(m))

str[0] = '-';

else if(S_ISDIR(m))

str[0] = 'd';

else if(S_ISCHR(m))

str[0] = 'c';

else if(S_ISBLK(m))

str[0] = 'b';

else if(S_ISFIFO(m))

str[0] = 'q';

else if(S_ISLNK(m))

str[0] = 'l';

else if(S_ISSOCK(m))

str[0] = 's';

else

str[0] = '?';

str[1] = '\0';

strcat(str,S_IRUSR&m?"r":"-");

strcat(str,S_IWUSR&m?"w":"-");

strcat(str,S_IXUSR&m?"x":"-");

strcat(str,S_IRGRP&m?"r":"-");

strcat(str,S_IWGRP&m?"w":"-");

strcat(str,S_IXGRP&m?"x":"-");

strcat(str,S_IROTH&m?"r":"-");

strcat(str,S_IWOTH&m?"w":"-");

strcat(str,S_IXOTH&m?"x":"-");

return str;

}

int main()

{

DIR* dp = opendir(".");

if(NULL == dp)

{

perror("opendir");

return -1;

}

struct dirent* de = readdir(dp);

struct stat buf = {};

char str[50] = {};

for(de; NULL!=de;de=readdir(dp))

{

stat(de->d_name,&buf);

printf("%s ",file_mode(buf.st_mode,str));

printf("zhizhen ");

printf("zhizhen ");

printf("%s ",file_time(buf.st_mtime,str));

printf("%s\n",de->d_name);

}

}

标签:de,strcat,else,tm,指正,ls,str,linux,include

来源: https://blog.csdn.net/weixin_44916364/article/details/97486942

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值