home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / LharcUUCP1_0.lha / LharcUUCP / source / ScanDir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  4.6 KB  |  146 lines

  1. /*
  2.  *      $Filename: ScanDir.c $
  3.  *      $Revision: 1.1 $
  4.  *      $Date: 1994/04/06 14:47:13 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: ScanDir.c,v 1.1 1994/04/06 14:47:13 simons Exp simons $
  23.  *
  24.  */
  25.  
  26. /************************************* includes ***********/
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <dos/dos.h>
  30. #include <dos/exall.h>
  31. #include <proto/dos.h>
  32.  
  33. /************************************* defines ************/
  34. char **ScanDir(char *, char *);
  35.  
  36. /************************************* global variables ***/
  37. static const char __RCSId[] = "$Id: ScanDir.c,v 1.1 1994/04/06 14:47:13 simons Exp simons $";
  38.  
  39. static char **namearray;
  40. static int namepointer;
  41.  
  42. /************************************* subroutines ********/
  43. int InitDirScan(char *pattern)
  44. {
  45.         namepointer = 0;
  46.  
  47.         if (namearray = ScanDir(NULL, pattern))
  48.                 return TRUE;
  49.         else
  50.                 return FALSE;
  51. }
  52.  
  53. char *ScanNextFile(void)
  54. {
  55.         if (namearray[namepointer])
  56.                 return namearray[namepointer++];
  57.         else
  58.                 return NULL;
  59. }
  60.  
  61. void EndDirScan(void)
  62. {
  63.         int i;
  64.  
  65.         if (!namearray)
  66.                 return;
  67.  
  68.         for (i = 0; namearray[i]; i++)
  69.                 free(namearray[i]);
  70.         free(namearray);
  71.  
  72.         namearray = NULL;
  73. }
  74.  
  75.  /*
  76.   * ScanDir() reads all entries in the specified directory and returns those
  77.   * matching the provided pattern. A pattern of NULL means that all files
  78.   * are returned. If the directory-parameter is NULL, the current directory
  79.   * will be scanned.
  80.   *
  81.   * PORT NOTE: Sorry, this routine is very AmigaOS specific and will not
  82.   *            work on other platforms. However, it should be pretty easy
  83.   *            to re-write.
  84.   */
  85.  
  86. char **ScanDir(char *directory, char *pattern)
  87. {
  88.         struct ExAllControl *eac;
  89.         struct ExAllData *ead;
  90.         BPTR dir_lock;
  91.         char **filenames = NULL, *parsed_pattern = NULL, *buffer;
  92.         int i = 0, cont;
  93.  
  94.         if (!(buffer = malloc(10 * 1024)))
  95.                 return NULL;
  96.  
  97.         if (!(filenames = malloc(sizeof(char *) * 4096))) {
  98.                 free(buffer);
  99.                 return NULL;
  100.         }
  101.  
  102.         if (pattern)
  103.                 if (parsed_pattern = malloc(strlen(pattern) + 256)) {
  104.                         if (ParsePatternNoCase(pattern, parsed_pattern, strlen(pattern) + 256) == -1L) {
  105.                                 free(buffer);
  106.                                 free(filenames);
  107.                                 free(parsed_pattern);
  108.                                 return NULL;
  109.                         }
  110.                 }
  111.                 else
  112.                         return NULL;
  113.  
  114.         if (dir_lock = Lock(directory ? directory : "", ACCESS_READ)) {
  115.                 if (eac = AllocDosObject(DOS_EXALLCONTROL, NULL)) {
  116.                         if (parsed_pattern)
  117.                                 eac->eac_MatchString = parsed_pattern;
  118.  
  119.                         do {
  120.                                 cont = ExAll(dir_lock, (struct ExAllData *) buffer, 10 * 1024, ED_NAME, eac);
  121.                                 ead = (struct ExAllData *) buffer;
  122.                                 if (eac->eac_Entries > 0) {
  123.                                         do {
  124.                                                 filenames[i++] = strdup(ead->ed_Name);
  125.                                                 ead = ead->ed_Next;
  126.                                         } while (ead);
  127.                                 }
  128.                         } while (cont);
  129.                         filenames[i] = NULL;
  130.  
  131.                         FreeDosObject(DOS_EXALLCONTROL, eac);
  132.                 }
  133.                 UnLock(dir_lock);
  134.         }
  135.  
  136.         if (parsed_pattern)
  137.                 free(parsed_pattern);
  138.         free(buffer);
  139.         if (*filenames)
  140.                 return filenames;
  141.         else {
  142.                 free(filenames);
  143.                 return NULL;
  144.         }
  145. }
  146.