home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-1 / WILDEXP.C < prev    next >
Text File  |  2000-06-30  |  5KB  |  211 lines

  1. /*
  2.     WILDEXP.C     v1.1    3/21/82
  3.     BDS C Command-line Wild-card expansion utility
  4.     Written by Leor Zolman
  5.  
  6.     Lets ambiguous file names appear on the command line to C programs,
  7.     automatically expanding the parameter list to contain all files that
  8.     fit the afn's.
  9.  
  10.     An afn preceded by a "!" causes all names matching the given afn to
  11.     be EXCLUDED from the resulting expansion list. Thus, to yield a
  12.     command line containing all files except "COM" files, you'd say:
  13.  
  14.         A>progname !*.com <cr>
  15.  
  16.     Another example: to get all files on B: except .C files, say:
  17.  
  18.         A>prognam b:*.* !b:*.c <cr>
  19.  
  20.     When giving a "!" afn, "*" chars in the string matches to the end of
  21.     either the filename or extension, just like CP/M, but "?" chars match
  22.     ONE and ONLY ONE character in either the filename or extension.
  23.  
  24.  
  25.     To use WILDEXP, begin your "main" function as follows:
  26.  
  27.     ---------------------------------------------
  28.     main(argc,argv)
  29.     char **argv;
  30.     {
  31.         ...            /* local declarations  */
  32.         wildexp(&argc,&argv);    /* first statement in program  */
  33.         dioinit(&argc,argv);    /* if using DIO, put this here */
  34.         .
  35.         .
  36.         .
  37.     ---------------------------------------------
  38.  
  39.     and link WILDEXP.CRL in with your program. That's all there is to
  40.     it; note that "wildexp" uses the "sbrk" function to obtain storage,
  41.     so don't go playing around with memory that is outside of the
  42.     external or stack areas unless you obtain the memory through "sbrk"
  43.     or "alloc" calls.
  44.  
  45. */
  46.  
  47. #include    "bdscio.h"
  48. #define        MAXITEMS    200    /* max no. of items after expansion */
  49. #define        SEARCH_FIRST    17    /* BDOS calls */
  50. #define        SEARCH_NEXT    18
  51.  
  52. wildexp(oargcp, oargvp)
  53. int    *oargcp;        /* pointer to old argc */
  54. char    ***oargvp;        /* pointer to old argv */
  55. {
  56.     int    nargc;        /* new argc */
  57.     char    **nargv;    /* new argv */
  58.     char    **oargv;    /* old argv */
  59.     int    oargc;        /* old argc */
  60.     char    fcb[36];    /* fcb used for search for first/next calls */
  61.     char    dmapos;        /* value returned by search calls */
  62.     char    first_time;    /* used in search routine */
  63.     char    tmpfn[20],    /* temp filename buffer */
  64.         *tmpfnp;
  65.     char    *notfns[20];    /* list of !<afn> entries */
  66.     int    notcount;    /* count of entries in notfns */
  67.     char    cur_drive;    /* currently logged drive */
  68.     int    i,j,k;
  69.  
  70.     cur_drive = bdos(25);
  71.  
  72.     oargv = *oargvp;
  73.     oargc = *oargcp;
  74.     nargc = 1;
  75.     notcount = 0;
  76.  
  77.     if ((nargv = sbrk(MAXITEMS * 2 + 2)) == ERROR)
  78.         return ERROR;
  79.  
  80.     for (i = 1; i < oargc; i++)
  81.         if (oargv[i][0] == '!') {
  82.             if (i == 1) {
  83.                 oargv[oargc] = "*.*";
  84.                 oargc++;
  85.             }                
  86.             notfns[notcount++] = &oargv[i][1];
  87.         }
  88.         else if (!haswild(oargv[i]))
  89.             nargv[nargc++] = oargv[i];
  90.         else {
  91.            setfcb(fcb,oargv[i]);
  92.  
  93.            tmpfnp = tmpfn;
  94.            if ((tmpfn[1] = oargv[i][1]) == ':') {
  95.             tmpfn[0] = oargv[i][0];
  96.             tmpfnp = tmpfn + 2;
  97.             bdos(14,tmpfn[0] - 'A');
  98.            }
  99.  
  100.            first_time = TRUE;
  101.            while (1) {            /* find all matching files */
  102.             dmapos = bdos(first_time ? SEARCH_FIRST : SEARCH_NEXT,
  103.                                     fcb);
  104.             if (dmapos == 255) break;
  105.             first_time = FALSE;
  106.             hackname(tmpfnp,(BASE + 0x80 + dmapos * 32));
  107.             if ((nargv[nargc] = sbrk(strlen(tmpfn) + 1)) == ERROR)
  108.                 return ERROR;
  109.             strcpy(nargv[nargc++], tmpfn);
  110.            }
  111.            bdos(14,cur_drive);        /* restore to current drive */
  112.         }
  113.  
  114.     for (i = 0; i < notcount; i++)
  115.         for (j = 1; j < nargc; j++)
  116.             while (match(notfns[i],nargv[j],cur_drive))
  117.             {
  118.                 if(j == --nargc)
  119.                     break;
  120.                 for (k = j; k < nargc; k++)
  121.                     nargv[k] = nargv[k+1];
  122.             }
  123.     *oargcp = nargc;
  124.     *oargvp = nargv;
  125.     return 0;
  126. }
  127.  
  128. hackname(dest,source)
  129. char *dest, *source;
  130. {
  131.     int i,j;
  132.  
  133.     j = 0;
  134.  
  135.     for (i = 1; i < 9; i++)
  136.     {
  137.         if (source[i] == ' ') break;
  138.         dest[j++] = source[i];
  139.     }
  140.     if (source[9] != ' ')
  141.         dest[j++] = '.';
  142.  
  143.     for (i = 9; i < 12; i++)
  144.     {
  145.         if (source[i] == ' ') break;
  146.         dest[j++] = source[i];
  147.     }
  148.     dest[j] = '\0';
  149.     return dest;
  150. }
  151.  
  152. int haswild(fname)
  153. char *fname;
  154. {
  155.     char c;
  156.  
  157.     while (c = *fname++)
  158.         if (c == '*' || c == '?') 
  159.             return TRUE;
  160.     return FALSE;
  161. }
  162.  
  163. int match(wildnam, filnam, cur_drive)
  164. char *wildnam, *filnam, cur_drive;
  165. {
  166.    char c;
  167.  
  168.    if (wildnam[1] != ':')
  169.    {
  170.     if (filnam[1] == ':')
  171.         if (filnam[0] - 'A' == cur_drive)
  172.             filnam += 2;
  173.         else
  174.             return FALSE;
  175.    }
  176.    else
  177.    {
  178.     if (filnam[1] != ':')
  179.         if (wildnam[0] - 'A' == cur_drive)
  180.             wildnam += 2;
  181.         else
  182.             return FALSE;
  183.    }
  184.  
  185.    while (c = *wildnam++)
  186.     if (c == '?')
  187.         if ((c = *filnam++) && c != '.')
  188.             continue;
  189.         else
  190.             return FALSE;
  191.     else if (c == '*')
  192.     {
  193.         while (c = *wildnam)
  194.         {     wildnam++;
  195.             if (c == '.') break;
  196.         }
  197.         while (c = *filnam)
  198.         {    filnam++;
  199.             if (c == '.') break;
  200.         }
  201.     }
  202.     else if (c == *filnam++)
  203.          continue;
  204.     else return FALSE;
  205.  
  206.    if (!*filnam)
  207.     return TRUE;
  208.    else
  209.     return FALSE;
  210. }
  211.