📄 ls.c
字号:
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void wenjian_leixing();
void wenjian_quanxian();
void wenjian_xinxi();
void shuchu_xinxi();
struct passwd *usrinfo;
struct group *gpinfo;
struct stat statbuf;
char wjlx;
char wjqx[9];
int lnum;
int size;
time_t lt;
char str[30];
int main(int argc,char *argv[])
{
DIR *dp;
char buf[255];
struct dirent *dirp;
int i,j = 0;
int dir_name_len = 0;
if(argc == 1)
{
getcwd(buf,255);
if((dp=opendir(buf)) == NULL)
printf("can't open %s",buf);
while((dirp = readdir(dp)) != NULL)
{
if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
continue;
if(lstat(dirp->d_name,&statbuf) < 0)
{
perror("lstat error!\n");
continue;
}
if(j != 0&&j%5 == 0)
printf("\n");
j++;
printf("%-16s",dirp->d_name);
}
printf("\n");
closedir(dp);
exit(0);
}
else if(argc == 2&&(strcmp(argv[1],"-l") == 0))
{
getcwd(buf,255);
if((dp=opendir(buf)) == NULL)
printf("can't open %s",buf);
while((dirp = readdir(dp)) != NULL)
{
if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
continue;
if(lstat(dirp->d_name,&statbuf) < 0)
{
perror("lstat error!\n");
continue;
}
wenjian_leixing();
wenjian_quanxian();
wenjian_xinxi();
shuchu_xinxi();
printf(" %s\n",dirp->d_name);
}
closedir(dp);
exit(0);
}
else if((strcmp(argv[1],"-l") == 0))
{
for(i = 2;i < argc;i++)
{
if(lstat(argv[i],&statbuf)<0)
{
perror("lstat error!\n");
continue;
}
if(!(S_ISDIR(statbuf.st_mode)))
{
wenjian_leixing();
wenjian_quanxian();
wenjian_xinxi();
shuchu_xinxi();
printf(" %s\n",argv[i]);
printf("\n");
}
else
{
printf("%s\n",argv[i]);
if((dp=opendir(argv[i])) == NULL)
printf("can't open %s",argv[i]);
while((dirp = readdir(dp)) != NULL)
{
if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
continue;
strcpy(buf,argv[i]);
strcat(buf,"/");
strcat(buf,dirp->d_name);
if(lstat(buf,&statbuf) < 0)
{
perror("lstat error\n");
continue;
}
wenjian_leixing();
wenjian_quanxian();
wenjian_xinxi();
shuchu_xinxi();
printf(" %s\n",dirp->d_name);
}
printf("\n");
closedir(dp);
}
}
}
else
{
for(i = 1;i < argc;i++)
{
if(lstat(argv[i],&statbuf)<0)
{
perror("lstat error!\n");
continue;
}
if(!(S_ISDIR(statbuf.st_mode)))
{
printf("%s\n",argv[i]);
printf("\n");
}
else
{
printf("%s\n",argv[i]);
if((dp=opendir(argv[i])) == NULL)
printf("can't open %s",argv[i]);
while((dirp = readdir(dp)) != NULL)
{
if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
continue;
strcpy(buf,argv[i]);
strcat(buf,"/");
strcat(buf,dirp->d_name);
if(lstat(buf,&statbuf) < 0)
{
perror("lstat error\n");
continue;
}
printf("%s\n",dirp->d_name);
}
printf("\n");
closedir(dp);
}
}
}
}
void wenjian_leixing()
{
/*biao shi wen jian lei xing*/
if(S_ISREG(statbuf.st_mode))
wjnx = '-';
else if(S_ISDIR(statbuf.st_mode))
wjnx = 'd';
else if(S_ISCHR(statbuf.st_mode))
wjnx = 'c';
else if(S_ISBLK(statbuf.st_mode))
wjnx = 'b';
else if(S_ISLNK(statbuf.st_mode))
wjnx = 'l';
}
void wenjian_quanxian()
{
/*biao shi wen jian de quan xian*/
if(statbuf.st_mode&S_IRUSR)
wjqx[0]='r';
else
wjqx[0]='-';
if(statbuf.st_mode&S_IWUSR)
wjqx[1]='w';
else
wjqx[1]='-';
if(statbuf.st_mode&S_IXUSR)
wjqx[2]='x';
else
wjqx[2]='-';
if(statbuf.st_mode&S_IRGRP)
wjqx[3]='r';
else
wjqx[3]='-';
if(statbuf.st_mode&S_IWGRP)
wjqx[4]='w';
else
wjqx[4]='-';
if(statbuf.st_mode&S_IXGRP)
wjqx[5]='x';
else
wjqx[5]='-';
if(statbuf.st_mode&S_IROTH)
wjqx[6]='r';
else
wjqx[6]='-';
if(statbuf.st_mode&S_IWOTH)
wjqx[7]='w';
else
wjqx[7]='-';
if(statbuf.st_mode&S_IXOTH)
wjqx[8]='x';
else
wjqx[8]='-';
}
void wenjian_xinxi()
{
struct tm *time;
lnum = statbuf.st_nlink;
size = statbuf.st_size;
usrinfo = getpwuid(statbuf.st_uid);
gpinfo = getgrgid(statbuf.st_gid);
lt = statbuf.st_mtime;
time = localtime(<);
strftime(str,50,"%b %d %R",time);
}
void shuchu_xinxi()
{
int j;
printf("%c",wjnx);
for(j = 0;j < 9;j++)
printf("%c",wjqx[j]);
printf(" %d",lnum);
if(usrinfo)
printf(" %s",usrinfo->pw_name);
if(gpinfo)
printf(" %s",gpinfo->gr_name);
printf("%13d ",size);
printf(str);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -