头文件

#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. extension 的一个应用 - 优化图片的读取机制

    枚举和 extension 都是 swift 中非常好用的特性.这里我们就来讨论一个应用的例子,供大家参考. 我们在开发 app 的时候,都会用到各种图片资源,而我们读取图片资源时主要是通过UIIma ...

  2. [转]NHibernate之映射文件配置说明

    1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表名会加上所指定的schema的名字扩展为 ...

  3. QT 的信号与槽

    转载: QT 的信号与槽机制介绍 QT 是一个跨平台的 C++ GUI 应用构架,它提供了丰富的窗口部件集,具有面向对象.易于扩展.真正的组件编程等特点,更为引人注目的是目前 Linux 上最为流行的 ...

  4. PB中获取datawindow提交的sql语句

    PB的群里边,有人问的到这个问题,查了一下,综合了两条回答,得到了答案 1.DW 控件的SQLpreview 事件里的sqlsyntax 参数即是 2.pb一般使用占位符优化SQL语句,也就是你看到的 ...

  5. PB中掉用Run以后,等Run的程序关闭以后才会执行后边的语句

    OleObject wsh integer li_rc CONSTANT integer MAXIMIZED = CONSTANT integer MINIMIZED = CONSTANT integ ...

  6. 如果选择构建ui界面方式,手写代码,xib和StoryBoard间的博弈

    代码手写UI这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用. 大型多人合作项目使用代码构建UI,主要是看中纯代码在版本管理时的优势,检查追踪改动以及进行代码合并相对容易一些. 另外,代 ...

  7. win8中如何禁用屏幕旋转的快捷键

    程序员通常会使用ctrl+alt+方向键 里编辑代码,特别对于使用eclipse的程序员,更是如此,但是win8却把这一快捷键给占用了,很不爽,如何办,很简单.直接上图: 2.但是发现禁用之后并没有解 ...

  8. SqlServer中Sql语句的逻辑执行顺序

    准备数据 Sql脚本如下,两张表,一张客户表Customers只包含customerid和city字段,一张订单表Orders包含orderid和customerid(关联Customers的cust ...

  9. 判断GPS、网络是否开启

    判断GPS.网络是否开启 1.判断GPS打开与否,没有打开则打开GPS private void initGPS(Context context) { LocationManager location ...

  10. 从一个Activity返回上一个Activity

    从一个Activity返回上一个Activity 要求:保留上一个Activity的数据 方法: 第一步:从Activity1转向Activity2时,用startActivityForResult而 ...