home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 365_03 / wildcard.c < prev   
C/C++ Source or Header  |  1992-04-06  |  3KB  |  159 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.  * It is also #included into ctags.c, ref.c, ...; in this case,
  18.  * we don't want a main function here.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #ifndef    WILDCARD_NO_MAIN
  23. # include "config.h"
  24. #endif
  25. #include "ctype.h"
  26. #ifdef    __TURBOC__
  27. #include <dir.h>
  28. #endif
  29.  
  30. #ifdef    M_I86
  31. #define    findfirst(a,b,c)    _dos_findfirst(a,c,b)
  32. #define    findnext        _dos_findnext
  33. #define    ffblk            find_t
  34. #define    ff_name            name
  35. #include <dos.h>
  36. #endif
  37.  
  38. /* Atari TOS, MWC */
  39. #ifdef M68000
  40. #include <stat.h>
  41. #include <osbind.h>
  42. #define    findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  43. #define    findnext(x)        (Fsnext())
  44. #define    ff_name    d_fname
  45. #endif
  46.  
  47. /* Atari TOS, GNU-C */
  48. #ifdef __m68k__
  49. #include <stat.h>
  50. #include <osbind.h>
  51. #define    findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  52. #define    findnext(x)        (Fsnext())
  53. #define    ff_name    dta_name
  54. #define    DMABUFFER struct _dta
  55. #endif
  56.  
  57. #define    MAXFILES    1000
  58.  
  59. int pstrcmp();
  60. extern char *calloc();
  61.  
  62. char *files[MAXFILES];
  63. int nfiles;
  64.  
  65. #ifndef    WILDCARD_NO_MAIN
  66.  
  67. main(argc, argv)
  68.     char **argv;
  69. {
  70.     int i;
  71.  
  72.     _ct_init("");
  73.     for (i=1; i<argc; i++)
  74.         expand(argv[i]);
  75.     if (nfiles)
  76.         printf("%s", files[0]);
  77.     for (i=1; i<nfiles; i++)
  78.     {
  79.         printf(" %s", files[i]);
  80.     }
  81.     putchar('\n');
  82.     return 0;
  83. }
  84.  
  85. #else
  86. char **wildexpand(argc, argv)
  87.     int *argc;
  88.     char **argv;
  89. {
  90.     int i;
  91.     
  92.     for (i=0; i<*argc; i++)
  93.         expand(argv[i]);
  94.     *argc=nfiles;
  95.     return files;
  96. }    
  97. #endif
  98.  
  99. expand(name)
  100.     char *name;
  101. {
  102.     char *filespec;
  103.     int wildcard=0;
  104. #if defined(M68000) || defined(__m68k__)
  105.     DMABUFFER findbuf;
  106. #else
  107.     struct ffblk findbuf;
  108. #endif
  109.     int err;
  110.     char buf[80];
  111.     int lastn;
  112.  
  113.     strcpy(buf, name);
  114.     for (filespec=buf; *filespec; filespec++)
  115.         ;
  116.  
  117.     while (--filespec>=buf)
  118.     {    if (*filespec=='?' || *filespec=='*')
  119.             wildcard=1;
  120.         if (*filespec=='/' || *filespec=='\\' || *filespec==':')
  121.             break;
  122.     }
  123.     if (!wildcard)
  124.         addfile(buf);
  125.     else
  126.     {
  127.         lastn=nfiles;
  128.         filespec++;
  129.         if ((err=findfirst(buf, &findbuf, 0))!=0)
  130.             addfile(buf);
  131.         while (!err)
  132.         {
  133.             strcpy(filespec, findbuf.ff_name);
  134.             addfile(buf);
  135.             err=findnext(&findbuf);
  136.         }
  137.         if (lastn!=nfiles)
  138.             qsort(files+lastn, nfiles-lastn, sizeof(char *), pstrcmp);
  139.     }
  140. }
  141.  
  142. addfile(buf)
  143.     char *buf;
  144. {
  145.     char *p;
  146.  
  147.     for (p=buf; *p; p++)
  148.         *p=tolower(*p);
  149.  
  150.     if (nfiles<MAXFILES && (files[nfiles]=calloc(strlen(buf)+1, 1))!=0)
  151.         strcpy(files[nfiles++], buf);
  152. }
  153.  
  154. int pstrcmp(a, b)
  155.     char **a, **b;
  156. {
  157.     return strcmp(*a, *b);
  158. }
  159.