home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / apps / text_ed / elv16b2 / st / wildcard.c < prev   
C/C++ Source or Header  |  1992-08-09  |  3KB  |  173 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.  
  61. #ifdef __STDC__
  62. #include <string.h>
  63. #include <stdlib.h>
  64. #else
  65. extern char *calloc();
  66. #endif
  67.  
  68. char *files[MAXFILES];
  69. int nfiles;
  70.  
  71. #ifndef    WILDCARD_NO_MAIN
  72.  
  73. main(argc, argv)
  74.     char **argv;
  75. {
  76.     int i;
  77.  
  78.     _ct_init("");
  79.     for (i=1; i<argc; i++)
  80.         expand(argv[i]);
  81.     if (nfiles)
  82.         printf("%s", files[0]);
  83.     for (i=1; i<nfiles; i++)
  84.     {
  85.         printf(" %s", files[i]);
  86.     }
  87.     putchar('\n');
  88.     return 0;
  89. }
  90.  
  91. #else
  92. char **wildexpand(argc, argv)
  93.     int *argc;
  94.     char **argv;
  95. {
  96.     int i;
  97.     
  98.     for (i=0; i<*argc; i++)
  99.         expand(argv[i]);
  100.     *argc=nfiles;
  101.     return files;
  102. }    
  103. #endif
  104.  
  105. int expand(name)
  106.     char *name;
  107. {
  108.     char *filespec;
  109.     int wildcard=0;
  110. #if defined(M68000) || defined(__m68k__)
  111.     DMABUFFER findbuf;
  112. #else
  113.     struct ffblk findbuf;
  114. #endif
  115.     int err;
  116.     char buf[80];
  117.     int lastn;
  118.  
  119.     strcpy(buf, name);
  120.     for (filespec=buf; *filespec; filespec++)
  121.         ;
  122.  
  123.     while (--filespec>=buf)
  124.     {    if (*filespec=='?' || *filespec=='*')
  125.             wildcard=1;
  126.         if (*filespec=='/' || *filespec=='\\' || *filespec==':')
  127.             break;
  128.     }
  129.     if (!wildcard)
  130.         addfile(buf);
  131.     else
  132.     {
  133.         lastn=nfiles;
  134.         filespec++;
  135.         if ((err=findfirst(buf, &findbuf, 0))!=0)
  136.             addfile(buf);
  137.         while (!err)
  138.         {
  139.             strcpy(filespec, findbuf.ff_name);
  140.             addfile(buf);
  141.             err=findnext(&findbuf);
  142.         }
  143.         if (lastn!=nfiles)
  144.             qsort(files+lastn, nfiles-lastn, sizeof(char *), pstrcmp);
  145.     }
  146. }
  147.  
  148. #if MINT
  149. extern    int __mint;
  150. #endif
  151.  
  152. int addfile(buf)
  153.     char *buf;
  154. {
  155.     char *p;
  156.  
  157. #if MINT
  158.     /* there are filesystems on MiNT that are case sensitive... and for
  159.        the vanilla GEMDOS fs MiNT already does this conversion itself  */
  160.     if (!__mint)
  161. #endif
  162.     for (p=buf; *p; p++)
  163.         *p=tolower(*p);
  164.     if (nfiles<MAXFILES && (files[nfiles]=calloc(strlen(buf)+1, (size_t) 1))!=0)
  165.         strcpy(files[nfiles++], buf);
  166. }
  167.  
  168. int pstrcmp(a, b)
  169.     char **a, **b;
  170. {
  171.     return strcmp(*a, *b);
  172. }
  173.