一个类似于find的简单程序实现 大牛请进!!!!!!!!!!
我是想编一个类似于函数find的简单程序 最后是要以这样的形式运行。/crawl path... [-maxdepth n] [-name pattern] [-type {d,f}] [-size [+-]n]
下面是我的程序,size的部分我不知道怎么编。希望大家不吝赐教!!!1
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<fnmatch.h>
#include<string.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
static int maxdepth;
static char *pattern;
static int size;
static char *path;
static int type;
readline(int argc,char *argv[]){
int i;
if (argc<=1)
{
printf("usage: myfind path... [-maxdepth n] [-name pattern] [-type {d,f}] [-size [+-] n]\n");
return ;
}
else if (argc>=2)
{
for (i=1;i<argc;i++)
{
if (strcmp(argv[i],"-maxdepth")==0 && strlen(argv[i+1])>0)
{
int depth = atoi(argv[i+1]);
if (depth>=0)
{
maxdepth=depth;
}
else return ;
}
else if (strcmp(argv[i],"-maxdepth")==0 && strlen(argv[i+1])==0)
{
return ;
}
if (strcmp(argv[i],"-name")==0 && strlen(argv[i+1])>0)
{
pattern = argv[i+1];
}
else if (strcmp(argv[i],"-name")==0 && strlen(argv[i+1])==0)
{
return ;
}
if (strcmp(argv[i],"-type")==0 && strlen(argv[i+1])==1)
{
if (*argv[i+1]=='f')
{
type=0;
}
else if (*argv[i+1]=='d')
{
type=1;
}
}
else if (strcmp(argv[i],"-type")==0&&strlen(argv[i+1])==0)
{
return ;
}
}
}
return;
}
void find_target(char *dir_name,char *target,int depth){
char str[1024];
DIR *dir_ptr;
struct dirent *dirent_ptr;
int zeichen;
int status;
const char *temp;
int temp_depth;
struct stat file_info;
if(stat(str,&file_info)!=-1){
if(S_ISDIR(file_info.st_mode))
status=1;
}
else status=0;
if((dir_ptr=opendir(dir_name))!=NULL){
while((dirent_ptr=readdir(dir_ptr))!=NULL){
if(strcmp(dirent_ptr->d_name,".")!=0&&strcmp(dirent_ptr->d_name,"..")!=0){
strcpy(str,dir_name);
strcat(str,"/");
strcat(str,dirent_ptr->d_name);
strcat(str,"\0");
temp=strrchr(str,'/');
if(temp==NULL) temp=str;
else temp++;
zeichen=fnmatch(target,temp,FNM_PATHNAME|FNM_PERIOD);
if(zeichen!=FNM_NOMATCH){
printf("%s\n",str);
temp_depth++;
}
if(status<depth)
find_target(str,target,depth);
}
}
closedir(dir_ptr);
}
else {
fprintf(stderr,"can not open the file%s:",dir_name);
perror("");
}
}
main(int argc,char *argv[]){
int i,temp;
for(i=1;i<argc;i++){
if(argv[i][0]=='-') break;
}
if (!(readline(argc, argv))){
exit(EXIT_FAILURE);
}
if(i<2)
find_target(".", pattern, maxdepth);
else{
for (temp=1; temp<i; temp++){
find_target(argv[temp], pattern, maxdepth);
}
}
return ;
}