home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / compress / filelist.c.old < prev    next >
Encoding:
Text File  |  1990-11-21  |  4.1 KB  |  195 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 *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((fib = AllocMem((LONG)sizeof(struct FileInfoBlock), MEMF_CLEAR)) != NULL)
  58.   {
  59.     if((fl = Lock(directory, ACCESS_READ)) != ZERO) /* Lock dir */
  60.     {
  61.       if((Examine(fl, fib)) != DOSFALSE)
  62.       {
  63.         if(fib->fib_DirEntryType < 0) /* File ? */
  64.         {
  65.           if((*patternmatch)(fib->fib_FileName, pattern) != 0)
  66.           {
  67.             p = xmalloc(sizeof(struct FileList)); /* Allocate list */
  68.  
  69.             p->fl_filename = xmalloc(strlen(directory) +
  70.                                      strlen(fib->fib_FileName) + 2);
  71.             (void)strcpy(p->fl_filename, directory);
  72.  
  73.             c = directory[strlen(directory) - 1];
  74.             if(c != ':'á&& c != '/')
  75.               strcat(p->fl_filename, "/");
  76.  
  77.             (void)strcat(p->fl_filename, fib->fib_FileName);
  78. #ifdef MYDEBUG
  79.             fprintf(stderr, "Filename: %s\n", p->fl_filename);
  80. #endif
  81.  
  82.             p->fl_filesize = fib->fib_Size;
  83.  
  84.             q->fl_next = p;
  85.             p->fl_next = NULL;
  86.             q = p;
  87.           }
  88.         }
  89.  
  90.         while((ExNext(fl, fib)) != DOSFALSE)
  91.         {
  92.           if(fib->fib_DirEntryType < 0) /* File ? */
  93.           {
  94.             if((*patternmatch)(fib->fib_FileName, pattern) != 0)
  95.             {
  96.               p = xmalloc(sizeof(struct FileList)); /* Allocate list */
  97.  
  98.               p->fl_filename = xmalloc(strlen(directory) +
  99.                                      strlen(fib->fib_FileName) + 2);
  100.               (void)strcpy(p->fl_filename, directory);
  101.  
  102.               c = directory[strlen(directory) - 1];
  103.               if(c != ':'á&& c != '/')
  104.                 strcat(p->fl_filename, "/");
  105.  
  106.               (void)strcat(p->fl_filename, fib->fib_FileName);
  107. #ifdef MYDEBUG
  108.             fprintf(stderr, "Filename: %s\n", p->fl_filename);
  109. #endif
  110.  
  111.               p->fl_filesize = fib->fib_Size;
  112.  
  113.               q->fl_next = p;
  114.               p->fl_next = NULL;
  115.               q = p;
  116.             }
  117.           }
  118.         }
  119.  
  120.         if(IoErr() != ERROR_NO_MORE_ENTRIES)
  121.         {
  122.           fprintf(stderr, "ExNext() failed\n");
  123.           error = 1;
  124.         }
  125.       }
  126.       else
  127.       {
  128.         fprintf(stderr, "Examine() failed\n");
  129.         error = 1;
  130.       }
  131.       UnLock(fl);
  132.     }
  133.     else
  134.     {
  135.       if(directory == NULL || *directory == '\0')
  136.         fprintf(stderr, "Can't lock current directory\n");
  137.       else
  138.         fprintf(stderr, "Can't lock directory '%s'\n", directory);
  139.       error = 1;
  140.     }
  141.  
  142.     FreeMem(fib, (LONG)sizeof(struct FileInfoBlock));
  143.   }
  144.   else
  145.   {
  146.     fprintf(stderr, "Insufficient free store\n");
  147.     exit(20);
  148.   }
  149.  
  150.   onbreak(NULL);
  151.  
  152.   if(error == 0)
  153.     return lsort(hlist.fl_next, 0, mycmp);
  154.   else
  155.     return NULL;
  156. }
  157.  
  158. void __stdargs Free_FileList(struct FileList *fl)
  159. {
  160.   struct FileList *p, *q;
  161.  
  162.   for(p = fl; p != NULL; )
  163.   {
  164.     free(p->fl_filename);
  165.     q = p;
  166.     p = p->fl_next;
  167.     free(q);
  168.   }
  169. }
  170.  
  171. int mycmp(struct FileList *a, struct FileList *b)
  172. {
  173.   return stricmp(a->fl_filename, b->fl_filename);
  174. }
  175.  
  176. int brk(void)
  177. {
  178.   return 0;
  179. }
  180.  
  181. void *__regargs xmalloc(int size)
  182. {
  183.   void *ptr;
  184.  
  185.   if((ptr = malloc(size)) != NULL)
  186.   {
  187.     return ptr;
  188.   }
  189.   else
  190.   {
  191.     fprintf(stderr, "Insufficient free store\n");
  192.     exit(20);
  193.   }
  194. }
  195.