home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ELYVER10.ZIP / MIKXMAS.ZIP / source / wildfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  4.0 KB  |  217 lines

  1. /*
  2.  
  3. Name:
  4. WILDFILE.C
  5.  
  6. Description:
  7. Some routines to support wildcard filename handling.. Done by MikMak
  8.  
  9.     1-3-95  : Adapted so it compiles for both borland & watcom
  10.     30-11-95: OS2 code by Tom Stokes
  11.  
  12. Portability:
  13.  
  14. MSDOS:    BC(y)    Watcom(y)    DJGPP(y)
  15. Win95:    BC(y)
  16. Os2:    y
  17. Linux:    n
  18.  
  19. (y) - yes
  20. (n) - no (not possible or not useful)
  21. (?) - may be possible, but not tested
  22.  
  23. */
  24. #ifdef __OS2__
  25. #define INCL_DOS
  26. #include <os2.h>
  27. #include <os2me.h>
  28. #include <mmio.h>
  29. #else
  30. #include <dos.h>
  31. #endif
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <malloc.h>
  36. #include <process.h>
  37. #include <errno.h>
  38. #include "wildfile.h"
  39.  
  40. static char path[_MAX_PATH];
  41. static char drive[_MAX_DRIVE];
  42. static char dir[_MAX_DIR];
  43. static char fname[_MAX_FNAME];        /* <- prevent TASM clash */
  44. static char ext[_MAX_EXT];
  45.  
  46. #ifdef __OS2__
  47. static HDIR         hdir = HDIR_SYSTEM;    /* os2 directory handle */
  48. static FILEFINDBUF  ffblk;
  49. #else
  50. static struct find_t ffblk;
  51. #endif
  52.  
  53. static char **newargv;
  54. static int count;
  55.  
  56.  
  57. static char *GetFirstName(char *wildname,int attrib)
  58. /*
  59.     Finds the first file in a directory that corresponds to the wildcard
  60.     name 'wildname'.
  61.  
  62.     returns:        ptr to full pathname
  63.  
  64.                 or
  65.  
  66.                 NULL if file couldn't be found
  67. */
  68. {
  69. #ifdef __OS2__
  70.         count = 1;
  71.         if (!DosFindFirst(wildname, &hdir, FILE_NORMAL, &ffblk,
  72.                         sizeof(ffblk), (PULONG)(&count), FIL_STANDARD)) {
  73.                 _splitpath(ffblk.achName,NULL,NULL,fname,ext);
  74.                 _makepath(path,drive,dir,fname,ext);
  75.                 return path;
  76.         }
  77.         return NULL;
  78. #else
  79.     _splitpath(wildname,drive,dir,fname,ext);
  80.     if(!_dos_findfirst(wildname,attrib,&ffblk)){
  81.         _splitpath(ffblk.name,NULL,NULL,fname,ext);
  82.         _makepath(path,drive,dir,fname,ext);
  83.         return path;
  84.     }
  85.     return NULL;
  86. #endif
  87. }
  88.  
  89.  
  90.  
  91. static char *GetNextName(void)
  92. /*
  93.     Finds another file in a directory that corresponds to the wildcard
  94.     name of the GetFirstName call.
  95.  
  96.     returns:        ptr to full pathname
  97.  
  98.                 or
  99.  
  100.                 NULL if file couldn't be found
  101. */
  102. {
  103. #ifdef __OS2__
  104.         count = 1;
  105.         if(!DosFindNext(hdir, &ffblk, sizeof(ffblk), (PULONG)(&count))) {
  106.                 _splitpath(ffblk.achName,NULL,NULL,fname,ext);
  107.                 _makepath(path,drive,dir,ffblk.achName,NULL);
  108.                 return path;
  109.     }
  110.     return NULL;
  111. #else
  112.     if(!_dos_findnext(&ffblk)){
  113.         _splitpath(ffblk.name,NULL,NULL,fname,ext);
  114.         _makepath(path,drive,dir,ffblk.name,NULL);
  115.         return path;
  116.     }
  117.     return NULL;
  118. #endif
  119. }
  120.  
  121.  
  122.  
  123. static void TackOn(char *s)
  124. {
  125.     newargv=(char **)realloc(newargv,(count+2)*sizeof(char *));
  126.  
  127.     if(newargv==NULL){
  128.         perror("Glob");
  129.         exit(-1);
  130.     }
  131.  
  132.     newargv[count++]=strdup(s);
  133.     newargv[count]=NULL;
  134. }
  135.  
  136.  
  137.  
  138. static void Expand(char *wildname,int attrib)
  139. {
  140.     char *s;
  141.  
  142.     s=(strpbrk(wildname,"*?")==NULL) ? NULL : GetFirstName(wildname,attrib);
  143.  
  144.     if(s==NULL){
  145.  
  146.         /* wildname is not a pattern, or there's no match for
  147.            this pattern -> add wildname to the list */
  148.  
  149.         TackOn(wildname);
  150.     }
  151.     else do{
  152.  
  153.         /* add all matches to the list */
  154.         TackOn(s);
  155.  
  156.     } while((s=GetNextName()) != NULL);
  157. }
  158.  
  159.  
  160.  
  161. static int fcmp(const void *a,const void *b)
  162. {
  163.     return(strcmp(*(char **)a,*(char **)b));
  164. }
  165.  
  166.  
  167.  
  168. void MyGlob(int *argc,char **argv[],int attrib)
  169. {
  170.     int i;
  171.     int *idxarr;
  172.  
  173.     newargv=NULL;
  174.     count=1;
  175.  
  176.     idxarr=(int *)calloc(*argc+1,sizeof(int));
  177.         newargv=(char **)calloc(2,sizeof(char *));
  178.  
  179.     if(newargv==NULL || idxarr==NULL){
  180.         errno=ENOMEM;
  181.         perror("Glob");
  182.         exit(-1);
  183.     }
  184.  
  185.     /* init newargv[0] */
  186.  
  187.     newargv[0]=(*argv)[0];
  188.  
  189.     /* Try to expand all arguments except argv[0] */
  190.  
  191.     for(i=1;i<*argc;i++){
  192.  
  193.         /* remember position old arg -> new arg */
  194.  
  195.         idxarr[i]=count;
  196.  
  197.         /* expand the wildcard argument */
  198.  
  199.         Expand((*argv)[i],attrib);
  200.     }
  201.  
  202.     idxarr[i]=count;
  203.  
  204.     for(i=1;i<*argc;i++){
  205.         qsort(&newargv[idxarr[i]],
  206.               idxarr[i+1]-idxarr[i],
  207.               sizeof(char *),fcmp);
  208.     }
  209.  
  210.     /* replace the old argc and argv values by the new ones */
  211.  
  212.     *argc=count;
  213.     *argv=newargv;
  214.  
  215.     free(idxarr);
  216. }
  217.