home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / compress / filelist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-21  |  3.7 KB  |  185 lines

  1. /*
  2.  
  3.    filelist.c
  4.  
  5.    Examine a directory and return a sorted list of all files.
  6.  
  7.    Copyright (C) 1990 Ingo Feulner, Magic Soft Developments.
  8.  
  9. */
  10.  
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #include <exec/types.h>
  16. #include <exec/memory.h>
  17. #include <libraries/dos.h>
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20. #include "filelist.h"
  21.  
  22. #define ZERO 0  /* for BPTRs */
  23.  
  24.  
  25. extern void *lsort(void *, int, int (*)());
  26. void *__regargs xmalloc(int);
  27. int mycmp(struct FileList *, struct FileList *);
  28. int brk(void);
  29.  
  30.  
  31. struct FileList *__stdargs Get_FileList(char *directory, char *pattern, int flag)
  32. {
  33.   struct FileInfoBlock __aligned fib;
  34.   struct FileList hlist;     /* head of list */
  35.   struct FileList *q, *p;
  36.  
  37.   int (*patternmatch)(char *, char *);
  38.  
  39.   BPTR fl;
  40.   int error = 0;
  41.   char c;
  42.  
  43.   onbreak(brk);
  44.  
  45.   if(flag != PUNIX)
  46.     patternmatch = astcsma;
  47.   else
  48.     patternmatch = stcsma;
  49.  
  50.  
  51. #ifdef MYDEBUG
  52.   fprintf(stderr, "Directory: %s   Pattern: %s\n", directory, pattern);
  53. #endif
  54.  
  55.   q = &hlist;
  56.  
  57.   if((fl = Lock(directory, ACCESS_READ)) != ZERO) /* Lock dir */
  58.   {
  59.     if((Examine(fl, &fib)) != DOSFALSE)
  60.     {
  61.       if(fib.fib_DirEntryType < 0) /* File ? */
  62.       {
  63.         if((*patternmatch)(fib.fib_FileName, pattern) != 0)
  64.         {
  65.           p = xmalloc(sizeof(struct FileList)); /* Allocate list */
  66.  
  67.           p->fl_filename = xmalloc(strlen(directory) +
  68.                                    strlen(fib.fib_FileName) + 2);
  69.           (void)strcpy(p->fl_filename, directory);
  70.  
  71.           c = directory[strlen(directory) - 1];
  72.           if(c != ':'á&& c != '/')
  73.             strcat(p->fl_filename, "/");
  74.  
  75.           (void)strcat(p->fl_filename, fib.fib_FileName);
  76. #ifdef MYDEBUG
  77.           fprintf(stderr, "Filename: %s\n", p->fl_filename);
  78. #endif
  79.  
  80.           p->fl_filesize = fib.fib_Size;
  81.  
  82.           q->fl_next = p;
  83.           p->fl_next = NULL;
  84.           q = p;
  85.         }
  86.       }
  87.  
  88.       while((ExNext(fl, &fib)) != DOSFALSE)
  89.       {
  90.         if(fib.fib_DirEntryType < 0) /* File ? */
  91.         {
  92.           if((*patternmatch)(fib.fib_FileName, pattern) != 0)
  93.           {
  94.             p = xmalloc(sizeof(struct FileList)); /* Allocate list */
  95.  
  96.             p->fl_filename = xmalloc(strlen(directory) +
  97.                                      strlen(fib.fib_FileName) + 2);
  98.             (void)strcpy(p->fl_filename, directory);
  99.  
  100.             c = directory[strlen(directory) - 1];
  101.             if(c != ':'á&& c != '/')
  102.               strcat(p->fl_filename, "/");
  103.  
  104.             (void)strcat(p->fl_filename, fib.fib_FileName);
  105. #ifdef MYDEBUG
  106.             fprintf(stderr, "Filename: %s\n", p->fl_filename);
  107. #endif
  108.  
  109.             p->fl_filesize = fib.fib_Size;
  110.  
  111.             q->fl_next = p;
  112.             p->fl_next = NULL;
  113.             q = p;
  114.           }
  115.         }
  116.       }
  117.  
  118.       if(IoErr() != ERROR_NO_MORE_ENTRIES)
  119.       {
  120.         fprintf(stderr, "ExNext() failed\n");
  121.         error = 1;
  122.       }
  123.     }
  124.     else
  125.     {
  126.       fprintf(stderr, "Examine() failed\n");
  127.       error = 1;
  128.     }
  129.     UnLock(fl);
  130.   }
  131.   else
  132.   {
  133.     if(directory == NULL || *directory == '\0')
  134.       fprintf(stderr, "Can't lock current directory\n");
  135.     else
  136.       fprintf(stderr, "Can't lock directory '%s'\n", directory);
  137.     error = 1;
  138.   }
  139.  
  140.   onbreak(NULL);
  141.  
  142.   if(error == 0)
  143.     return lsort(hlist.fl_next, 0, mycmp);
  144.   else
  145.     return NULL;
  146. }
  147.  
  148. void __stdargs Free_FileList(struct FileList *fl)
  149. {
  150.   struct FileList *p, *q;
  151.  
  152.   for(p = fl; p != NULL; )
  153.   {
  154.     free(p->fl_filename);
  155.     q = p;
  156.     p = p->fl_next;
  157.     free(q);
  158.   }
  159. }
  160.  
  161. int mycmp(struct FileList *a, struct FileList *b)
  162. {
  163.   return stricmp(a->fl_filename, b->fl_filename);
  164. }
  165.  
  166. int brk(void)
  167. {
  168.   return 0;
  169. }
  170.  
  171. void *__regargs xmalloc(int size)
  172. {
  173.   void *ptr;
  174.  
  175.   if((ptr = malloc(size)) != NULL)
  176.   {
  177.     return ptr;
  178.   }
  179.   else
  180.   {
  181.     fprintf(stderr, "Insufficient free store\n");
  182.     exit(20);
  183.   }
  184. }
  185.