home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / FILES202.ZIP / GETFILE.C < prev    next >
C/C++ Source or Header  |  1989-02-28  |  7KB  |  279 lines

  1. /* getfile.c
  2. **
  3. ** Copyright (c) 1988-89, Christopher Laforet
  4. ** All Rights Reserved
  5. **
  6. ** (v1.xx) for Turbo-C
  7. ** (v2.xx) for OS/2 - CML 2-89
  8. **
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <os2.h>
  15. #include <string.h>
  16. #include "files.h"
  17.  
  18.  
  19.  
  20.  
  21. int compare(arg1,arg2)
  22. struct file **arg1;
  23. struct file **arg2;
  24.     {
  25.     return(strcmp((*arg1)->file_name,(*arg2)->file_name));
  26.     }
  27.  
  28.  
  29. void getfile(path_name,file_mask)
  30. unsigned char *path_name;
  31. unsigned char *file_mask;
  32.     {
  33.     struct file **file_info = NULL;
  34.     unsigned short cur_alloc = 0;
  35.     unsigned short max_alloc = 0;
  36.     FILEFINDBUF findbuf;
  37.     HDIR hdir;
  38.     unsigned char buffer[100];
  39.     unsigned char date[15];
  40.     unsigned char time[10];
  41.     unsigned short rtn;
  42.     unsigned short num = 1;
  43.     unsigned short count;
  44.  
  45.     hdir = _osmode == OS2_MODE ? 0xffff : 0x1;
  46.     if (path_name[0])
  47.         {
  48.         sprintf(buffer,"%s\\%s",path_name,file_mask);
  49.         }
  50.     else
  51.         {
  52.         strcpy(buffer,file_mask);
  53.         }
  54.     strlwr(path_name);
  55.     rtn = DosFindFirst(buffer,&hdir,0x37,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num,0L);
  56.     while (!rtn)
  57.         {
  58.         if (cur_alloc >= max_alloc)
  59.             {
  60.             if (!(file_info = realloc(file_info,(max_alloc += 6) * sizeof(struct file *))))
  61.                 {
  62.                 fprintf(stderr,"Aborting: Out of memory...\n");
  63.                 exit(-1);
  64.                 }
  65.             }
  66.         if (!(file_info[cur_alloc] = calloc(1,sizeof(struct file))))
  67.             {
  68.             fprintf(stderr,"Aborting: Out of memory...\n");
  69.             exit(-1);
  70.             }
  71.         if (!(file_info[cur_alloc]->file_name = calloc(findbuf.cchName + 1,sizeof(char))))
  72.             {
  73.             fprintf(stderr,"Aborting: Out of memory...\n");
  74.             exit(-1);
  75.             }
  76.         strcpy(file_info[cur_alloc]->file_name,findbuf.achName);
  77.         strlwr(file_info[cur_alloc]->file_name);
  78.         file_info[cur_alloc]->file_attr = findbuf.attrFile;
  79.         memcpy(&file_info[cur_alloc]->file_date,&findbuf.fdateLastWrite,sizeof(FDATE));
  80.         memcpy(&file_info[cur_alloc]->file_time,&findbuf.ftimeLastWrite,sizeof(FTIME));
  81.         file_info[cur_alloc]->file_size = findbuf.cbFile;
  82.         ++cur_alloc;
  83.         num = 1;
  84.         rtn = DosFindNext(hdir,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num);
  85.         }
  86.     DosFindClose(hdir);
  87.     if (sort_flag)
  88.         {
  89.         qsort(file_info,cur_alloc,sizeof(struct file *),compare);
  90.         }
  91.     for (count = 0; count < cur_alloc; count++)
  92.         {
  93.         convert_date(&file_info[count]->file_date,date);
  94.         convert_time(&file_info[count]->file_time,time);
  95.         if ((directory_flag == 0) && !(file_info[count]->file_attr & 0x10))
  96.             {
  97.             ++total_files;
  98.             total_bytes += file_info[count]->file_size;
  99.             if (verbose_flag)
  100.                 {
  101.                 if (file_info[count]->file_attr & 0x1)
  102.                     {
  103.                     fprintf(stdout,"-r");
  104.                     }
  105.                 else
  106.                     {
  107.                     fprintf(stdout,"--");
  108.                     }
  109.                 if (file_info[count]->file_attr & 0x2)
  110.                     {
  111.                     fprintf(stdout,"h");
  112.                     }
  113.                 else
  114.                     {
  115.                     fprintf(stdout,"-");
  116.                     }
  117.                 if (file_info[count]->file_attr & 0x4)
  118.                     {
  119.                     fprintf(stdout,"s");
  120.                     }
  121.                 else
  122.                     {
  123.                     fprintf(stdout,"-");
  124.                     }
  125.                 if (file_info[count]->file_attr & 0x8)
  126.                     {
  127.                     fprintf(stdout,"v");
  128.                     }
  129.                 else
  130.                     {
  131.                     fprintf(stdout,"-");
  132.                     }
  133.                 if (file_info[count]->file_attr & 0x20)
  134.                     {
  135.                     fprintf(stdout,"a");
  136.                     }
  137.                 else
  138.                     {
  139.                     fprintf(stdout,"-");
  140.                     }
  141.                 fprintf(stdout,"   %7lu %12.12s  %5.5s  %s%s%s\n",file_info[count]->file_size,date,time,path_name,path_name[0] ? "\\" : "",file_info[count]->file_name);
  142.                 }
  143.             else
  144.                 {
  145.                 fprintf(stdout," %7lu %12.12s  %5.5s  %s%s%s\n",file_info[count]->file_size,date,time,path_name,path_name[0] ? "\\" : "",file_info[count]->file_name);
  146.                 }
  147.             }
  148.         else if ((file_info[count]->file_attr & 0x10) && (file_info[count]->file_name[0] != '.'))
  149.             {
  150.             if (verbose_flag)
  151.                 {
  152.                 fprintf(stdout,"d-----");
  153.                 fprintf(stdout,"     -DIR- %12.12s  %5.5s  %s%s%s\n",date,time,path_name,path_name[0] ? "\\" : "",file_info[count]->file_name);
  154.                 }
  155.             else
  156.                 {
  157.                 fprintf(stdout,"   -DIR- %12.12s  %5.5s  %s%s%s\n",date,time,path_name,path_name[0] ? "\\" : "",file_info[count]->file_name);
  158.                 }
  159.             }
  160.         }
  161.     if (pattern_flag)
  162.         {
  163.         unsigned char newbuf[100];
  164.  
  165.         if (cur_alloc)
  166.             {
  167.             for (count = 0; count < cur_alloc; count++)
  168.                 {
  169.                 free(file_info[count]->file_name);
  170.                 free(file_info[count]);
  171.                 }
  172.             free(file_info);
  173.             file_info = NULL;
  174.             cur_alloc = max_alloc = 0;
  175.             }
  176.         if (path_name[0])
  177.             {
  178.             sprintf(newbuf,"%s\\*.*",path_name);
  179.             }
  180.         else
  181.             {
  182.             strcpy(newbuf,"*.*");
  183.             }
  184.         hdir = _osmode == OS2_MODE ? 0xffff : 0x1;
  185.         num = 1;
  186.         rtn = DosFindFirst(newbuf,&hdir,0x10,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num,0L);
  187.         while (!rtn)
  188.             {
  189.             if ((findbuf.attrFile & 0x10) && !current_flag && (findbuf.achName[0] != '.'))
  190.                 {
  191.                 if (cur_alloc >= max_alloc)
  192.                     {
  193.                     if (!(file_info = realloc(file_info,(max_alloc += 6) * sizeof(struct file *))))
  194.                         {
  195.                         fprintf(stderr,"Aborting: Out of memory...\n");
  196.                         exit(-1);
  197.                         }
  198.                     }
  199.                 if (!(file_info[cur_alloc] = calloc(1,sizeof(struct file))))
  200.                     {
  201.                     fprintf(stderr,"Aborting: Out of memory...\n");
  202.                     exit(-1);
  203.                     }
  204.                 if (!(file_info[cur_alloc]->file_name = calloc(findbuf.cchName + 1,sizeof(char))))
  205.                     {
  206.                     fprintf(stderr,"Aborting: Out of memory...\n");
  207.                     exit(-1);
  208.                     }
  209.                 strcpy(file_info[cur_alloc]->file_name,findbuf.achName);
  210.                 strlwr(file_info[cur_alloc]->file_name);
  211.                 file_info[cur_alloc]->file_attr = findbuf.attrFile;
  212.                 memcpy(&file_info[cur_alloc]->file_date,&findbuf.fdateLastWrite,sizeof(FDATE));
  213.                 memcpy(&file_info[cur_alloc]->file_time,&findbuf.ftimeLastWrite,sizeof(FTIME));
  214.                 file_info[cur_alloc]->file_size = findbuf.cbFile;
  215.                 ++cur_alloc;
  216.                 }
  217.             num = 1;
  218.             rtn = DosFindNext(hdir,(PFILEFINDBUF)&findbuf,sizeof(FILEFINDBUF),&num);
  219.             }
  220.         DosFindClose(hdir);
  221.         if (cur_alloc)
  222.             {
  223.             for (count = 0; count < cur_alloc; count)
  224.                 {
  225.                 if (path_name[0])
  226.                     {
  227.                     sprintf(newbuf,"%s\\%s",path_name,file_info[count]->file_name);
  228.                     }
  229.                 else
  230.                     {
  231.                     sprintf(newbuf,"%s",file_info[count]->file_name);
  232.                     }
  233.                 getfile(newbuf,file_mask);
  234.                 }
  235.             for (count = 0; count < cur_alloc; count++)
  236.                 {
  237.                 free(file_info[count]->file_name);
  238.                 free(file_info[count]);
  239.                 }
  240.             free(file_info);
  241.             file_info = NULL;
  242.             cur_alloc = max_alloc = 0;
  243.             }
  244.         if (sort_flag)
  245.             {
  246.             qsort(file_info,cur_alloc,sizeof(struct file *),compare);
  247.             }
  248.         }
  249.     else if (!current_flag)
  250.         {
  251.         for (count = 0; count < cur_alloc; count++)
  252.             {
  253.             if ((file_info[count]->file_attr & 0x10) && (file_info[count]->file_name[0] != '.'))    /* directory */
  254.                 {
  255.                 if (path_name[0])
  256.                     {
  257.                     sprintf(buffer,"%s\\%s",path_name,file_info[count]->file_name);
  258.                     }
  259.                 else
  260.                     {
  261.                     strcpy(buffer,file_info[count]->file_name);
  262.                     }
  263.                 getfile(buffer,file_mask);
  264.                 free(file_info[count]->file_name);
  265.                 free(file_info[count]);
  266.                 }
  267.             else
  268.                 {
  269.                 free(file_info[count]->file_name);
  270.                 free(file_info[count]);
  271.                 }
  272.             }
  273.         }
  274.     if (file_info)
  275.         {
  276.         free(file_info);
  277.         }
  278.     }
  279.