/* Get filenames and sizes which match wildcard string */

#include <libraries/dos.h>
#include <ctype.h>

void ExtractPath();
extern int NoCase();
extern wildmat();
char name[30], path[50], filename[31];

struct FileInfoBlock fi;

FindFiles(filepath)

char filepath[80];

{
  long size, lock, total = 0;
  int bool, error;
  char tempstr[31];

  ExtractPath(filepath);
  lock = Lock(path, ACCESS_READ);
  if (!lock)
    {
      printf("Unable to lock; probably an invalid pathname.\n");
      exit(FALSE);
    }

  if (Examine(lock, &fi))
    do
      {
      /* If we're not at the end, and if it's not a directory...*/
      if ((*fi.fib_FileName != 0) && (fi.fib_DirEntryType < 0))
        {
        strcpy (tempstr, fi.fib_FileName);
        bool = wildmat(tempstr, name);
        if (bool > 0)
          {
          printf ("%-31s%7ld\n", fi.fib_FileName, fi.fib_Size);
          total += fi.fib_Size;
          }
        }
      }
    while (ExNext(lock, &fi));
  error = IoErr();
  if(error != ERROR_NO_MORE_ENTRIES)
    printf("Error %d occurred!\n", error);
  return (total);
}


void ExtractPath(pathname)

char pathname[80];

{
  int i, j = 0;
  i = strlen(pathname);
/* Extract filename */
  while (pathname[i-1] != '/' && pathname[i-1] != ':' && i != 0)
    name[j++] = pathname[--i];
  reverse(name);

/* Build pathname */
  j = 0;
  while (j < i)
    path[j++] = pathname[j];
}

reverse(s)
register char *s;
{
   register int c, i, j;

for(i=0, j = strlen(s) - 1; i < j; i++, j--)
  {
   c    = s[i];
   s[i] = s[j];
   s[j] = c;
  }
}
