home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / LES177AS.ZIP / WILDCARD.C < prev   
Encoding:
C/C++ Source or Header  |  1991-10-08  |  1.8 KB  |  91 lines

  1. /* wildcard.c */
  2.  
  3. /* Author:
  4.  *    Guntram Blohm
  5.  *    Buchenstrasse 19
  6.  *    7904 Erbach, West Germany
  7.  *    Tel. ++49-7305-6997
  8.  *    sorry - no regular network connection
  9.  */
  10.  
  11. /* this program implements wildcard expansion for elvis/dos. It works
  12.  * like UNIX echo, but uses the dos wildcard conventions
  13.  * (*.* matches all files, * matches files without extension only,
  14.  * filespecs may contain drive letters, wildcards not allowed in directory
  15.  * names).
  16.  *
  17.  * Grieviously hacked by M.Lord for use with LESS177 port to MSDOS.
  18.  * The original elegant code is now barely discernable.
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <dir.h>
  25. #include <dos.h>
  26.  
  27. extern void *malloc();
  28. char *buf, *bufp;
  29. unsigned len;
  30.  
  31. void
  32. addfile(f)
  33.     char *f;
  34. {
  35.     char *p;
  36.  
  37.     if ((!len) && ((buf = bufp = (char *) malloc (len = 128)) == NULL)) {
  38.         len = 0;
  39.         return;
  40.     }
  41.  
  42.     if ((len - (int)(bufp - buf)) <= (2 + strlen(f))) {
  43.         *bufp = '\0';
  44.         if ((p = (char *) malloc(len + 128)) == NULL)
  45.             return;
  46.         len += 128;
  47.         bufp = (char *) strcpy (p, buf);
  48.         free (buf);
  49.         for (buf = bufp; *bufp; bufp++);
  50.     }
  51.     *bufp++ = ' ';
  52.     while ((*bufp++ = tolower(*f++)) != '\0');
  53.     --bufp;
  54. }
  55.  
  56. char *
  57. expand(name)
  58.     char *name;
  59. {
  60.     char *filespec;
  61.     int wildcard=0;
  62.     struct ffblk findbuf;
  63.     int err;
  64.     char f[80];
  65.     len = 0;    /* force allocation of a fresh buffer */
  66.  
  67.     strcpy(f, name);
  68.     for (filespec=f; *filespec; filespec++);
  69.  
  70.     while (--filespec>=f)
  71.     {    if (*filespec=='?' || *filespec=='*')
  72.             wildcard=1;
  73.         if (*filespec=='/' || *filespec=='\\' || *filespec==':')
  74.             break;
  75.     }
  76.     if (!wildcard)
  77.         addfile(f);
  78.     else
  79.     {
  80.         filespec++;
  81.         err=findfirst(f, &findbuf, 0);
  82.         while (!err)
  83.         {
  84.             strcpy(filespec, findbuf.ff_name);
  85.             addfile(f);
  86.             err=findnext(&findbuf);
  87.         }
  88.     }
  89.     return (len == 0) ? NULL : buf;
  90. }
  91.