home *** CD-ROM | disk | FTP | other *** search
- /*
-
- filelist.c
-
- Examine a directory and return a sorted list of all files.
-
- Copyright (C) 1990 Ingo Feulner, Magic Soft Developments.
-
- */
-
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <libraries/dos.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include "filelist.h"
-
- #define ZERO 0 /* for BPTRs */
-
-
- extern void *lsort(void *, int, int (*)());
- void *__regargs xmalloc(int);
- int mycmp(struct FileList *, struct FileList *);
- int brk(void);
-
-
- struct FileList *__stdargs Get_FileList(char *directory, char *pattern, int flag)
- {
- struct FileInfoBlock *fib;
- struct FileList hlist; /* head of list */
- struct FileList *q, *p;
-
- int (*patternmatch)(char *, char *);
-
- BPTR fl;
- int error = 0;
- char c;
-
- onbreak(brk);
-
- if(flag != PUNIX)
- patternmatch = astcsma;
- else
- patternmatch = stcsma;
-
-
- #ifdef MYDEBUG
- fprintf(stderr, "Directory: %s Pattern: %s\n", directory, pattern);
- #endif
-
- q = &hlist;
-
- if((fib = AllocMem((LONG)sizeof(struct FileInfoBlock), MEMF_CLEAR)) != NULL)
- {
- if((fl = Lock(directory, ACCESS_READ)) != ZERO) /* Lock dir */
- {
- if((Examine(fl, fib)) != DOSFALSE)
- {
- if(fib->fib_DirEntryType < 0) /* File ? */
- {
- if((*patternmatch)(fib->fib_FileName, pattern) != 0)
- {
- p = xmalloc(sizeof(struct FileList)); /* Allocate list */
-
- p->fl_filename = xmalloc(strlen(directory) +
- strlen(fib->fib_FileName) + 2);
- (void)strcpy(p->fl_filename, directory);
-
- c = directory[strlen(directory) - 1];
- if(c != ':'á&& c != '/')
- strcat(p->fl_filename, "/");
-
- (void)strcat(p->fl_filename, fib->fib_FileName);
- #ifdef MYDEBUG
- fprintf(stderr, "Filename: %s\n", p->fl_filename);
- #endif
-
- p->fl_filesize = fib->fib_Size;
-
- q->fl_next = p;
- p->fl_next = NULL;
- q = p;
- }
- }
-
- while((ExNext(fl, fib)) != DOSFALSE)
- {
- if(fib->fib_DirEntryType < 0) /* File ? */
- {
- if((*patternmatch)(fib->fib_FileName, pattern) != 0)
- {
- p = xmalloc(sizeof(struct FileList)); /* Allocate list */
-
- p->fl_filename = xmalloc(strlen(directory) +
- strlen(fib->fib_FileName) + 2);
- (void)strcpy(p->fl_filename, directory);
-
- c = directory[strlen(directory) - 1];
- if(c != ':'á&& c != '/')
- strcat(p->fl_filename, "/");
-
- (void)strcat(p->fl_filename, fib->fib_FileName);
- #ifdef MYDEBUG
- fprintf(stderr, "Filename: %s\n", p->fl_filename);
- #endif
-
- p->fl_filesize = fib->fib_Size;
-
- q->fl_next = p;
- p->fl_next = NULL;
- q = p;
- }
- }
- }
-
- if(IoErr() != ERROR_NO_MORE_ENTRIES)
- {
- fprintf(stderr, "ExNext() failed\n");
- error = 1;
- }
- }
- else
- {
- fprintf(stderr, "Examine() failed\n");
- error = 1;
- }
- UnLock(fl);
- }
- else
- {
- if(directory == NULL || *directory == '\0')
- fprintf(stderr, "Can't lock current directory\n");
- else
- fprintf(stderr, "Can't lock directory '%s'\n", directory);
- error = 1;
- }
-
- FreeMem(fib, (LONG)sizeof(struct FileInfoBlock));
- }
- else
- {
- fprintf(stderr, "Insufficient free store\n");
- exit(20);
- }
-
- onbreak(NULL);
-
- if(error == 0)
- return lsort(hlist.fl_next, 0, mycmp);
- else
- return NULL;
- }
-
- void __stdargs Free_FileList(struct FileList *fl)
- {
- struct FileList *p, *q;
-
- for(p = fl; p != NULL; )
- {
- free(p->fl_filename);
- q = p;
- p = p->fl_next;
- free(q);
- }
- }
-
- int mycmp(struct FileList *a, struct FileList *b)
- {
- return stricmp(a->fl_filename, b->fl_filename);
- }
-
- int brk(void)
- {
- return 0;
- }
-
- void *__regargs xmalloc(int size)
- {
- void *ptr;
-
- if((ptr = malloc(size)) != NULL)
- {
- return ptr;
- }
- else
- {
- fprintf(stderr, "Insufficient free store\n");
- exit(20);
- }
- }
-