头文件

#ifndef __FUNC_H__
#define __FUNC_H__ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h> #define ENT_CNT 128
#define FILE_LEN 256 int get_file_name(DIR* pdir,char* dirname ,char names[][FILE_LEN]);
void str_sort(char arr[][FILE_LEN], int cnt);
void show_ent(char name[][FILE_LEN], int cnt);
void mode_to_str(mode_t mode, char* dest);
char* time_format(char* src); #endif

主函数

/*************************************************************************
> File Name: func.c
> Author: KrisChou
> Mail: zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:31:34 PM CST
************************************************************************/
#include "func.h" int main(int argc, char* argv[])
{
DIR* pdir ;
struct dirent *pent ;
    
    char file_names[ENT_CNT][FILE_LEN] ;
int cnt ; //所遍历目录子项的个数
char pold[128]=""; //记录程序原来的工作路径
char pnew[128]="."; //记录所需遍历目录的绝对路径,使用stat获取文件信息需要绝对路径(除非程序遍历当前目录)
getcwd(pold, 128);
if(argc == 2)
{
pdir = opendir(argv[1]);
chdir(argv[1]);
getcwd(pnew, 128);
chdir(pold);
}else if(argc == 1)
{
pdir = opendir("."); }else
{
printf("USAGE: EXE DIR \n");
exit(1);
} if(pdir == NULL)
{
perror("opendir");
exit(1);
}
cnt = get_file_names(pdir,pnew ,file_names); //将每一项的 绝对路径(pnew) + filename 存入数组,以便stat获取文件信息
str_sort(file_names, cnt);
show_ent(file_names, cnt);
return 0 ;
}

1. get_file_name

/*************************************************************************
> File Name: ./src/get_file_name.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 03:03:23 PM CST
************************************************************************/ #include "func.h"
int get_file_names(DIR* pdir, char* dirname, char names[][FILE_LEN])
{
struct dirent* pent ;
int cnt = 0 ;
while( (pent = readdir(pdir)) != NULL )
{
if(strncmp(pent ->d_name, ".", 1) == 0 || strncmp(pent ->d_name, "..", 2) == 0)
{
continue ;
}
strcpy(names[cnt],dirname);
strcat(names[cnt], "/");
strcat(names[cnt], pent -> d_name);
cnt ++ ;
}
closedir(pdir);
return cnt ; }

2. str_sort

/*************************************************************************
> File Name: ./src/str_sort.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 03:19:05 PM CST
************************************************************************/
#include "func.h" static int my_cmp(const void* left, const void* right )
{
return strcmp((char* )left , (char*)right);
} void str_sort(char arr[][FILE_LEN], int cnt)
{
qsort(arr, cnt, FILE_LEN, my_cmp);
}

3. show_ent

/*************************************************************************
> File Name: show_ent.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:36:17 PM CST
************************************************************************/
#include "func.h" void show_ent(char name[][FILE_LEN], int cnt)
{
struct stat my_stat ;
char buf[11]="";
char *pstr ;
int index ;
for(index = 0; index < cnt; index ++)
{
memset(&my_stat, 0, sizeof(my_stat));
if( stat(name[index], &my_stat) == -1 )
{
perror("stat");
exit (1);
}
mode_to_str(my_stat.st_mode, buf);
pstr = ctime(&(my_stat.st_atime));//Mon Aug 18 14:37:40 2014
pstr = time_format(pstr);
printf("%10s.%2d%7s%7s%5d%13s %s\n",buf, my_stat.st_nlink, getpwuid(my_stat.st_uid)->pw_name, getgrgid(my_stat.st_gid) ->gr_name, my_stat.st_size, pstr,name[index]); }
}

3.1 mode_to_str

/*************************************************************************
> File Name: mode_to_str.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:32:33 PM CST
************************************************************************/
#include "func.h" void mode_to_str(mode_t mode, char* dest)
{
memset(dest,'-',10);
if(S_ISDIR(mode))
{
dest[0]='d';
}
if(S_ISREG(mode))
{
dest[0]='-';
} if(mode & S_IRUSR)
{
dest[1] = 'r' ;
}
if(mode & S_IWUSR)
{
dest[2] = 'w' ;
}
if(mode & S_IXUSR)
{
dest[3] = 'x' ;
}
if(mode & S_IRGRP)
{
dest[4] = 'r' ;
}
if(mode & S_IWGRP)
{
dest[5] = 'w' ;
}
if(mode & S_IXGRP)
{
dest[6] = 'x' ;
}
if(mode & S_IROTH)
{
dest[7] = 'r' ;
}
if(mode & S_IWOTH)
{
dest[8] = 'w' ;
}
if(mode & S_IXOTH)
{
dest[9] = 'x' ;
}
}

3.2 time_format

/*************************************************************************
> File Name: time_format.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:35:12 PM CST
************************************************************************/
#include "func.h" char* time_format(char* src)
{
int index = strlen(src) - 1 ;
for(; src[index]!=':'; index -- )
{ }
src[index] = '\0' ;
return src + 4 ;
}

Makefile

SRC_DIR := ./src
INC_DIR := ./include
EXE_DIR := ./bin
OBJECTS := $(wildcard $(SRC_DIR)/*.c)
INCLUDES :=$(wildcard $(INC_DIR)/*.h)
CC := gcc
FLAGS := -o
$(EXE_DIR)/main : $(OBJECTS) $(INCLUDES)
$(CC) $(FLAGS) $@ $(OBJECTS) -I$(INC_DIR)
 

编程实现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命令

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  4. 实现linux下的ls

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

  5. 【Linux基础】linux下修改ls显示的时间格式

    1.修改ls显示格式 ls -l --time-style '+%Y/%m/%d %H:%M:%S' drwxr-x--- edwetl edwetl // :: arc_test ls -l --t ...

  6. 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 - ...

  7. linux下实现ls()函数遍历目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...

  8. Linux下用ls和du命令查看文件以及文件夹大小

    ls的用法 ls -l |grep "^-"|wc -l或find ./company -type f | wc -l  查看某文件夹下文件的个数,包括子文件夹里的. ls -lR ...

  9. [C++]linux下实现ls()函数遍历目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...

随机推荐

  1. Swift function how to return nil

    这两天在学习Stanford出品的iOS7的课程,这个课程去年也看过,但是看到第3课就不行了,满篇的OC,把人都搞晕了.这段时间因为要写个iOS的App,正好赶上了Swift问世,所以趁着这股劲继续学 ...

  2. Orchard工作原理

    概述 本文翻译仅供学习之用,了解Orchard工作原理设计思想.技术点及关键词,如有缺漏请不吝指正.鉴于能力有限定有诸多曲解或不完整的地方,请海涵.不定时完善整理. CMS不像常规的web程序,它更像 ...

  3. C#取枚举描述

    一直都觉得枚举是个很不错的东西,可以给我们带来很多方便,而且也增加代码的可读性. 我在之前已经介绍过枚举的简要应用了,再次再来写下怎么获取枚举的描述. 源码如下: 首先,我们定义个含有描述的枚举类型 ...

  4. Java实现UDP之Echo客户端和服务端

    Java实现UDP之Echo客户端和服务端 代码内容 采用UDP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...

  5. spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描

    1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK

  6. snmp数据包分析

    今天看了一下snmp数据包的报文格式,用wireshark抓了两个数据包来分析. 先说说snmp get-request的书报包格式吧,get-next-request,get-response,se ...

  7. 使用usb+preseed在virtualbox上安装ubuntu(一)

    1.制作usb boot盘,在ubuntu上使用startup disk creater将ubuntu-server12.04.iso写入到usb中: 2.修改syslinux文件夹中的syslinu ...

  8. 怎么将java项目打包成双击就可以运行的jar包---fatjar

    fatjar下载地址:http://pan.baidu.com/s/1cQ01o 下载fatJar插件,解压缩后是一个.../plugins/(net...)把plugins下面的(net..)文件夹 ...

  9. Careercup - Google面试题 - 5692127791022080

    2014-05-08 22:09 题目链接 原题: Implement a class to create timer object in OOP 题目:用OOP思想设计一个计时器类. 解法:我根据自 ...

  10. Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1

    Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1[转] 1.一般来说MySQL(小于5.5.3)字符集设置为 ...