home *** CD-ROM | disk | FTP | other *** search
- /*
- * WildDemo2.c - Search filesystem for patterns, and separate into directories
- * and files, sorting each separately using DA lists.
- *
- * -+=SDB=+-
- *
- * Copyright (c) 1987, Scott Ballantyne
- * Use and abuse as you please.
- *
- * Manx:
- * cc WildDemo2.c
- * ln WildDemo2.o -larp -lc
- *
- * Lattice:
- * lc WildDemo2.c
- * blink lib:c.o WildDemo2.o to WildDemo2 lib lib:arp.lib lib:lc.lib lib:amiga.lib
- */
-
- #include <exec/types.h>
- #include "libraries/arpbase.h"
- #include <libraries/dos.h>
- #include "arpfunctions.h"
-
-
- struct DirectoryEntry *FileList = NULL; /* Head of DAList */
-
- /* Our secondary keys for sorting the list, we want the directories to bunch
- * up at the beginning.
- */
-
- #define DIRECTORY 0L
- #define FILE 1L
-
- /* Our templates and help strings */
- char *CLI_Template = "Pattern";
- char *CLI_Help = "List Files or directories by wildcard";
-
- BOOL lflag; /* Cosmetics */
-
- VOID columnize();
-
- VOID main(argc, argv)
- int argc;
- char **argv;
- {
- struct AnchorPath *Anchor;
- register struct DirectoryEntry *de; /* For later */
- register LONG fcount = 0, dcount = 0;
- LONG key;
- char *pat;
- LONG Result;
-
- if (argc < 2)
- pat = "*"; /* All files/dirs in current directory */
- else
- pat = argv[1];
-
- /* Get our AnchorBase */
-
- if ( Anchor = (struct AnchorPath *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
- {
- Anchor->ap_Length = 0;
- Anchor->ap_BreakBits |= SIGBREAKF_CTRL_C;
- }
- else
- {
- Puts("No memory!");
- exit(20);
- }
-
- Result = FindFirst( pat, Anchor);
-
- while ( Result == 0 )
- {
-
- if (Anchor->ap_Info.fib_DirEntryType >= 0)
- {
- key = DIRECTORY;
- dcount++;
- }
- else
- {
- key = FILE;
- fcount++;
- }
-
- if ( !AddDANode( Anchor->ap_Info.fib_FileName, &FileList, 0L, key))
- {
- Puts("Out of memory!");
- FreeDAList( FileList );
- exit(20);
- }
- Result = FindNext( Anchor );
- }
-
- /* Free the Anchor chain built by the above functions */
- FreeAnchorChain( Anchor );
-
- if (Result == ERROR_BREAK) /* Control 'C' */
- Puts("***Break");
- else if (Result == ERROR_OBJECT_NOT_FOUND)
- {
- Printf("Can't find %s\n", pat);
- exit(20);
- }
- else if (Result != ERROR_NO_MORE_ENTRIES)
- {
- Printf("ERROR #%ld\n", Result);
- exit(20);
- }
-
- /* Now munge through the DA list, displaying the sorted files */
-
- de = FileList;
- Printf("%ld Directories:", dcount);
-
- if (de->de_Type == DIRECTORY) /* While we have directories */
- {
- lflag = 1;
- for ( ; de ; de = de->de_Next)
- {
- if (de->de_Type != DIRECTORY)
- break;
- columnize(de->de_Name);
- }
- }
- /* And now do the files */
- Printf("\n%ld Files:", fcount);
- if ( de ) /* Still more entries */
- {
- lflag = 1;
- for ( ; de ; de = de->de_Next)
- columnize(de->de_Name);
- }
- Puts("");
- FreeDAList( FileList );
- }
-
- /* Simple routine to print a file or directory name in 2 columns */
-
- VOID columnize(name)
- char *name;
- {
-
- if (lflag)
- Printf("\n\t");
- Printf("%-32s", name);
- lflag = !lflag;
- }
-