主要用到的函数可以参考头文件,仅仅支持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. Windows本机调试内部组件

    将详细分析Windows调试的本机接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全专家的参考 ...

  2. 使用singer tap-postgres 同步数据到pg

    singer 是一个很不错的开源etl 解决方案,以下演示一个简单的数据从pg 同步到pg 很简单就是使用tap-postgres + target-postgres 环境准备 对于测试的环境的数据库 ...

  3. shell脚本编程基础之case语句

    基础简介 脚本编程分为: 面向过程 选择结构:if语句,单分支.双分支.多分支:case语句 控制结构:顺序结构(默认) 循环结构:for.while.until 面向对象 case语句结构 case ...

  4. mysql 选择所有同学名字

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  5. 修复LSP 解决不能上网问题

    电脑突然不能上网,ping路由提示"传输失败,常见故障" 1, 打开CMD 2, 输入"netsh winsock reset" 回车 3, 重启电脑 LSP ...

  6. 团队作业-Alpha(2/4)

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

  7. _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed

    在Ubuntu18上使用交叉编译工具,报这个错.研究之下发现,工具的绝对路径过长,ubuntu18对其优化,修改路径,导致报错. 使用命令:export LC_ALL=C

  8. GIS地理工具案例教程——批量合并影像-批量镶嵌栅格

    GIS地理工具案例教程--批量合并影像-批量镶嵌栅格 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 关键词:批量.迭代.循环.自动.智能.地理 ...

  9. spring4.x企业应用开发读书笔记1

    第一章 概述 1 spring 以 ioc 和 aop 为内核,提供了展现层 springMVC.持久层SpringJDBC及业务层事务管理等一站式企业级应用技术. 2spring的特性 方便解耦,简 ...

  10. Python中的虚拟环境的使用

    1.安装virtualenv pip3 install virtualenv 2.创建目录 mkdir Myproject cd Myproject 3.创建独立运行环境-命名 virtualenv ...