home *** CD-ROM | disk | FTP | other *** search
/ PC Loisirs 18 / cd.iso / sharewar / mikm202 / source / wildfile.c < prev   
Encoding:
C/C++ Source or Header  |  1995-09-18  |  2.9 KB  |  172 lines

  1. /*
  2.     WILDFILE.C
  3.  
  4.     Some routines to support wildcard filename handling.. Done by MikMak
  5.  
  6.     1-3-95 : Adapted so it compiles for both borland & watcom
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <malloc.h>
  13. #include <process.h>
  14. #include <errno.h>
  15. #include <dos.h>
  16. #include "wildfile.h"
  17.  
  18. static char path[_MAX_PATH];
  19. static char drive[_MAX_DRIVE];
  20. static char dir[_MAX_DIR];
  21. static char name[_MAX_FNAME];
  22. static char ext[_MAX_EXT];
  23.  
  24. static struct find_t ffblk;
  25.  
  26. static char **newargv;
  27. static int count;
  28.  
  29.  
  30. char *GetFirstName(char *wildname,int attrib)
  31. /*
  32.     Finds the first file in a directory that corresponds to the wildcard
  33.     name 'wildname'.
  34.  
  35.     returns:        ptr to full pathname
  36.  
  37.                 or
  38.  
  39.                 NULL if file couldn't be found
  40. */
  41. {
  42.     _splitpath(wildname,drive,dir,name,ext);
  43.     if(!_dos_findfirst(wildname,attrib,&ffblk)){
  44.         _splitpath(ffblk.name,NULL,NULL,name,ext);
  45.         _makepath(path,drive,dir,name,ext);
  46.         return path;
  47.     }
  48.     return NULL;
  49. }
  50.  
  51.  
  52.  
  53. char *GetNextName(void)
  54. /*
  55.     Finds another file in a directory that corresponds to the wildcard
  56.     name of the GetFirstName call.
  57.  
  58.     returns:        ptr to full pathname
  59.  
  60.                 or
  61.  
  62.                 NULL if file couldn't be found
  63. */
  64. {
  65.     if(!_dos_findnext(&ffblk)){
  66.         _splitpath(ffblk.name,NULL,NULL,name,ext);
  67.         _makepath(path,drive,dir,ffblk.name,NULL);
  68.         return path;
  69.     }
  70.     return NULL;
  71. }
  72.  
  73.  
  74. static char **newargv;
  75. static int count;
  76.  
  77.  
  78. void TackOn(char *s)
  79. {
  80.     newargv=realloc(newargv,(count+2)*sizeof(char *));
  81.  
  82.     if(newargv==NULL){
  83.         perror("Glob");
  84.         exit(-1);
  85.     }
  86.  
  87.     newargv[count++]=strdup(s);
  88.     newargv[count]=NULL;
  89. }
  90.  
  91.  
  92.  
  93. void Expand(char *wildname,int attrib)
  94. {
  95.     char *s;
  96.  
  97.     s=(strpbrk(wildname,"*?")==NULL) ? NULL : GetFirstName(wildname,attrib);
  98.  
  99.     if(s==NULL){
  100.  
  101.         /* wildname is not a pattern, or there's no match for
  102.            this pattern -> add wildname to the list */
  103.  
  104.         TackOn(wildname);
  105.     }
  106.     else do{
  107.  
  108.         /* add all matches to the list */
  109.         TackOn(s);
  110.  
  111.     } while((s=GetNextName()) != NULL);
  112. }
  113.  
  114.  
  115.  
  116. int fcmp(const void *a,const void *b)
  117. {
  118.     return(strcmp(*(char **)a,*(char **)b));
  119. }
  120.  
  121.  
  122.  
  123. void MyGlob(int *argc,char **argv[],int attrib)
  124. {
  125.     int i;
  126.     int *idxarr;
  127.  
  128.     newargv=NULL;
  129.     count=1;
  130.  
  131.     idxarr=calloc(*argc+1,sizeof(int));
  132.     newargv=calloc(2,sizeof(char **));
  133.  
  134.     if(newargv==NULL || idxarr==NULL){
  135.         errno=ENOMEM;
  136.         perror("Glob");
  137.         exit(-1);
  138.     }
  139.  
  140.     // init newargv[0]
  141.  
  142.     newargv[0]=(*argv)[0];
  143.  
  144.     // Try to expand all arguments except argv[0]
  145.  
  146.     for(i=1;i<*argc;i++){
  147.  
  148.         // remember position old arg -> new arg
  149.  
  150.         idxarr[i]=count;
  151.  
  152.         // expand the wildcard argument
  153.  
  154.         Expand((*argv)[i],attrib);
  155.     }
  156.  
  157.     idxarr[i]=count;
  158.  
  159.     for(i=1;i<*argc;i++){
  160.         qsort(&newargv[idxarr[i]],
  161.               idxarr[i+1]-idxarr[i],
  162.               sizeof(char *),fcmp);
  163.     }
  164.  
  165.     // replace the old argc and argv values by the new ones
  166.  
  167.     *argc=count;
  168.     *argv=newargv;
  169.  
  170.     free(idxarr);
  171. }
  172.