主要用到的函数可以参考头文件,仅仅支持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. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  2. CDH CM版本 6.0.1 升级到 CM 6.2.0 当前最新版本(CentOS 7.x)

    CDH 的 6.0.1 是一个尴尬的版本,那时候 cloudera 还没有将 spark 更新到 2.4 还使用的是 spark 2.2版本. 但后来我们发现 2.3 | 2.4 更新了非常多的 fe ...

  3. JS filter的使用

    定义和用法 filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() 不会改变原始数组 ...

  4. 两个对象值相同 (x.equals(y) == true) ,但却可有不同的 hash code ,这 句话对不对

    答:不对,有相同的 hash code这是java语言的定义:1) 对象相等则hashCode一定相等:2) hashCode相等对象未必相等 1.如果是基本变量,没有hashcode和equals方 ...

  5. photoshop 的安装破解

    最近学习需要用到photoshop,但是photoshop试用期只有30天,于是尝试破解photoshop.参考了网上的很多博客,失败了好几次,终于找到一篇靠谱的博客,很顺利的成功了.在这里记录一下, ...

  6. 第09组 Beta冲刺(3/4)

    队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...

  7. Unity初学者必备5款资源插件

    Unity技术经理Sam Dogantimur为Unity初学者精心挑选和推荐了5款入门必备资源插件,涵盖游戏开发原型创作.着色器特效以及角色控制等,帮助Unity新手快速上手游戏开发. 我们将详细介 ...

  8. 布局优化: <include />、<merge /> 、<ViewStub /> 标签的使用

    在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...

  9. (八)Knockout 组件 Components

    概述 :组件和自定义元素 Components 是将UI代码组织成自包含的.可重用的块的一种强大而干净的方法.他们: -可以表示单个控件/窗口小部件或应用程序的整个部分 -包含它们自己的视图,并且通常 ...

  10. zk集群部署

    一.环境准备 当前环境:centos7.3三台软件版本:zookeeper-3.5.2部署目录:/usr/local/zookeeper启动端口:2181配置文件:/usr/local/zookeep ...