高仿linux下的ls -l命令——C语言实现
主要用到的函数可以参考头文件,仅仅支持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语言实现的更多相关文章
- 实现Linux下的ls -l命令
基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...
- 模拟linux下的ls -l命令
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
- 编程实现Linux下的ls -l
头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...
- linux下ls -l命令(即ll命令)查看文件的显示结果分析
在linux下使用“ls -l”或者“ls -al”或者“ll”命令查看文件及目录详情时,shell中会显示出好几列的信息.平时也没怎么注意过,今天忽然心血来潮想了解一下,于是整理了这篇博客,以供参考 ...
- linux下如何使用sftp命令【转】
linux下如何使用sftp命令 from: http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...
- linux下find和grep命令详解
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...
- 终端的乐趣--Linux下有趣的终端命令或者工具【转】
转自:https://blog.csdn.net/gatieme/article/details/52144603 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...
- Linux下的一些常用命令(一)
在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...
- 实现linux下的ls
实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...
随机推荐
- QML学习(四)——<Text显示>
文本显示是界面开发必不可少的内容,在Qt Quick模块中提供了 Text 项目来进行文本的显示,其中可以使用 font 属性组对文本字体进行设置.这一篇我们来看看它们的具体使用. 使用字体 就像前面 ...
- Fluent也弹窗
具体步骤见<fluent加载第三方(C++,Fortran等)动态链接库> 我们对导入的动态链接库进行改动 打开VS2013 源代码: #include #ifdef __cplusplu ...
- 解决PHP7无法监听9000端口问题/502错误解决办法
问题背景 昨晚帮配置nginx+php服务的时候,发生了一个奇怪的事情netstat -anp|grep 9000查看9000端口,一直没有监听,于是nginx无法通过fastcgi来代理php请求. ...
- Jmeter(四十三)_性能测试分配堆内存
内存泄漏.内存溢出是什么? 内存泄露是指你的应用使用资源之后没有及时释放,导致应用内存中持有了不需要的资源,这是一种状态描述: 内存溢出是指你应用的内存已经不能满足正常使用了,堆栈已经达到系统设置的最 ...
- ubuntu之路——day13 只用python的numpy在较为底层的阶段实现单隐含层神经网络
首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...
- Nginx location wildcard
Module ngx_http_core_modulehttps://nginx.org/en/docs/http/ngx_http_core_module.html#location locatio ...
- tomcat请求响应代码分享
NioEndpoint的Poller轮询器持续进行扫描是否有新的event()方法调用后产生新的event配置. 发现后执行AbstractProtocol.class中的process()方法进行处 ...
- 【翻译】Flink Table Api & SQL — SQL
本文翻译自官网:SQL https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/sql.html Flink Tab ...
- [LeetCode] 21. Merge Two Sorted Lists 合并有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- sql多列值一行显示
select stuff(( select ',' + UserNM from tblSysUser for xml path('')), 1,1,'')