编程实现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)
相关文章推荐
- Linux&C编程之Linux系统命令“ls -l”的简单实现
- (八)linux下编程实现ls功能的打开、访问 、关闭目录以及提取属性。
- linux编程实践4(实现ls -l命令)
- 编程实现Linux下的ls -l
- Linux系统编程_1_目录读取(实现简单ls命令)
- 使用linux-c编程实现简单的ls命令
- 使用linux-c编程实现简单的ls命令
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】Linux内核抢占实现机制分析
- linux通过shelll编程实现自动化远程唤醒(小陈原创作品,转载请尊重作者)
- Linux守护进程的编程实现
- UNIX环境高级编程学习之第六章系统数据文件和信息-修改第四章实现的Shell的“ls -l”功能
- UNIX环境高级编程学习之第四章文件和目录-用C实现Shell中的"ls -l"功能
- 【嵌入式Linux学习七步曲之第七篇 Linux的高级应用编程】Linux下的线程实现机制分析
- 实战 linux蓝牙编程之(八) hci 协议 应用编程实现
- linux下php实现C/C++扩展编程
- Linux网络编程:一个简单的正向代理服务器的实现
- Linux的那些事儿(4)----Linux环境下编程之Unix标准化及实现
- 成功实现Linux下Socket编程中用send发送结构体
- 【嵌入式Linux学习七步曲之第五篇 Linux内核及驱动编程】Linux系统调用的实现机制分析
- Linux守护进程的编程实现