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 / WILDEX16.C < prev    next >
Text File  |  2000-06-30  |  6KB  |  228 lines

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