home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 273_01 / findfile.cc < prev    next >
Text File  |  1988-02-11  |  3KB  |  90 lines

  1. #include <dir.h>
  2. #include <dos.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. find_file(char *dir_str, char *file_spec, char *dir_hit, char *file_hit,char *srch_type)
  7. /* find_file will search a disk looking for a specified file. Parms are:
  8.     char *dir_str = starting disk and directory to begin search.
  9.     char *file_spec = file spec to search for.
  10.     char *srch_type = "e" to search for executables i.e. bat, com, or exe.
  11.                     = "n" to search for anything.
  12.                     if search type is set to "e" and a file is found
  13.                     *srch_type will be set to b,c, or e indicating the
  14.                     type of file found. b=batch, c=com, e=exe.
  15.     char *dir_hit   = if file found the directory the file was found in.
  16.     char *file_hit  = if file found the filename.ext of the file.
  17.     RETURN CODE = 1 if file found.
  18.                 = 0 if not found.
  19. */
  20. {
  21. int rc, rc2, rc3, f_rc;
  22. struct ffblk *curr_dta;
  23. char *next_dir;
  24. char *curr_srch;
  25.     curr_dta=(struct ffblk *)malloc(sizeof(struct ffblk));
  26.     next_dir=(char *)malloc(80);
  27.     curr_srch=(char *)malloc(80);
  28.     if(curr_dta == NULL || next_dir == NULL || curr_srch == NULL) return(0);
  29.     strcpy(curr_srch,dir_str);
  30.     strcat(curr_srch,"*.*");
  31.     rc=findfirst(curr_srch,curr_dta,FA_DIREC);
  32.     while(rc == 0) {
  33.         if(curr_dta->ff_attrib & FA_DIREC && curr_dta->ff_name[0] != '.') {
  34.             strcpy(next_dir,dir_str);
  35.             strcat(next_dir,curr_dta->ff_name);
  36.             strcat(next_dir,"\\");
  37.             rc3=find_file(next_dir,file_spec,dir_hit,file_hit,srch_type);
  38.             if(rc3) {
  39.                 rc2=rc3;
  40.                 goto end_it;
  41.             }
  42.         }
  43.         rc=findnext(curr_dta);
  44.     }
  45.  
  46.     rc2=get_files(dir_str,file_spec,curr_dta,dir_hit,file_hit,srch_type);
  47. end_it:
  48.     free(curr_dta); free(next_dir); free(curr_srch);
  49.     return(rc2);
  50. }
  51.  
  52. get_files(char *dir_str, char *file_spec, struct ffblk *curr_dta,
  53.          char *dir_hit, char *file_hit, char *srch_type)
  54. {
  55. int reg_file = FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_ARCH;
  56. int rc;
  57. char search_str[80];
  58. char *exe_types[] = {"BAT","COM","EXE"," "};
  59. int x;
  60.     strupr(srch_type);
  61.     if(*srch_type == 'E') {
  62.         x=0;
  63.         while(*exe_types[x] != ' ') {
  64.             strcpy(search_str,dir_str);
  65.             strcat(search_str,file_spec);
  66.             strcat(search_str,".");
  67.             strcat(search_str,exe_types[x]);
  68.             rc=findfirst(search_str,curr_dta,reg_file);
  69.             if(rc == 0) {
  70.                 strcpy(dir_hit,dir_str);
  71.                 strcpy(file_hit,curr_dta->ff_name);
  72.                 *srch_type = *exe_types[x];
  73.                 return(1);
  74.             }
  75.             x++;
  76.         }
  77.         return(0);
  78.     }
  79.  
  80.     strcpy(search_str,dir_str);
  81.     strcat(search_str,file_spec);
  82.     rc=findfirst(search_str,curr_dta,reg_file);
  83.     if(rc == 0) {
  84.         strcpy(dir_hit,dir_str);
  85.         strcpy(file_hit,curr_dta->ff_name);
  86.         return(1);
  87.     }
  88.     return(0);
  89. }
  90.