home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / e / elv17src.zip / WILDCARD.C < prev   
C/C++ Source or Header  |  1992-12-30  |  4KB  |  188 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. #ifdef __STDC__
  23. # include <stdlib.h>
  24. #endif
  25. #ifndef    WILDCARD_NO_MAIN
  26. # include "config.h"
  27. #endif
  28. #ifdef    __TURBOC__
  29. #include <dir.h>
  30. #endif
  31.  
  32. /* We include ctype.c here (instead of including just ctype.h and linking
  33.  * with ctype.o) because on some systems ctype.o will have been compiled in
  34.  * "large model" and the wildcard program is to be compiled in "small model" 
  35.  * You can't mix models.  By including ctype.c here, we can avoid linking
  36.  * with ctype.o.
  37.  *
  38.  * HOWEVER, if WILDCARD_NO_MAIN is defined then wildcard.c is itself being
  39.  * included in another .c file, which has either already included ctypes.c or
  40.  * will be linked with ctypes.o, so in this instance we include just ctypos.h.
  41.  */
  42. #ifdef WILDCARD_NO_MAIN
  43. # include "ctype.h"
  44. #else
  45. # include "ctype.c"
  46. #endif
  47.  
  48. #ifdef    M_I86
  49. #define    findfirst(a,b,c)    _dos_findfirst(a,c,b)
  50. #define    findnext        _dos_findnext
  51. #define    ffblk            find_t
  52. #define    ff_name            name
  53. #include <dos.h>
  54. #endif
  55.  
  56. /* Atari TOS, MWC */
  57. #ifdef M68000
  58. #include <stat.h>
  59. #include <osbind.h>
  60. #define    findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  61. #define    findnext(x)        (Fsnext())
  62. #define    ff_name    d_fname
  63. #endif
  64.  
  65. /* Atari TOS, GNU-C */
  66. #ifdef __m68k__
  67. #include <stat.h>
  68. #include <osbind.h>
  69. #define    findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  70. #define    findnext(x)        (Fsnext())
  71. #define    ff_name    dta_name
  72. #define    DMABUFFER struct _dta
  73. #endif
  74.  
  75. #define    MAXFILES    1000
  76.  
  77. int pstrcmp();
  78. #ifndef __STDC__
  79. extern char *calloc();
  80. #endif
  81.  
  82. char *files[MAXFILES];
  83. int nfiles;
  84.  
  85. #ifndef    WILDCARD_NO_MAIN
  86.  
  87. main(argc, argv)
  88.     char **argv;
  89. {
  90.     int i;
  91.  
  92.     _ct_init("");
  93.     for (i=1; i<argc; i++)
  94.         expand(argv[i]);
  95.     if (nfiles)
  96.         printf("%s", files[0]);
  97.     for (i=1; i<nfiles; i++)
  98.     {
  99.         printf(" %s", files[i]);
  100.     }
  101.     putchar('\n');
  102.     return 0;
  103. }
  104.  
  105. #else
  106. char **wildexpand(argc, argv)
  107.     int *argc;
  108.     char **argv;
  109. {
  110.     int i;
  111.     
  112.     for (i=0; i<*argc; i++)
  113.         expand(argv[i]);
  114.     *argc=nfiles;
  115.     return files;
  116. }    
  117. #endif
  118.  
  119. expand(name)
  120.     char *name;
  121. {
  122.     char *filespec;
  123.     int wildcard=0;
  124. #if defined(M68000) || defined(__m68k__)
  125.     DMABUFFER findbuf;
  126. #else
  127.     struct ffblk findbuf;
  128. #endif
  129.     int err;
  130.     char buf[80];
  131.     int lastn;
  132.  
  133.     strcpy(buf, name);
  134.     for (filespec=buf; *filespec; filespec++)
  135.         ;
  136.  
  137.     while (--filespec>=buf)
  138.     {    if (*filespec=='?' || *filespec=='*')
  139.             wildcard=1;
  140.         if (*filespec=='/' || *filespec=='\\' || *filespec==':')
  141.             break;
  142.     }
  143.     if (!wildcard)
  144.         addfile(buf);
  145.     else
  146.     {
  147.         lastn=nfiles;
  148.         filespec++;
  149.         if ((err=findfirst(buf, &findbuf, 0))!=0)
  150.             addfile(buf);
  151.         while (!err)
  152.         {
  153.             strcpy(filespec, findbuf.ff_name);
  154.             addfile(buf);
  155.             err=findnext(&findbuf);
  156.         }
  157.         if (lastn!=nfiles)
  158.             qsort(files+lastn, nfiles-lastn, sizeof(char *), pstrcmp);
  159.     }
  160. }
  161.  
  162. #if MINT
  163. extern int __mint;
  164. #endif
  165. addfile(buf)
  166.     char *buf;
  167. {
  168.     char *p;
  169.  
  170. #if MINT
  171.     /* there are filesystems on MiNT that are case sensitive... and for
  172.      * the vanilla GEMDOS fs MiNT already does this conversion itself.
  173.      */
  174.     if (!__mint)
  175. #endif
  176.     for (p=buf; *p; p++)
  177.         *p=tolower(*p);
  178.  
  179.     if (nfiles<MAXFILES && (files[nfiles]=calloc(strlen(buf)+1, 1))!=0)
  180.         strcpy(files[nfiles++], buf);
  181. }
  182.  
  183. int pstrcmp(a, b)
  184.     char **a, **b;
  185. {
  186.     return strcmp(*a, *b);
  187. }
  188.