|
发表于 2007-6-27 15:55:54
|
显示全部楼层
试了一下,给个最简单的程序(还可能有bug,哈)。这个和ls还是有很大的区别的,比如只能有一个参数,还有仍然会显示一些本该隐藏的文件等等。
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <time.h>
- const char *argDir;
- static int get_file_size_time(const char *filename)
- {
- struct stat statbuf;
- if(stat(filename,&statbuf)==-1)
- {
- printf("Get stat on %s Error: %s\n", filename,strerror(errno));
- return(-1);
- }
- if(strcmp(filename, argDir) && strcmp(filename, ".") && strcmp(filename, "..") || !S_ISDIR(statbuf.st_mode))
- printf("%s\n", filename);
- if(S_ISDIR(statbuf.st_mode))
- return(1);
- return(0);
- }
- int main(int argc,char **argv)
- {
- DIR *dirp;
- struct dirent *direntp;
- int stats;
- if(argc!=2)
- {
- printf("Usage: %s filename\n\a",argv[0]);
- exit(1);
- }
- argDir = argv[1];
- if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1))exit(1);
- if((dirp=opendir(argv[1]))==NULL)
- {
- printf("Open Directory %s Error: %s\n",
- argv[1],strerror(errno));
- exit(1);
- }
- chdir(argDir);
- while((direntp=readdir(dirp))!=NULL)
- if(get_file_size_time(direntp->d_name)==-1)
- break;
- closedir(dirp);
- exit(1);
- }
复制代码 |
|