模拟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 ...
随机推荐
- C#实现远程机器管理
原文:C#实现远程机器管理 目前处于待离职状态,原先所有的工作都在进行交接,过程当中不乏有很多先前整理的和动手尝试实现的功能:我的主页中已经列出来一部分内容,有兴趣的可以前往看一看. 接下来的内容主要 ...
- 关于VCL的编写 (一) 如何编写自己的VCL控件
如何编写自己的VCL控件 用过Delphi的朋友们,大概对Delphi的最喜欢Delphi的不是他的强类型的pascal语法,而是强大的VCL控件,本人就是一位VCL控件的爱好者. VCL控件的开源, ...
- Xcode 5.1.1 与 Xcode 6.0.1 共存
Xcode 5.1.1 (下面简称Xcode5)和Xcode 6.0.1(下面简称Xcode6)都是正式版本号.其应用程序文件名称都是"Xcode".假设通过AppStore升级或 ...
- 【ThinkingInC++】53、构造函数,析构函数,全局变量
/** * 图书:[ThinkingInC++] * 特征:构造函数,析构函数,全局变量 * 时刻:2014年9一个月17日本18:07:43 * 笔者:cutter_point */ #includ ...
- Linux学习笔记——举例说,makefile 多个文件
0.前言 从学习C语言開始就慢慢開始接触makefile,查阅了非常多的makefile的资料但总感觉没有真正掌握makefile,假设自己动手写一个makefile总认为非常吃力. 所以特意 ...
- Windows下Git服务器搭建[转]
Windows下Git服务器搭建 作为对前两天Git服务器搭建的一个整理,我想分别从服务端和客户端两个角度来记录下整个搭建过程,为了达到目标,我们需要哪些操作. (一)服务端软件和账号的安装配置 ...
- c# 通过配置自动附加数据库
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Data.SqlCli ...
- hdu 1298 T9(特里+DFS)
pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...
- 【C语言探索之旅】 第二部分第三课:数组
内容简介 1.课程大纲 2.第二部分第三课: 数组 3.第二部分第四课预告:字符串 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语 ...
- 恶意软件"跨平台" 小心钱包很受伤
什么是跨平台攻击? 举例来说.就像网络诈骗犯为了避开电子商务平台的监控.会在微博上发消息.百度上撒网,腾讯上联系,最后在淘宝上交易.这样的跨平台操作的模式会大大添加犯罪过程监控和取证的难度.而跨平台攻 ...