您的位置:首页 > 运维架构 > Linux

编程实现Linux下的ls -l

2014-08-19 23:18 246 查看

头文件

#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>

#defineENT_CNT128
#defineFILE_LEN256

intget_file_name(DIR*pdir,char*dirname,charnames[][FILE_LEN]);
voidstr_sort(chararr[][FILE_LEN],intcnt);
voidshow_ent(charname[][FILE_LEN],intcnt);
voidmode_to_str(mode_tmode,char*dest);
char*time_format(char*src);

#endif

主函数

/*************************************************************************
>FileName:func.c
>Author:KrisChou
>Mail:zhoujx0219@163.com
>CreatedTime:Tue19Aug201402:31:34PMCST
************************************************************************/
#include"func.h"

intmain(intargc,char*argv[])
{
DIR*pdir;
structdirent*pent;
charfile_names[ENT_CNT][FILE_LEN];
intcnt;//所遍历目录子项的个数
charpold[128]="";//记录程序原来的工作路径
charpnew[128]=".";//记录所需遍历目录的绝对路径,使用stat获取文件信息需要绝对路径(除非程序遍历当前目录)
getcwd(pold,128);
if(argc==2)
{
pdir=opendir(argv[1]);
chdir(argv[1]);
getcwd(pnew,128);
chdir(pold);
}elseif(argc==1)
{
pdir=opendir(".");

}else
{
printf("USAGE:EXEDIR\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);
return0;
}

1.get_file_name

/*************************************************************************
>FileName:./src/get_file_name.c
>Author:KrisChou
>Mail:zhoujx0219@163.com
>CreatedTime:Tue19Aug201403:03:23PMCST
************************************************************************/

#include"func.h"
intget_file_names(DIR*pdir,char*dirname,charnames[][FILE_LEN])
{
structdirent*pent;
intcnt=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);
returncnt;

}

2.str_sort

/*************************************************************************
>FileName:./src/str_sort.c
>Author:KrisChou
>Mail:zhoujx0219@163.com
>CreatedTime:Tue19Aug201403:19:05PMCST
************************************************************************/
#include"func.h"

staticintmy_cmp(constvoid*left,constvoid*right)
{
returnstrcmp((char*)left,(char*)right);
}

voidstr_sort(chararr[][FILE_LEN],intcnt)
{
qsort(arr,cnt,FILE_LEN,my_cmp);
}

3.show_ent

/*************************************************************************
>FileName:show_ent.c
>Author:KrisChou
>Mail:zhoujx0219@163.com
>CreatedTime:Tue19Aug201402:36:17PMCST
************************************************************************/
#include"func.h"

voidshow_ent(charname[][FILE_LEN],intcnt)
{
structstatmy_stat;
charbuf[11]="";
char*pstr;
intindex;
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));//MonAug1814:37:402014
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.1mode_to_str

/*************************************************************************
>FileName:mode_to_str.c
>Author:KrisChou
>Mail:zhoujx0219@163.com
>CreatedTime:Tue19Aug201402:32:33PMCST
************************************************************************/
#include"func.h"

voidmode_to_str(mode_tmode,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.2time_format

/*************************************************************************
>FileName:time_format.c
>Author:KrisChou
>Mail:zhoujx0219@163.com
>CreatedTime:Tue19Aug201402:35:12PMCST
************************************************************************/
#include"func.h"

char*time_format(char*src)
{
intindex=strlen(src)-1;
for(;src[index]!=':';index--)
{

}
src[index]='\0';
returnsrc+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)

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: