home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / elvis184.zip / src / wildcard.c < prev   
C/C++ Source or Header  |  1995-04-23  |  5KB  |  217 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 and elvprsv.c; in this case,
  18.  * we don't want a main function here.
  19.  */
  20.  
  21. /* OS/2, emx+gcc 0.9a */
  22. #if (defined(OS2) && !defined(WILDCARD_NO_MAIN))
  23. # include <sys/emx.h>   /* sigh...emx insists this be the first header file */
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #ifdef __STDC__
  28. # include <stdlib.h>
  29. #endif
  30. #ifndef    WILDCARD_NO_MAIN
  31. # include "config.h"
  32. #endif
  33. #ifdef    __TURBOC__
  34. # include <dir.h>
  35. #endif
  36.  
  37. void    expand P_((char *));
  38. void    addfile P_((char *));
  39.  
  40. /* We include ctype.c here (instead of including just ctype.h and linking
  41.  * with ctype.o) because on some systems ctype.o will have been compiled in
  42.  * "large model" and the wildcard program is to be compiled in "small model" 
  43.  * You can't mix models.  By including ctype.c here, we can avoid linking
  44.  * with ctype.o.
  45.  *
  46.  * HOWEVER, if WILDCARD_NO_MAIN is defined then wildcard.c is itself being
  47.  * included in another .c file, which has either already included ctypes.c or
  48.  * will be linked with ctypes.o, so in this instance we include just ctypos.h.
  49.  */
  50. #ifdef WILDCARD_NO_MAIN
  51. # include "ctype.h"
  52. #else
  53. # include "ctype.c"
  54. #endif
  55.  
  56. #ifdef    M_I86
  57. # define findfirst(a,b,c)    _dos_findfirst(a,c,b)
  58. # define findnext        _dos_findnext
  59. # define ffblk            find_t
  60. # define ff_name        name
  61. # include <dos.h>
  62. #endif
  63.  
  64. /* Atari TOS, MWC */
  65. #ifdef M68000
  66. # include <stat.h>
  67. # include <osbind.h>
  68. # define findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  69. # define findnext(x)        (Fsnext())
  70. # define ff_name        d_fname
  71. #endif
  72.  
  73. /* Atari TOS, GNU-C */
  74. #ifdef __m68k__
  75. # include <stat.h>
  76. # include <osbind.h>
  77. # define findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  78. # define findnext(x)        (Fsnext())
  79. # define ff_name        dta_name
  80. # define DMABUFFER         struct _dta
  81. #endif
  82.  
  83. /* OS/2, emx+gcc 0.8x */
  84. #if OS2
  85. # define findfirst(a,b,c)    __findfirst(a,c,b)
  86. # define findnext        __findnext
  87. # define ffblk            _find
  88. # define ff_name        name
  89. #endif
  90.  
  91. #define    MAXFILES    1000
  92.  
  93. #if !defined(__STDC__) && !defined(__TURBOC__)
  94. extern char *calloc();
  95. #endif
  96.  
  97. char *files[MAXFILES];
  98. int nfiles;
  99.  
  100. #ifndef    WILDCARD_NO_MAIN
  101.  
  102. main(argc, argv)
  103.     char **argv;
  104. {
  105.     int i;
  106.  
  107.     /* GRR:  for OS/2 and emx+gcc, could use _wildcard() built-in */
  108.  
  109.     _ct_init("");
  110.     for (i=1; i<argc; i++)
  111.         expand(argv[i]);
  112.     if (nfiles)
  113.         printf("%s", files[0]);
  114.     for (i=1; i<nfiles; i++)
  115.     {
  116.         printf(" %s", files[i]);
  117.     }
  118.     putchar('\n');
  119.     return 0;
  120. }
  121.  
  122. #else
  123. char **wildexpand(argc, argv)
  124.     int *argc;
  125.     char **argv;
  126. {
  127.     int i;
  128.     
  129.     /* GRR:  for OS/2 and emx+gcc, could just use _wildcard() built-in */
  130.  
  131.     for (i=0; i<*argc; i++)
  132.         expand(argv[i]);
  133.     *argc=nfiles;
  134.     return files;
  135. }    
  136. #endif
  137.  
  138. #ifdef __TURBOC__
  139. int pstrcmp(const void *a, const void *b)
  140. #else
  141. int pstrcmp(a, b)
  142.     char **a, **b;
  143. #endif
  144. {
  145.     return strcmp(*(char **)a, *(char **)b);
  146. }
  147.  
  148. void expand(name)
  149.     char *name;
  150. {
  151.     char *filespec;
  152.     int wildcard=0;
  153. #if defined(M68000) || defined(__m68k__)
  154.     DMABUFFER findbuf;
  155. #else
  156.     struct ffblk findbuf;
  157. #endif
  158.     int err;
  159.     char buf[80];
  160.     int lastn;
  161.  
  162.     strcpy(buf, name);
  163.     for (filespec=buf; *filespec; filespec++)
  164.         ;
  165.  
  166.     while (--filespec>=buf)
  167.     {    if (*filespec=='?' || *filespec=='*')
  168.             wildcard=1;
  169.         if (*filespec=='/' || *filespec=='\\' || *filespec==':')
  170.             break;
  171.     }
  172.     if (!wildcard)
  173.         addfile(buf);
  174.     else
  175.     {
  176.         lastn=nfiles;
  177.         filespec++;
  178.         if ((err=findfirst(buf, &findbuf, 0))!=0)
  179.             addfile(buf);
  180.         while (!err)
  181.         {
  182.             strcpy(filespec, findbuf.ff_name);
  183.             addfile(buf);
  184.             err=findnext(&findbuf);
  185.         }
  186.         if (lastn!=nfiles)
  187.             qsort(files+lastn, nfiles-lastn, sizeof(char *), pstrcmp);
  188.     }
  189. }
  190.  
  191. #if MINT
  192. extern int __mint;
  193. #endif
  194. void addfile(buf)
  195.     char *buf;
  196. {
  197.     char *p;
  198.  
  199. #if MINT
  200.     /* there are filesystems on MiNT that are case sensitive... and for
  201.      * the vanilla GEMDOS fs MiNT already does this conversion itself.
  202.      */
  203.     if (!__mint)
  204. #endif
  205. #if !OS2
  206.     for (p=buf; *p; p++)
  207.         *p=tolower(*p);
  208. #endif
  209.     /* convert spaces to something else so not confused with delimiter */
  210.     for (p=buf; *p; p++)
  211.         if (*p == ' ')
  212.             *p = SPACEHOLDER;
  213.  
  214.     if (nfiles<MAXFILES && (files[nfiles]=calloc(strlen(buf)+1, 1))!=0)
  215.         strcpy(files[nfiles++], buf);
  216. }
  217.