实现Linux下的ls和ls-l
ls的C语言代码实现
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <linux/limits.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#define PARAM_NONE 0 //无参数
#define PARAM_A 1 //-a
#define PARAM_L 2 //-l
#define MAXROWLEN 80 //一行最多显示的字符数
int g_leave_len = MAXROWLEN; //一行是剩余长度,用于输出对齐
int g_maxlen; //存放某目录下最长文件名的长度
void my_error(const char* errstring, int line)
{
fprintf(stderr,"line:%d",line);
perror(errstring);
exit();
}
//打印单个文件,且没有-l参数
void display_single(char *name)
{
int i,len;
//如果本行不足以打印一个文件名则换行
if(g_leave_len < g_maxlen)
{
printf("\n");
g_leave_len = MAXROWLEN;
}
len = strlen(name);
len = g_maxlen - len;
printf("%-s",name);
;i<len;i++)
{
printf(" ");
}
printf(" ");
g_leave_len = g_leave_len - g_maxlen - ;
}
/*获取文件属性并打印*/
void display_attribute(struct stat buf, char *name)
{
];
struct passwd *psd;
struct group *grp;
//获取文件类型
if(S_ISLNK(buf.st_mode))
printf(");
else if(S_ISREG(buf.st_mode))
printf("-");
else if(S_ISDIR(buf.st_mode))
printf("d");
else if(S_ISCHR(buf.st_mode))
printf("c");
else if(S_ISBLK(buf.st_mode))
printf("b");
else if(S_ISFIFO(buf.st_mode))
printf("f");
else if(S_ISSOCK(buf.st_mode))
printf("s");
//获取文件权限
if(buf.st_mode & S_IRUSR)
printf("r");
else
printf("-");
if(buf.st_mode & S_IWUSR)
printf("w");
else
printf("-");
if(buf.st_mode & S_IXUSR)
printf("x");
else
printf("-");
if(buf.st_mode & S_IRGRP)
printf("r");
else
printf("-");
if(buf.st_mode & S_IWGRP)
printf("w");
else
printf("-");
if(buf.st_mode & S_IXGRP)
printf("x");
else
printf("-");
if(buf.st_mode & S_IROTH)
printf("r");
else
printf("-");
if(buf.st_mode & S_IWOTH)
printf("w");
else
printf("-");
if(buf.st_mode & S_IXOTH)
printf("x");
else
printf("-");
printf(" ");
//根据uid和gid获取文件所有者的用户名于组名
psd = getpwuid(buf.st_uid);
grp = getgrgid(buf.st_gid);
printf("%4d",buf.st_nlink);
printf("%-8s",psd->pw_name);
printf("%-9s",grp->gr_name);
printf("%6d",buf.st_size);
strcpy(buf_time, ctime(&buf.st_mtime));//将格林位置时间转化成正常时间格式
buf_time[strlen(buf_time) - ] = ;
printf(" %s",buf_time);
}
//根据flag参数显示文件内容,调用display_single或者display_attribute
void display(int flag,char *pathname)
{
int i,j;
struct stat buf;
];
,j=;i<strlen(pathname);i++)
{
if(pathname[i] == '/')
{
j = ;
}
else
name[j++] = pathname[i];
}
name[j] = ;
)
{
my_error("stat",__LINE__);
}
if(flag == PARAM_NONE)
{
] != '.')//不显示隐藏文件
{
display_single(name);
}
}
else if(flag == PARAM_A)
{
display_single(name);
}
else if(flag == PARAM_L)
{
] != '.')
{
display_attribute(buf,name);
printf(" %-s\n",name);
}
}
else if(flag == (PARAM_A | PARAM_L))
{
display_attribute(buf,name);
printf(" %-s\n",name);
}
}
void display_dir(int flag_param,const char *path)
{
DIR* dir;
struct dirent* dirent;
][PATH_MAX+],temp[PATH_MAX+];
;//总共有多少个文件
if((dir = opendir(path)) == NULL)
{
my_error("opendir",__LINE__);
}
//获取文件总数和最长文件名
while((dirent = readdir(dir)) != NULL)
{
if(g_maxlen < strlen(dirent->d_name))
g_maxlen = strlen(dirent->d_name);
count++;
}
closedir(dir);
)
my_error("文件太多超过了256个",__LINE__);
int i,j,len = strlen(path);
//获取目录下所有的文件名
dir = opendir(path);
;i<count;i++)
{
dirent = readdir(dir);
if(dirent == NULL)
{
my_error("readdir",__LINE__);
}
strncpy(filenames[i],path,len);
filenames[i][len] = ;
strcat(filenames[i],dirent->d_name);
filenames[i][len+strlen(dirent->d_name)] = ;
}
//对文件名进行排序
;i<count-;i++)
;j<count-;j++)
{
)
{
strcpy(temp,filenames[j]);
strcpy(filenames[j] , filenames[i]);
strcpy(filenames[i] , temp);
}
}
;i<count;i++)
display(flag_param,filenames[i]);
closedir(dir);
//没有-l的话打印一个换行符
)
printf("\n");
}
int main(int argc, char **argv)
{
int i,j,k;
int num;//记录-的个数
];
]; // 保存命令行参数
int flag_param = PARAM_NONE;
struct stat buf;
j = ;
num = ;
;i<argc;i++)
{
] == '-')
{
;k<strlen(argv[i]);k++)
{
param[j] = argv[i][k];
j++;
}
num++;
}
}
//现在只支持-a和-l参数
;i<j;i++)
{
if(param[i] == 'a')
{
flag_param |= PARAM_A;
}
else if(param[i] == 'l')
{
flag_param |= PARAM_L;
}
else
{
printf("错误的参数:%c\n",param[i]);
exit();
}
}
param[j] = ;
//如果没有输入文件名或者目录,就显示当前目录
) == argc)
{
strcpy(path,"./");
path[] = ;
display_dir(flag_param,path);
;
}
i = ;
;i<argc;i++)
{
] != '-')
{
strcpy(path,argv[i]);
)
my_error("stat",__LINE__);
if(S_ISDIR(buf.st_mode))
{
//判断目录是否以/结尾
] != '/')
{
path[strlen(argv[i])] = '/';
path[strlen(argv[i] + )] = ;
}
else
path[strlen(argv[i])] = ;
display_dir(flag_param,path);
}
else
{
display(flag_param,path);
}
}
}
;
}
截图:

实现Linux下的ls和ls-l的更多相关文章
- Linux下ll命令与ls -l
还大三Linux课的债. 1.ll命令用于显示当前文件下非隐藏文件的详情 查询结果分为7栏: 1)如' -rw-r--r--' 表示三种用户对该文件的不同权限: r:可读:w:可写:x:可执行 其中第 ...
- linux 下用 c 实现 ls -l 命令
#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/sta ...
- linux下 解释 终端命令 ls -al或者ls -li 输出的信息
$ ls -al drwxr-xr-x. wjshan0808 wjshan0808 Sep : .cache $ ls -li ...
- linux下用c实现ls命令
struct dirent中的几个成员: d_type:4表示为目录,8表示为文件 d_reclen:16表示子目录或文件,24表示非子目录 d_name:目录或文件的名称 #include < ...
- Linux 下的基本命令
Linux 下的基本命令 1. ls 命令 格式 : ls [OPTION]... [FILE]... 用途 : 显示目录下的内容 [OPTION] : -l : 列出详细信息 -d : 显示目录本身 ...
- linux 下ln命令--笔记
linux 下ln命令 ln命令用来为文件创建连接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接.如果要创建符号连接必须使用"-s"选项.注意:符号链接文件不是一个独立 ...
- 实现Linux下的ls -l命令
基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...
- linux下出现+ ls --color=auto -l --color=auto...++ echo -ne '\033]0;root@imon-2:~'等
[root@imon-2 ~]# cd /root/ + cd /root/ ++ echo -ne '\033]0;root@imon-2:~' [root@imon-2 ~]# ll + ls - ...
- linux下ls -l命令(即ll命令)查看文件的显示结果分析
在linux下使用“ls -l”或者“ls -al”或者“ll”命令查看文件及目录详情时,shell中会显示出好几列的信息.平时也没怎么注意过,今天忽然心血来潮想了解一下,于是整理了这篇博客,以供参考 ...
- 控制台程序的中文输出乱码问题(export LC_CTYPE=zh_CN.GBK,或者修改/etc/sysconfig/i18n为zh_CN.GBK。使用setlocale(LC_CTYPE, "");会使用默认办法。编译器会将源码做转换成Unicode格式,或者指定gcc的输入文件的编码参数-finput-charset=GBK。Linux下应该用wprintf(L"%ls/n",wstr))
今天发现用securecrt登陆时,gcc编译出错时会出现乱码,但直接在主机的窗口界面下用Shell编译却没有乱码.查看了一下当时的错误描述,发现它的引号是中文引号,导致在SecureCRT中显示出错 ...
随机推荐
- 阿里云物联网平台体验(NetGadgeteer+C#篇)
目前对接阿里云物联网平台有多种软件和硬件,可以有多种不同语言来实现对接,比如c/c++,Java,JS,Python,C#等等,不过C#版本只有PC对接云平台的代码,嵌入式的目前还没有看到,所以本篇文 ...
- 使用h5py操作hdf5文件
HDF(Hierarchical Data Format)指一种为存储和处理大容量科学数据设计的文件格式及相应库文件.HDF 最早由美国国家超级计算应用中心 NCSA 开发,目前在非盈利组织 HDF ...
- Git diff结果显示分析
1.diff的三种格式: 正常格式(normal diff) 上下文格式(context diff) 合并格式(unified diff) 2.示例文件为了便于讲解,先新建两个示例文件.第一个文件叫做 ...
- IntelliJIdea 2016.2 使用 tomcat 8.5 调试spring的web项目时,bean被实例化两次导致timer和thread被启动了两遍的问题的解决
今天新搭建了一个spring的web项目,项目启动时会启动一个线程,线程里定时执行任务,另外还启动了一个定时器,每秒钟统计系统吞吐量等业务性能数据.但是调试的时候惊奇的发现定时器和线程均被启动了两次. ...
- James 如何作为服务在后台启动
james 启动后是在前台运行的,就像你跑一个微服务,前台运行显然不合理,关闭ssh后就会断开,所以我们得配置在后台,使用service配置即可 配置 james/bin 下的phoenix.sh,配 ...
- [elk]kafka_elk
kafka https://www.tutorialspoint.com/apache_kafka/apache_kafka_cluster_architecture.htm https://dzon ...
- Linux系统盘扩容-物理机非虚拟机
# Linux系统盘扩容 ## 了解磁盘分区情况 - `blkid` 查看分区情况- `fdisk -l` 分区表 ## 系统挂载分区 - `/etc/fstab` 启动挂载分区情况 ## 双系统下分 ...
- 【Unity】微信支付SDK官方安卓Demo的使用问题
Unity3d使用微信支付是属于APP内发起支付调用的情况,其本质上是在安卓项目上使用微信SDK,安卓项目开发完成后再导入到Unity中作为Unity插件使用,即Unity中C#调用安卓(Java)代 ...
- 【javascript】定时器组件
'use strict'; module.exports = function() { this.timer = {}; this.config = {}; // 初始化参数 this.init = ...
- MySQL中TEXT与BLOB字段类型的区别
这篇文章主要介绍了MySQL中TEXT与BLOB字段类型的区别,本文总结了6大区别,需要的朋友可以参考下 在MySQL中有两个字段类型容易让人感觉混淆,那就是TEXT与BLOB,特别是自己写博客程 ...