模拟linux下的ls -l命令
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h> #define ERR_EXIT(m) \
do { \
perror(m);\
exit(EXIT_FAILURE);\
}while(0) const char *statbuf_get_perms(struct stat *sbuf);
const char *statbuf_get_date(struct stat *sbuf);
const char *statbuf_get_filename(struct stat *sbuf, const char *name);
const char *statbuf_get_user_info(struct stat *sbuf);
const char *statbuf_get_size(struct stat *sbuf); int main(int argc, const char *argv[])
{
DIR *dir = opendir(".");
if(dir == NULL)
ERR_EXIT("opendir"); struct dirent *dr;
while((dr = readdir(dir)))
{
const char *filename = dr->d_name;
if(filename[0] == '.')
continue; char buf[1024] = {0};
struct stat sbuf;
if(lstat(filename, &sbuf) == -1)
ERR_EXIT("lstat"); strcpy(buf, statbuf_get_perms(&sbuf));
strcat(buf, " ");
strcat(buf, statbuf_get_user_info(&sbuf));
strcat(buf, " ");
strcat(buf, statbuf_get_size(&sbuf));
strcat(buf, " ");
strcat(buf, statbuf_get_date(&sbuf));
strcat(buf, " ");
strcat(buf, statbuf_get_filename(&sbuf, filename)); printf("%s\n", buf);
} closedir(dir);
return 0;
} const char *statbuf_get_perms(struct stat *sbuf)
{
static char perms[] = "----------";
mode_t mode = sbuf->st_mode; //文件类型
switch(mode & S_IFMT)
{
case S_IFSOCK:
perms[0] = 's';
break;
case S_IFLNK:
perms[0] = 'l';
break;
case S_IFREG:
perms[0] = '-';
break;
case S_IFBLK:
perms[0] = 'b';
break;
case S_IFDIR:
perms[0] = 'd';
break;
case S_IFCHR:
perms[0] = 'c';
break;
case S_IFIFO:
perms[0] = 'p';
break;
} //权限
if(mode & S_IRUSR)
perms[1] = 'r';
if(mode & S_IWUSR)
perms[2] = 'w';
if(mode & S_IXUSR)
perms[3] = 'x';
if(mode & S_IRGRP)
perms[4] = 'r';
if(mode & S_IWGRP)
perms[5] = 'w';
if(mode & S_IXGRP)
perms[6] = 'x';
if(mode & S_IROTH)
perms[7] = 'r';
if(mode & S_IWOTH)
perms[8] = 'w';
if(mode & S_IXOTH)
perms[9] = 'x'; if(mode & S_ISUID)
perms[3] = (perms[3] == 'x') ? 's' : 'S';
if(mode & S_ISGID)
perms[6] = (perms[6] == 'x') ? 's' : 'S';
if(mode & S_ISVTX)
perms[9] = (perms[9] == 'x') ? 't' : 'T'; return perms;
} const char *statbuf_get_date(struct stat *sbuf)
{
static char datebuf[1024] = {0};
struct tm *ptm;
time_t ct = sbuf->st_ctime;
if((ptm = localtime(&ct)) == NULL)
ERR_EXIT("localtime"); const char *format = "%b %e %H:%M"; //时间格式 if(strftime(datebuf, sizeof datebuf, format, ptm) == 0)
{
fprintf(stderr, "strftime error\n");
exit(EXIT_FAILURE);
} return datebuf;
} const char *statbuf_get_filename(struct stat *sbuf, const char *name)
{
static char filename[1024] = {0};
//name 处理链接名字
if(S_ISLNK(sbuf->st_mode))
{
char linkfile[1024] = {0};
if(readlink(name, linkfile, sizeof linkfile) == -1)
ERR_EXIT("readlink");
snprintf(filename, sizeof filename, " %s -> %s", name, linkfile);
}else
{
strcpy(filename, name);
} return filename;
} const char *statbuf_get_user_info(struct stat *sbuf)
{
static char info[1024] = {0};
snprintf(info, sizeof info, " %3d %8d %8d", sbuf->st_nlink, sbuf->st_uid, sbuf->st_gid); return info;
} const char *statbuf_get_size(struct stat *sbuf)
{
static char buf[100] = {0};
snprintf(buf, sizeof buf, "%8lu", (unsigned long)sbuf->st_size);
return buf;
}
模拟linux下的ls -l命令的更多相关文章
- 实现Linux下的ls -l命令
基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...
- 高仿linux下的ls -l命令——C语言实现
主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0 相关测试图片: 话不多说,直接上码 #include <stdio.h> #include < ...
- 编程实现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下有趣的终端命令或者工具【转】
转自:https://blog.csdn.net/gatieme/article/details/52144603 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...
- linux下如何使用sftp命令【转】
linux下如何使用sftp命令 from: http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...
- Linux下的一些常用命令(一)
在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...
- linux下find和grep命令详解
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...
- 实现linux下的ls
实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...
随机推荐
- Flex强制类型转换错误
1.错误描写叙述 TypeError: Error #1034: 强制转换类型失败:无法将 HoleDetailInnerClass0@c2cccf1 转换为 mx.controls.listClas ...
- 玩转html5(一)-----盘点html5新增的那些酷酷的input类型和属性
今天正式开始学习html5了,相比html以前的版本,html5新增了好多功能,属性,使我们做出来的界面更加的绚丽,而且使用起来超级简单,这篇文章先来说说html增加的那些input类型和属性. 这些 ...
- 《反project核心原则》说明
致亲爱的中国读者: 大家好 !我是<逆向project核心原理> 作者 李承远(ReverseCore). (韩文博客地址:www.reversecore.com) 首先.非常高兴我的 ...
- CF417D--- Cunning Gena(序列+像缩进dp)
A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t ...
- 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...
- 跟我extjs5(03--在项目过程中加载文件)
跟我extjs5(03--在项目过程中加载文件) 上一节中用sencha工具自己主动创建了一个项目.而且能够在浏览器中查看. 如今我们来看看js类载入过程. 例如以下图所看到的: watermark/ ...
- 取缔Chrome装载电脑管家的广告过滤脚本代码
今天Chrome调试脚本.加载在下面的脚本中找到的内容: /* 电脑管家chrome 广告过滤 */ var GJAD_CS = { elemhideElt : null, setElemhideCS ...
- SQL Server 日志收缩
- 第十九章——使用资源调控器管理资源(1)——使用SQLServer Management Studio 配置资源调控器
原文:第十九章--使用资源调控器管理资源(1)--使用SQLServer Management Studio 配置资源调控器 本系列包含: 1. 使用SQLServer Management Stud ...
- PowerMockito使用详解(转)
一.为什么要使用Mock工具 在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等). 而我们没法控制这些外部依赖的对象,为了解 ...