主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0

相关测试图片:

编译 gcc -std=c99 ls_l.c -o ls

运行 ./ls -l

( 请勿在文件结构复杂的目录下执行,程序会挂的!)

话不多说,直接上码

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <dirent.h> #define MAX_FILE_NUM 200 //可能还有一些小问题没有解决,功能基本已经实现,如有建议,望大佬赐教 typedef struct LS
{
char mode[]; // 文件的模式
int dir_num; // 是否目录或目录中包含目录的数量
char user[]; // 文件的用户名
char group[]; // 文件的组名
long size; // 文件的字节数
char time[]; // 文件的最后修改时间
char year[]; // 拓展用,年份
char mon[]; // 月份
char hour[]; // 时
char min[]; // 分
int st_mode; // 文件类型和权限
char name[]; // 文件名
}LS; // 获取文件的模式
char* file_mode(mode_t m,char* str)
{
if(S_ISREG(m))
str[] = '-';
else if(S_ISDIR(m))
str[] = 'd';
else if(S_ISCHR(m))
str[] = 'c';
else if(S_ISBLK(m))
str[] = 'b';
else if(S_ISFIFO(m))
str[] = 'q';
else if(S_ISLNK(m))
str[] = 'l';
// else if(S_ISSOCK(m))
// str[0] = 's';
else
str[] = '?'; str[] = '\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 dir_count(char* path)
{
DIR *dir;
dir = opendir(path);
struct dirent *dirent;
int count = ;
while((dirent = readdir(dir)) != NULL)
{
if(dirent->d_type == )
count++;
}
closedir(dir);
return count;
} // 是否是目录或目录下有目录
int is_dir(struct dirent *dirent)
{
char* a = dirent->d_name;
if(dirent->d_type == )
return ;
if(dirent->d_type == )
{
if(dir_count(a) == )
return ;
else
return dir_count(a);
}
} // 获取用户名
char* file_user(uid_t st_uid,char* str)
{
struct passwd *user;
user = getpwuid(st_uid);
sprintf(str,"%s",user->pw_name);
return str;
} // 获取组名
char* file_group(uid_t st_uid,char* str)
{
struct passwd *user;
user = getpwuid(st_uid);
struct group *grp;
grp = getgrgid(user->pw_gid);
sprintf(str,"%s",grp->gr_name);
return str;
} // 获取文件大小
off_t file_size(struct stat buf)
{
off_t size = buf.st_size;
return size;
} // 获取最后修改时间
char* file_time(time_t mt,char* str)
{
struct tm* t = localtime(&mt);
sprintf(str,"%d月 %02d %02d:%02d",t->tm_mon+,t->tm_mday,t->tm_hour,t->tm_min);
return str;
} // 获取文件的数量
int file_count(char* path)
{
DIR *dir;
dir = opendir(path);
struct dirent *dirent;
int count = ;
while((dirent = readdir(dir)) != NULL)
{
count++;
}
closedir(dir);
return count;
} // 交换
void equal(LS *a,LS *b)
{
strcpy(a->mode,b->mode);
a->dir_num = b->dir_num;
strcpy(a->user,b->user);
strcpy(a->group,b->group);
a->size = b->size;
strcpy(a->time,b->time);
a->st_mode = b->st_mode;
strcpy(a->name,b->name);
} // 排序
void sort(LS *info,int index)
{
LS *temp = (LS*)malloc(sizeof(LS));
for(int i=index-; i>; i--)
{
for(int j=; j<i; j++)
{
if(strcmp(info[i].name,info[j].name)<)
{
equal(temp,&info[i]);
equal(&info[i],&info[j]);
equal(&info[j],temp);
}
}
}
} // 输出结构体
void show_ls(LS *info,int index)
{
for(int i=; i<index; i++)
{
//printf("%d: ",i);
printf("%s \033[0m",info[i].mode);
printf("%d ",info[i].dir_num);
printf("%s ",info[i].user);
printf("%s ",info[i].group);
printf("%5ld ",info[i].size);
printf(" %s ",info[i].time);
//printf("%d ",info[i].st_mode);
if( == info[i].st_mode)
{
// 颜色
printf("\033[34m\033[1m%s\033[0m",info[i].name);
}
else if( == info[i].st_mode)
{
printf("\033[32m\033[1m%s\033[0m",info[i].name);
}
else
{
printf("%s",info[i].name);
}
if(i < index)
printf("\n");
}
//printf("循环结束\n");
} // 创建结构体,赋值
LS *create(struct stat buf,struct dirent *dirent)
{
LS* info = (LS*)malloc(sizeof(LS));
char str[] = {};
//puts(file_mode(buf.st_mode,str));
strcpy(info->mode,file_mode(buf.st_mode,str));
//puts(info->mode);
info->dir_num = is_dir(dirent);
strcpy(info->user,file_user(buf.st_uid,str));
strcpy(info->group,file_group(buf.st_uid,str));
info->size = file_size(buf);
strcpy(info->time,file_time(buf.st_mtime,str));
info->st_mode = buf.st_mode;
strcpy(info->name,dirent->d_name); return info;
} int main(int argc,char* argv[])
{
LS info[MAX_FILE_NUM];
char* l = "-l";
if(argc != )
{
printf("仅支持传入 -l\n");
return ;
}
if(strcmp(argv[],l) != )
{
printf("\"ls:无法识别的选项\"%s\"\n",argv[]);
printf("请尝试执行\"ls --help\"来获取更多信息。\n");
return ;
}
char* a = ".";
char* b = "..";
char* path = malloc();
strcpy(path,"./"); // 只支持当前路径
int count = file_count(path); DIR *dir;
dir = opendir(path);
struct dirent *dirent;
int index = ; // 结构体下标
int blocks = ;
for(int i=; i<count; i++)
{
dirent = readdir(dir);
struct stat buf = {};
if(stat(dirent->d_name,&buf))
{
perror("stat");
return -;
} // 跳过特殊情况
if(strcmp(dirent->d_name,a)== || strcmp(dirent->d_name,b)==)
continue;
blocks += buf.st_blocks;
//printf("%d\n",blocks);
info[index++] = *create(buf,dirent);
}
closedir(dir);
//printf("文件总数:%d\n",index);
//show_ls(info,index); printf("总用量 %d\n",blocks/);
sort(info,index);
show_ls(info,index);
return ;
}

高仿linux下的ls -l命令——C语言实现的更多相关文章

  1. 实现Linux下的ls -l命令

    基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...

  2. 模拟linux下的ls -l命令

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  3. 编程实现Linux下的ls -l

    头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...

  4. linux下ls -l命令(即ll命令)查看文件的显示结果分析

    在linux下使用“ls -l”或者“ls -al”或者“ll”命令查看文件及目录详情时,shell中会显示出好几列的信息.平时也没怎么注意过,今天忽然心血来潮想了解一下,于是整理了这篇博客,以供参考 ...

  5. linux下如何使用sftp命令【转】

    linux下如何使用sftp命令 from:   http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...

  6. linux下find和grep命令详解

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...

  7. 终端的乐趣--Linux下有趣的终端命令或者工具【转】

    转自:https://blog.csdn.net/gatieme/article/details/52144603 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  8. Linux下的一些常用命令(一)

    在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...

  9. 实现linux下的ls

    实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...

随机推荐

  1. CSS文本元素

    一.属性 font-size:16px;  文字大小 Font-weight: 700 ;   值从100-900,文字粗细,不推荐使用font-weight:bold; Font-family:微软 ...

  2. Postgresql 时间串转换格式

    1. 时间:02-AUG-18 17:01:34 转成正常 年月日 时分秒 select to_char(to_timestamp('02-AUG-18 17:01:34', 'dd-mon-yy,h ...

  3. Codeforces 1163E Magical Permutation [线性基,构造]

    codeforces 思路 我顺着图论的标签点进去的,却没想到-- 可以发现排列内每一个数都是集合里的数异或出来的. 考虑答案的上界是多少.如果能用小于\(2^k\)的数构造出\([0,2^k-1]\ ...

  4. kafka-python 1.4.6 版本触发的一个 rebalance 问题

    在使用了最新版的 kafka-python 1.4.6 在 broker 对 topic 进行默认配置的情况下报出类似错误 CommitFailedError CommitFailedError: C ...

  5. Uncaught SyntaxError: Unexpected token o

    浏览器报Uncaught SyntaxError: Unexpected token o 这原因是你ajax获取数据的时候对数据进行错误操作,比如使用了 JSON.parse(data) 对数据进行转 ...

  6. Java使用Jsoup之爬取博客数据应用实例

    导入Maven依赖 <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <g ...

  7. 剑指offer: 求1+2+...+n

    题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 思路分析: 由于题目的限制条件很多.同样想到 ...

  8. Qt源码学习之路(2) QCoreApplication(1)

    QCoreApplication最重要的函数便是exec(),我们便从这个函数开始分析QCoreApplication都干了什么. 先列出exec()函数的源码 static int exec();/ ...

  9. Xamarin图表开发基础教程(7)OxyPlot框架

    Xamarin图表开发基础教程(7)OxyPlot框架 Xamarin.Forms中使用OxyPlot框架 在Xamarin. Forms平台上实现图表显示需要完成以下的步骤: 1.添加OxyPlot ...

  10. vue---自定义指令的使用

    在vue开发项目中,指令的使用场景也是比较多的,那么该如何定义使用呢? 找到 src / directive 下新建 gender 目录,下面新建 index.js 和 gender.js index ...