Linux---Ls命令 初级实现
By xxx0624
Done:
ls
ls -a
ls -l
ls /tmp
ls -R
ls -t
FileName color
FileName output
/*
By xxx0624
Done:
ls
ls -a
ls -l
ls /tmp
ls -R
ls -t
FileName color
FileName output
*/ #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ioctl.h>
#include<dirent.h>
#include<pwd.h>
#include<grp.h>
#include<unistd.h>
/*head file*/ #define LenOfName 256
#define maxN 1005
#define maxM 505
#define maxL 105
#define LenOfPath 256<<4
#define LS 0 //ls
#define LS_A 1 //ls -a
#define LS_L 2 //ls -l
#define LS_TMP 3 //ls /tmp
#define LS_R 4 //ls -R
#define LS_T 5 //ls -t
/*define file*/ void do_ls( int,char [] );
void dostat( char * );
void show_file_info( char *,struct stat * );
void mode_to_letters( int,char [] );
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
void getcolor( char * );
int get_file_type( char * );
int get_modify_time( char * );
void getWidth();
void display_R( char * );
int cmp1( const void * ,const void * );
int cmp2( const void * ,const void * );
int cmp3( const void * ,const void * ); struct outputFile{
char FileName[ LenOfName ];
int modify_time ;
int file_type ;
}Output[ maxN ],OutputPoint[ maxM ],Temp[ maxN+maxM ];
int colormode,foreground,background;
int terminalWidth ; void dostat( char *filename ){
struct stat info;
if( stat( filename,&info )==- ){
perror( filename );
}
else{
getcolor( filename );
show_file_info( filename,&info );
}
return ;
}
/*get file info*/ void mode_to_letters( int mode,char str[] ){
strcpy( str,"----------" ); if( S_ISDIR( mode ) ) str[] = 'd';
if( S_ISCHR( mode ) ) str[] = 'c';
if( S_ISBLK( mode ) ) str[] = 'b'; if( mode&S_IRUSR ) str[] = 'r';
if( mode&S_IWUSR ) str[] = 'w';
if( mode&S_IXUSR ) str[] = 'x'; if( mode&S_IRGRP ) str[] = 'r';
if( mode&S_IWGRP ) str[] = 'w';
if( mode&S_IXGRP ) str[] = 'x'; if( mode&S_IROTH ) str[] = 'r';
if( mode&S_IWOTH ) str[] = 'w';
if( mode&S_IXOTH ) str[] = 'x'; return ;
} char *uid_to_name( uid_t uid ){
struct passwd *pw_ptr;
static char numstr[ ];
if( (pw_ptr = getpwuid( uid ) )==NULL ){
sprintf(numstr,"%d",uid);
return numstr;
}
else{
return pw_ptr->pw_name;
}
}
char *gid_to_name( gid_t gid ){
struct group *grp_ptr;
static char numstr[ ];
if( (grp_ptr = getgrgid( gid ) )==NULL ){
sprintf(numstr,"%d",gid);
return numstr;
}
else{
return grp_ptr->gr_name;
}
}
/*get userid & groupid*/ int get_file_type( char *filename ){
struct stat info;
stat( filename,&info );
int file_type = ;
file_type = info.st_mode & S_IFMT;
return file_type;
}
/*get file type*/ int get_modify_time( char *filename ){
struct stat info;
stat( filename,&info );
int modify_time = ;
modify_time = info.st_mtime;
return modify_time;
}
/*get file last modify time*/ int isadir(char *str)
{
struct stat info; return ( lstat(str,&info) != - && S_ISDIR(info.st_mode) );
}
/*judge the file is or not a dir*/ void display_R( char *filename ){
char fullpath[ LenOfPath ];
struct outputFile Output2[ maxN ];
getcolor( filename );
printf("\033[%d;%d;%dm%s\033[0m :\n",colormode,foreground,background,filename);
DIR * dir_ptr ;
struct dirent *direntp;
int cntTemp = ;
int i ;
if( (dir_ptr = opendir( filename ) )==NULL ){
fprintf( stderr,"myls:cannot open %s\n",filename );
closedir( dir_ptr );
return ;
}
else {
while( (direntp = readdir( dir_ptr ))!=NULL ){
if( strcmp( direntp->d_name,"." )==||strcmp( direntp->d_name,".." )== )
continue;
strcpy( Output2[ cntTemp ].FileName,direntp->d_name );
strcpy( Output[ cntTemp ].FileName,direntp->d_name );
cntTemp ++ ;
}
closedir( dir_ptr );
if( cntTemp> ){
qsort( Output2,cntTemp,sizeof( Output2[] ),cmp1 );
qsort( Output,cntTemp,sizeof( Output[] ),cmp1 );
}
display_Ls( cntTemp );
for( i=;i<cntTemp;i++ ){
memset( fullpath,'\0',sizeof( fullpath ) );
strcpy( fullpath,filename );
strcat( fullpath,"/" );
strcat( fullpath,Output2[ i ].FileName );
if( isadir( fullpath ) )
display_R( fullpath );
}
}
return ;
}
/*active ls -R && Output */ void getWidth(){
char *tp;
struct winsize wbuf;
terminalWidth = ;
if( isatty(STDOUT_FILENO) ){
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &wbuf) == - || wbuf.ws_col == ){
if( tp = getenv("COLUMNS") )
terminalWidth = atoi( tp );
}
else
terminalWidth = wbuf.ws_col;
}
return ;
}
/******************************************************************
得出终端的宽度,默认宽度为80,如果获取失败,将激活-1选项
*******************************************************************/ int cmp1( const void *p ,const void *q ){
char T1[ LenOfName ],T2[ LenOfName ];
strcpy( T1,(*(struct outputFile *)p).FileName );
strcpy( T2,(*(struct outputFile *)q).FileName );
int len1 = strlen( T1 );
int i ;
for( i=;i<len1;i++ ){
if( T1[ i ]>='A' && T1[ i ]<='Z' ){
T1[ i ] = T1[ i ] - 'A' + 'a';
}
}
int len2 = strlen( T2 );
for( i=;i<len2;i++ ){
if( T2[ i ]>='A' && T2[ i ]<='Z' ){
T2[ i ] = T2[ i ] - 'A' + 'a';
}
}
return strcmp( T1,T2 );
}
/********************************************************
文件名排序 cmp1
*********************************************************/ int cmp2( const void *p,const void *q ){
return (*(struct outputFile *)p).file_type < (*(struct outputFile *)q).file_type ;
}
/********************************************************
文件类型排序 cmp2
*********************************************************/ int cmp3( const void *p,const void *q ){
return (*(struct outputFile *)p).modify_time < (*(struct outputFile *)q).modify_time ;
}
/********************************************************
文件修改时间排序 cmp3
*********************************************************/ void show_file_info( char *filename,struct stat * info_p ){
char modestr[ ]; mode_to_letters( info_p->st_mode,modestr ); printf("%s",modestr);
printf("%4d ",(int)info_p->st_nlink);
printf("%-8s ",uid_to_name(info_p->st_uid));
printf("%-8s ",gid_to_name(info_p->st_gid));
printf("%8ld ",(long)info_p->st_size);
printf("%.12s ",+ctime(&info_p->st_mtime));
printf("\033[%d;%d;%dm%s\033[0m\n",colormode,foreground,background,filename);
return ;
}
/*******************************************************
ls -l 的输出
********************************************************/ void getcolor( char *filename ){
struct stat info;
stat( filename,&info );
foreground = ;
background = ;
colormode = ;
switch ( (info.st_mode & S_IFMT) ){
case S_IFREG: /*regular 普通文件 , 白色*/
foreground = ;
break;
case S_IFLNK: /*symbolic link 链接文件 , 青蓝色*/
foreground = ;
colormode = ;
break;
case S_IFSOCK: /*紫红色*/
foreground = ;
colormode = ;
break;
case S_IFDIR: /*directory 目录文件 , 蓝色*/
foreground = ;
break;
case S_IFBLK: /*block special 块设备文件 , 黄色*/
foreground = ;
colormode = ;
break;
case S_IFCHR: /*character special 字符设备文件 , 黄色*/
foreground = ;
colormode = ;
break;
case S_IFIFO: /*fifo 绿色*/
foreground = ;
colormode = ;
break;
}
}
/*******************************************************
给文件添加颜色
********************************************************/ void display_Ls( int cnt ){
int wordLenMax = ;//the LenMax word
int wordRowNum = ;//the amount of one row
int wordColNum = ;//the amount of one col
int i , j;
for( i=;i<cnt;i++ ){
if( i== ) wordLenMax = strlen( Output[ i ].FileName );
else wordLenMax = wordLenMax>strlen( Output[ i ].FileName )?wordLenMax:strlen( Output[ i ].FileName );
}
wordLenMax += ;
wordRowNum = terminalWidth / wordLenMax;
if( cnt%wordRowNum== ) wordColNum = cnt / wordRowNum;
else wordColNum = cnt / wordRowNum + ;
for( i=;i<wordColNum;i++ ){
j = i;
while( j<cnt ){
getcolor( Output[ j ].FileName );
printf("\033[%d;%d;%dm%-15s\033[0m ",colormode,foreground,background,Output[ j ].FileName);
j += wordColNum;
}
printf("\n");
}
return ;
}
/**********************************************************
ls的分栏输出
***********************************************************/ void display_Ls_a( int cntPoint,int cnt ){
int CNT = ;
int wordLenMax = ;//the LenMax word
int wordRowNum = ;//the amount of one row
int wordColNum = ;//the amount of one col
int i , j;
for( i=;i<cntPoint;i++ ){
strcpy( Temp[ CNT ].FileName,OutputPoint[ i ].FileName );
Temp[ CNT ].file_type = OutputPoint[ i ].file_type;
Temp[ CNT ].modify_time = OutputPoint[ i ].modify_time;
CNT ++;
wordLenMax = wordLenMax>strlen( OutputPoint[ i ].FileName )?wordLenMax:strlen( OutputPoint[ i ].FileName );
}
for( i=;i<cnt;i++ ){
strcpy( Temp[ CNT ].FileName,Output[ i ].FileName );
Temp[ CNT ].file_type = Output[ i ].file_type;
Temp[ CNT ].modify_time = Output[ i ].modify_time;
CNT ++;
wordLenMax = wordLenMax>strlen( Output[ i ].FileName )?wordLenMax:strlen( Output[ i ].FileName );
}
wordLenMax += ;
wordRowNum = terminalWidth / wordLenMax;
if( CNT%wordRowNum== ) wordColNum = CNT / wordRowNum;
else wordColNum = CNT / wordRowNum + ;
for( i=;i<wordColNum;i++ ){
j = i;
while( j<CNT ){
getcolor( Temp[ j ].FileName );
printf("\033[%d;%d;%dm%-15s\033[0m ",colormode,foreground,background,Temp[ j ].FileName);
j += wordColNum;
}
printf("\n");
}
return ;
}
/**********************************************************
ls -a 的分栏输出
***********************************************************/ void display_Ls_tmp( int cnt ){
display_Ls( cnt );
return ;
}
/**********************************************************
ls /tmp 的分栏输出
***********************************************************/ void display_Ls_R( char *filename ){
char fullpath[ LenOfPath ];
strcpy( fullpath,get_current_dir_name() );
strcat( fullpath,"/" );
strcat( fullpath,filename );
display_R( fullpath );
return ;
}
/**********************************************************
ls -R 的分栏输出
***********************************************************/ void do_ls( int myJudge,char myOrder[] ){
if( myJudge==LS_R ){
display_Ls_R( "." );
return ;
}
/**********************************************************
ls -R
***********************************************************/
char dirname[ maxL ];
if( myJudge!=LS_TMP ){
strcpy( dirname,"." );
}
else {
strcpy( dirname,myOrder );
}
DIR * dir_ptr;
struct dirent *direntp;
int cntOutput = ;
int cntOutputPoint = ;
if( ( dir_ptr = opendir( dirname ) )==NULL ){
fprintf( stderr,"myls:cannot open %s\n",dirname );
}
else{
while( (direntp = readdir( dir_ptr ) )!=NULL ){
if( direntp->d_name[ ]=='.' ) {
strcpy( OutputPoint[ cntOutputPoint ].FileName,direntp->d_name );
OutputPoint[ cntOutputPoint ].file_type = get_file_type( OutputPoint[ cntOutputPoint ].FileName );
OutputPoint[ cntOutputPoint ].modify_time = get_modify_time( OutputPoint[ cntOutputPoint ].FileName );
cntOutputPoint ++;
}
else {
strcpy( Output[ cntOutput ].FileName,direntp->d_name );
Output[ cntOutput ].file_type = get_file_type( Output[ cntOutput ].FileName );
Output[ cntOutput ].modify_time = get_modify_time( Output[ cntOutput ].FileName );
cntOutput ++;
}
} if( myJudge==LS_T ){
qsort( OutputPoint,cntOutputPoint,sizeof( OutputPoint[] ),cmp3 );
qsort( Output,cntOutput,sizeof( Output[] ),cmp3 );
}
else {
qsort( OutputPoint,cntOutputPoint,sizeof( OutputPoint[] ),cmp1 );
qsort( Output,cntOutput,sizeof( Output[] ),cmp1 );
}
/**********************************************************
预处理输出
***********************************************************/
if( myJudge==LS||myJudge==LS_T ){
display_Ls( cntOutput );
closedir( dir_ptr );
}
/**********************************************************
ls && ls -t
***********************************************************/
else if( myJudge==LS_A ){
display_Ls_a( cntOutputPoint,cntOutput );
closedir( dir_ptr );
}
/**********************************************************
ls -a
***********************************************************/
else if( myJudge==LS_L ){
int i;
for( i=;i<cntOutput;i++ )
dostat( Output[ i ].FileName );
closedir( dir_ptr );
}
/**********************************************************
ls -l
***********************************************************/
else {
display_Ls_tmp( cntOutput );
closedir( dir_ptr );
}
/**********************************************************
ls /tmp
***********************************************************/
}
return ;
}/*sovle*/ int main( int argc, char *argv[] ){
getWidth( );
int i ;
if( argc== ){
do_ls( LS,"ls" );
}
else{
int ord;
while( (ord = getopt(argc,argv,":laRt"))!=- ){
switch( ord ){
case 'a':
do_ls( LS_A,"ls-a" );
break;
case 'l':
do_ls( LS_L,"ls-l" );
break;
case 'R':
do_ls( LS_R,"ls-R" );
break;
case 't':
do_ls( LS_T,"ls-t" );
break;
default :
break;
}
}
for( i=;i<argc;i++ ){
if( argv[ i ][ ]=='-' ) continue;
printf("%s:\n",argv[ i ]);
do_ls( LS_TMP,argv[ i ] );
}
}
return ;
}
/*main*/
Linux---Ls命令 初级实现的更多相关文章
- linux ls命令教程,ls命令怎么用,全部招数都教你
linux ls命令的用法大全 学习linux这么久了,最常用的命令莫属 ls命令了,今天就总结下ls命令的用法与经验技巧. ls命令按文件大小查看文件 a.降序:ls -lsh moudae ...
- Linux ls命令详解
ls 命令可以说是Linux下最常用的命令之一. -a 列出目录下的所有文件,包括以 . 开头的隐含文件.(后有详解)-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在c语言里一样)的形式列出. ...
- linux ls 命令
ls 命令是 Linux 下最常用的命令之一,用来查询目录下的内容(list directory contents).本文将介绍其基本的用法和一些典型的用例.笔者使用的测试环境为 ubuntu 16. ...
- Linux ls命令
ls:即列表List的意思,用来列出目录下的文件用来列出给定目录下的文件,参数为空默认列出当前目录下的文件. 用法是:ls [选项] [目录] 常用的选项有 -a, –all 列出目录下的所有文件,包 ...
- linux ls命令按时间显示文件
本文介绍下,使用ls命令显示文件,并按时间排序的方法,供大家学习参考. 在linux系统中,使用ls命令按时间排序文件,其实很简单,如下: #ls -tr 即可按时间排序当前目录下的文件. 附,l ...
- Linux ls命令参数详解
-a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录. -l -- 长(long).列举目 ...
- Linux ls命令参数详解 <转>
下面是一个与 ls 一起使用的一些常用选项的简短列表.请记住,你可以通过阅读 ls 的说明书页(man ls)来获得选项的完整列表. -a – 全部(all).列举目录中的全部文件,包括隐藏文件(.f ...
- Linux ls 命令实现(简化版)
在学习linux系统编程的时候,实现了ls命令的简化版本号. 实现的功能例如以下: 1. 每种文件类型有自己的颜色 (- 普通文件, d 文件夹文件, l 链接文件. c 字符设备文件. b 快设备文 ...
- 你应该了解的 7个Linux ls 命令技巧
在前面我们系列报道的两篇文章中,我们已经涵盖了关于‘ls’命令的绝大多数内容.本文时‘ls命令’系列的最后一部分.如果你还没有读过该系列的其它两篇文章,你可以访问下面的链接. 15 个‘ls’命令的面 ...
- Linux ls命令详解-乾颐堂CCIE
ls命令用法举例: 例一:列出/home文件夹下的所有文件和目录的详细资料: 1 ls -l -R /home 命令参数之前要有一短横线“-”, 上面的命令也可以这样写: 1 ls -lR /ho ...
随机推荐
- XPath 初步讲解
2016-05-05 XPath是JavaScript 中节点查找手段,ie9以后的版本才支持w3c标准,其他浏览器基本支持.在e8之前的浏览器,通过基于 activeX的xml dom对象实现. 为 ...
- [Bootstrap]全局样式(三)
表格 1.基本类 .table {width/margin-bottom/} {padding/border-top} e.g.:<table class="table" ...
- JavaScript的语法规则
JavaScript的语法规则 JavaScript区分大小写 JavaScript脚本程序须嵌入在HTML文件中 JavaScript脚本程序中不能包含HTML标记代码 每行写一条脚本语句 语句末尾 ...
- xml的生成与解析_老师笔记
使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 Fil ...
- spring读取prperties配置文件(2)
接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件. 这里我先定义好属性文件"user.propert ...
- 完美高仿精仿京东商城手机客户端android版源码
完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...
- js 中的正则表达式
一:正则表达式 定义:记录文本规则的代码 作用:表单验证,爬虫技术,可以对目标的内容进行替换. 二:正则表达式的组成 1:普通字符组成正则 浏览器的输出 2:定义字符集组成正则 3:特殊字符集组成正则 ...
- DirectSound学习(二)--流式缓冲区
使用流式缓冲方式播放波形音频文件比较复杂,主要原因是在只有一个缓冲区提供给用户的前提下,这个缓冲区在提供给声卡播放数据的同是还需要用户不断的定时向其中写入数据.要注意从缓冲区这时是一个环形缓冲区,声音 ...
- HTML邮件制作规范
以下内容有些是别人总结的,有些是自己在工作中总结的. 模板最佳尺寸:显示宽度550px-750px,模板高度控制在一屏以内. 1. 用table+css方式构建模板 Div+css布局不完全被邮件客户 ...
- RequireJS的简单应用
一.RequireJS的主要作用与优点 主要作用:js模块化.编写复用js代码 优点: 1.防止命名冲突 2.声明不同js文件之间的依赖 3.代码模块化 (1)一个文件一个模块:每个js文件应该只定义 ...