home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / WILDFILE / WD.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  4KB  |  155 lines

  1. /*
  2.  EPSHeader
  3.  
  4.    File: wd.c
  5.    Author: J. Kercheval
  6.    Created: Sun, 03/31/1991  14:59:48
  7. */
  8. /*
  9.  EPSRevision History
  10.  
  11.    J. Kercheval  Sat, 04/20/1991  20:32:50  create wd (Wild Directory)
  12.    J. Kercheval  Sat, 04/20/1991  21:24:34  This is V1.13 of WD
  13. */
  14.  
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #include "wildfile.h"
  20.  
  21. #ifndef BOOLEAN
  22. # define BOOLEAN int
  23. # define TRUE 1
  24. # define FALSE 0
  25. #endif
  26.  
  27. #ifdef __TURBOC__
  28. # define _splitpath fnsplit
  29. #endif
  30.  
  31. #define Author "J. Kercheval"
  32. #define Version "Wild Card Simple Directory Lister"
  33.  
  34.  
  35. /*----------------------------------------------------------------------------
  36. *
  37. *  Print Usage
  38. *
  39. ----------------------------------------------------------------------------*/
  40.  
  41. void Usage( char *progname )
  42. {
  43.     char drive[5],dir[255],fname[10],ext[5];
  44.  
  45.     _splitpath( progname,drive,dir,fname,ext );
  46.     fprintf( stderr,"\n%s -- %s\n\n", Version, Author );
  47.     fprintf( stderr,
  48.       "Usage: %s [ -h | -? ] | [{FILENAME}] \n\n",fname );
  49.     fprintf( stderr,
  50.       "  Used to list files using *IX shell type globbing (Wild Cards).\n" );
  51.     fprintf( stderr,
  52.       "  Multiple filenames may be listed on the command line.  You may\n" );
  53.     fprintf( stderr,
  54.       "  use either \\ or / as directory delimiters.  If you wish to\n" );
  55.     fprintf( stderr,
  56.       "  match a / or a \\ then they must be literally escaped within a\n" );
  57.     fprintf( stderr,
  58.       "  range ([..]) construct. (ie. [\]] would match ']').  The allowed\n");
  59.     fprintf( stderr,
  60.       "  special characters for wild card matching are:\n" );
  61.     fprintf( stderr,
  62.       "   `*' matches any sequence of characters (zero or more)\n" );
  63.     fprintf( stderr,
  64.       "   `?' matches any character\n" );
  65.     fprintf( stderr,
  66.       "   [SET] matches any character in the specified set\n" );
  67.     fprintf( stderr,
  68.       "   [!SET] or [^SET] matches any character not in the specified set\n" );
  69.     fprintf( stderr,
  70.       "   \\ is allowed within a set to escape a character like ']' or '-'\n\n" );
  71.     fprintf( stderr, "   ie. %s\n", fname );
  72.     fprintf( stderr, "   ie. %s t*s*t\n", fname );
  73.     fprintf( stderr, "   ie. %s *t \\*.bat\n", fname );
  74.     fprintf( stderr, "   ie. %s \\bin\\[a-e]?[!t]\n", fname );
  75.     fprintf( stderr, "   ie. %s *f*.[bde-hxyz]*\n\n", fname );
  76.     exit( 1 );
  77. }
  78.  
  79.  
  80. /*----------------------------------------------------------------------------
  81. *
  82. *  wd displays all files matching the passed pathname in single column 
  83. *  similar to the DOS DIR command.
  84. *
  85. ----------------------------------------------------------------------------*/
  86.  
  87. void wd( char *pathname )
  88. {
  89.     struct file_info_struct ff; /* the file find structure */
  90.  
  91.     /* initialize ff */
  92.     strcpy( ff.file_pattern,pathname );
  93.     ff.file_attributes = _FA_NORMAL | _FA_READONLY | _FA_ARCHIVE |
  94.                          _FA_HIDDEN | _FA_SYSTEM   | _FA_DIRECTORY;
  95.  
  96.     /* print path to output */      
  97.     fprintf( stdout,"\nFile(s) matching \"%s\"\n\n",pathname );
  98.     
  99.     /* find the initial file matching pattern */
  100.     if ( find_firstfile( &ff ) ) {
  101.         
  102.         /* loop through all matching files in the parameter */
  103.         do {
  104.  
  105.             /* print the info */
  106.             fprintf( stdout,"%s\n", ff.file.name );
  107.  
  108.         } while ( find_nextfile( &ff ) );
  109.     }
  110. }
  111.  
  112.  
  113. /*----------------------------------------------------------------------------
  114. *
  115. *  main loops through the parameter list and calls wd
  116. *
  117. ----------------------------------------------------------------------------*/
  118.  
  119. int main( int argc, char *argv[] )
  120. {
  121.  
  122.     /* if not enough parameters than Usage() */
  123.     if ( argc < 2 ) {
  124.         wd("*");
  125.         exit(0);
  126.     }
  127.  
  128.     /* enter the main loop */
  129.     for ( argc--,argv++; argc; argc--,argv++ ) {
  130.  
  131.         /* parse the argument list */
  132.         switch ( argv[0][0] ) {
  133.  
  134.             case '-':
  135.                 switch ( argv[0][1] ) {
  136.                     case 'h':
  137.                     case 'H':
  138.                     case '?':
  139.  
  140.                         Usage( (--argv)[0] );
  141.                         break;
  142.                 }
  143.                 break;
  144.  
  145.             /* this is a file parameter */
  146.             default:
  147.                 wd( *argv );
  148.                 break;
  149.         }
  150.     }
  151.  
  152.     /* successful exit */
  153.     return( 0 );
  154. }
  155.