home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / contrib / zelk / src-zlib / zglob.c < prev   
Encoding:
C/C++ Source or Header  |  1992-10-17  |  2.7 KB  |  132 lines

  1. /* zglob.c zilla sep26 - filename match
  2.  * adapted in part from code in Oliver Laumann's Elk (unix.c),
  3.  * also calls Wildmat by Rich Salz (rsalz@bbn.com)
  4.  */
  5.  
  6. #include <theusual.h>
  7. #include <sys/types.h>
  8. /*#include <sys/stat.h>*/
  9. /*#include <errno.h>*/
  10.  
  11. #if Eansiincludes
  12. #  include <dirent.h>
  13. #else
  14. #  include <sys/dir.h>
  15. #endif
  16.  
  17. /* define this if you do not want . and .. included in the matches */
  18. #define EXCLUDEDOTS
  19.  
  20. #define DOT '.'
  21.  
  22. int Zreaddir(spec,buffer,maxitems) 
  23.   char *spec,**buffer;
  24.   int maxitems;
  25. {
  26.     register DIR *d;
  27. #if Eansiincludes
  28.     register struct dirent *dp;
  29. #else
  30.     register struct direct *dp;
  31. #endif
  32.     register char *dir;
  33.     int n;
  34.  
  35.     dir = Zpathgetpath(spec);
  36.     if (str_len(dir)==0) dir = ".";
  37.  
  38.     if ((d = opendir (dir)) == NULL)
  39.       Zfail("cannot open directory %s", spec);
  40.  
  41.     n = 0;
  42.     while ((dp = readdir (d)) != NULL) {
  43.       register int len = str_len(dp->d_name);
  44.  
  45. #ifdef EXCLUDEDOTS
  46.       if ((dp->d_name[0] == DOT) &&
  47.           ((len == 1)
  48.            || ((len == 2) && (dp->d_name[1] == DOT))))
  49.         continue;
  50. #endif
  51.       buffer[n] = malloc(len+1);
  52.       Zbcopy(dp->d_name,buffer[n],len);
  53.       (buffer[n])[len] = (char)0;
  54.       n++;
  55.       if (n == maxitems) {
  56.         Zwarning("Zglob: name buffer full\n");
  57.         break;
  58.       }
  59.     }
  60.  
  61.     closedir (d);
  62.     return n;
  63. } /*zreaddir*/
  64.  
  65.  
  66.  
  67. int Zglob(spec,buffer,maxitems) 
  68.   char *spec,**buffer;
  69.   int maxitems;
  70. {
  71.     register DIR *d;
  72. #if Eansiincludes
  73.     register struct dirent *dp;
  74. #else
  75.     register struct direct *dp;
  76. #endif
  77.     register char *dir;
  78.     int n;
  79.  
  80.     dir = Zpathgetpath(spec);
  81.     if (str_len(dir)==0) dir = ".";
  82.  
  83.     if ((d = opendir (dir)) == NULL)
  84.       Zfail("cannot open directory %s", spec);
  85.  
  86.     n = 0;
  87.     while ((dp = readdir (d)) != NULL) {
  88.       register int len = str_len(dp->d_name);
  89.  
  90. #ifdef EXCLUDEDOTS
  91.       if ((dp->d_name[0] == DOT) &&
  92.           ((len == 1)
  93.            || ((len == 2) && (dp->d_name[1] == DOT))))
  94.         continue;
  95. #endif
  96.  
  97.       if (wildmat(dp->d_name,spec)) {
  98.         buffer[n] = malloc(len+1);
  99.         Zbcopy(dp->d_name,buffer[n],len);
  100.         (buffer[n])[len] = (char)0;
  101.         n++;
  102.         if (n == maxitems) {
  103.           Zwarning("Zglob: name buffer full\n");
  104.           break;
  105.         }
  106.       }/*wildmat match*/
  107.  
  108.     }/*readdir loop*/
  109.  
  110.     closedir (d);
  111.     return n;
  112. } /*zglob*/
  113.  
  114.  
  115. #ifdef TESTIT /*%%%%%%%%%%%%%%%%*/
  116. /*% cc -DTESTIT % wildmat.c -lZ -o zglobTST %*/
  117. main(ac,av) 
  118. int ac; char *av[];
  119. {
  120. # define Nmatches 1000
  121.   char *matches[Nmatches];
  122.   int i,n;
  123.  
  124.   if (ac != 2) Zquit(1,"zglobTST <path>\n");
  125.   n = Zglob(av[1],matches,Nmatches);
  126.   printf("found %d files\n",n);
  127.   for( i=0; i < n; i++ )
  128.     printf("%d: %s\n",i,matches[i]);
  129. }
  130.  
  131. #endif /*TESTIT%%%%%%%%%%%%%%%%*/
  132.