#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命令的更多相关文章

  1. 实现Linux下的ls -l命令

    基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...

  2. 高仿linux下的ls -l命令——C语言实现

    主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0 相关测试图片: ​ ​ 话不多说,直接上码 #include <stdio.h> #include < ...

  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下有趣的终端命令或者工具【转】

    转自:https://blog.csdn.net/gatieme/article/details/52144603 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  6. linux下如何使用sftp命令【转】

    linux下如何使用sftp命令 from:   http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个 ...

  7. Linux下的一些常用命令(一)

    在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...

  8. linux下find和grep命令详解

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...

  9. 实现linux下的ls

    实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...

随机推荐

  1. 于iOS跳转到应用程序AppStore

    1.找到应用程序的叙述原文链接,实例: https://57324.api-01.com/serve? action=click&publisher_id=57324&site_id= ...

  2. aspx向silverlight传值

    原文:http://www.cnblogs.com/lensso/archive/2010/07/27/1785844.html 方法1: 向嵌入aspx页面的silverlight对象添加imnit ...

  3. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  4. MSSQL连接字符串,你真的清楚吗?

    原文:MSSQL连接字符串,你真的清楚吗? 几年前当我第一次面试时,考官发现我是个新手于是他让我写个连接字符串,虽然当时就知道X种连接字符串的写法,但是当时却没能写对一个,工作多年后我仍然不能写一个正 ...

  5. BeagleBone Black教训四局:简单LED对照实验

    BBB教训四局:简单LED对照实验 学习BBB董事会最终目的是做同样的想象单片机控制.但控制是不一样的想法,在所有(Linux在本质上,硬件设备的控制,以虚拟文件有关的设备下的读写),研究了几天头都大 ...

  6. spring 配置属性的详细信息

    摘要(这篇文章讲的红,蓝说这话节) 字面值 字面值:可用字符串表示的值,能够通过<value>元素标签或value属性进行注入 基本数据类型及其封装类.String等类型都能够採取字面值注 ...

  7. 【LeetCode】【Python解读】Container with most water

    这个问题是芭芭拉在采访中遇到的,不幸的是,的复杂性O(n2)该,太失望了,难怪没有通过面试. Given n non-negative integers a1, a2, ..., an, where ...

  8. 探索static——不需要能够使用该类实例?

    在一般情况下.需要使用一个上课时间.你必须先实例化类,它调用的能力.在编程过程中发现.有些类不能直接实例来使用,利用其场.法等等. 这时候.靠的就是static作用.static英文意思为" ...

  9. cocos2dX 音乐和声音效果

    今天, 我们学习cocos2dX里面音乐和音效, 为什么学他呢, 难道你喜欢看无声电影( 旁白: 我就喜欢关着声音玩), 我们不要管旁白了, 她是个疯子 老规矩, 资源准备好: 一首<故宫神思& ...

  10. WPF学习(1)WPF概述

    WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于NET Framework 3.0的一部分.它提供了统一的编程模型 ...