📄 my_ls.c
字号:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
int main()
{
struct stat c_stat;
struct dirent *tmp_file;
struct tm *create_tm;
char buff[500];
int count=0;
DIR *dirtmp_file = opendir(".");
if(dirtmp_file == NULL)
{
printf("OPEN FAILD!\n");
return 0;
}
while(1)
{
memset(&c_stat,0,sizeof(stat));
tmp_file = readdir(dirtmp_file);
if(tmp_file == NULL)
{
break;
}
stat(tmp_file->d_name,&c_stat);
//文件类型
switch(c_stat.st_mode & S_IFMT)
{
case S_IFSOCK:printf("s");break;
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("p");break;
}
// 文件所有者的权限
if(c_stat.st_mode & S_IRUSR)
printf("r");
else
printf("-");
if(c_stat.st_mode & S_IWUSR)
printf("w");
else
printf("-");
if(c_stat.st_mode & S_IXUSR)
printf("x");
else
printf("-");
//用户组的权限
if(c_stat.st_mode & S_IRGRP)
printf("r");
else
printf("-");
if(c_stat.st_mode & S_IWGRP)
printf("w");
else
printf("-");
if(c_stat.st_mode & S_IXGRP)
printf("x");
else
printf("-");
//其他用户的权限
if(c_stat.st_mode & S_IROTH)
printf("r");
else
printf("-");
if(c_stat.st_mode & S_IWOTH)
printf("w");
else
printf("-");
if(c_stat.st_mode & S_IXOTH)
printf("x");
else
printf("-");
//文件硬连接数目
printf("%5d",c_stat.st_nlink);
//用户识别码
printf("%7d",c_stat.st_uid);
//组识别码
printf("%7d",c_stat.st_gid);
//文件大小
printf("%15d",c_stat.st_size);
//创建时间
memset(buff,0,500);
create_tm = localtime(&c_stat.st_mtime);
strftime(buff,500,"%b %d %H:%M",create_tm);
printf(" %s",buff);
//文件名
printf(" %-s\r\n",tmp_file->d_name);
count ++;
}
printf("total %d\r\n",count);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -