#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
//主要就是这个函数进行查找,其中4个参数,分别是查找起始路径,需要查找的文件名,保存文件所在的路径,用于在对目录文件进行递归调用时跳出函数
char * find (char *, char *, char *, int *);

int main(){
   printf("输入你需要查找的文件名:");
   char filename[256];
   scanf("%s",filename);
   char pathname[256];
   printf("输入你查找的起始目录:");
   scanf("%s",pathname);
   char new_p[256];
   char * a;
   int judge;
   judge = 0;
   int * jud;
   jud= &judge;
   if((a=find(pathname, filename, new_p, jud))!= NULL)
  {
      if(*jud==0)
      {
          printf("找到文件:\n");
          printf("它的目录是: %s\n",new_p);
      }
  }

  else
      printf("不存在此文件\n");
  return 0;
}


char * find(char * pathname, char * filename, char * new_p, int * jud){
      DIR * dir;
      char *h;
      if((dir=opendir(pathname))==NULL)
      {
          perror("opendir");
          exit(1);
      }
      struct dirent * ptr;
      struct stat buf;
      while((ptr=readdir(dir))!=NULL)
      {
          memset(&buf,0,sizeof(struct stat));
        if(strncmp(ptr->d_name,".",1)==0)
          {
            continue;
          }
          char new_pathname[256];
        int i;
          for(i=0;pathname[i]!='\0';++i)
          {
              new_pathname[i]=pathname[i];
          }
          char between[1];
          between[0]='/';
          new_pathname[i] = between[0];
          ++i;
          int k;
          int j;
          for(k=i,j=0; ptr->d_name[j]!='\0';j++,k++)
          {
              new_pathname[k]=ptr->d_name[j];
          }
          new_pathname[k]='\0';

        if((stat(new_pathname,&buf))==-1)
          {
              perror("stat");
              exit(1);
          }
          if((buf.st_mode & S_IFMT)==S_IFDIR)
          {
               if((h=find(new_pathname,filename,new_p,jud))!=NULL)
               {
                   if(*jud==0)
                   {
                   int new_i;
                   for(new_i=0;new_pathname[new_i]!='\0';)
                       {
                           ++new_i;
                       }
                   int file_i;
                   for(file_i=0;filename[file_i]!='\0';)
                       {
                            ++file_i;
                       }
                   char bw[1];
                   bw[0]='/';
                   new_pathname[new_i]=bw[0];
                   new_i++;
                   int count_i;
                   for(count_i=0;count_i<=file_i;++count_i)
                   {
                       new_pathname[new_i]=filename[count_i];
                       ++new_i;
                   }

                   strcpy(new_p,new_pathname);
                   printf("找到文件:\n");
                   printf("它的目录是:%s\n",new_pathname);
                   *jud=1;                
                   break;
                   }
                   break;
               }                
          }
          else
          {
              if(strcmp(ptr->d_name,filename)==0)
              {
                  strcpy(new_p,new_pathname);
                  return new_p;
              }
          }
      }
      if(*jud==1)
      {
          return "find";
      }
      else
      {
          return NULL;
      }

}