home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / MICROC.ZIP / FFF.C < prev    next >
C/C++ Source or Header  |  1994-01-01  |  6KB  |  256 lines

  1. /*
  2.  
  3.   FFF.C "Fast File Find" (C) 1991 John Hall
  4.   Original code by: John Hall
  5.   Ported to MICRO-C by: M. "Hannibal" Toal
  6.  
  7.   This program is free software; you can redistribute it and/or modify
  8.   it under the terms of the GNU General Public License as published by
  9.   the Free Software Foundation; either version 2 of the License, or
  10.   (at your option) any later version.
  11.  
  12.   This program is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License
  18.   along with this program; if not, write to the Free Software
  19.   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.   Changes:
  22.  
  23.   JH    1991            Original Program as received from James Hall.
  24.   MT    April 1995      Ported to MICRO-C.
  25.             Eliminated external function getinfo().
  26.             Some constant values changed from Decimal to Hex.
  27.             Attempt made at duplicating fnsplit () function.
  28.             Added usage, getopt, and pagel freedos functions.
  29.             Aesthetic changes to make like other Free-DOS progs.
  30. */
  31.  
  32. #include <stdio.h>
  33. #include "freedos.h"        /* Free-DOS header file and functions */
  34. #include "getopt.c"        /* for parsing options */
  35. #include "fsplit.c"        /* to split file names into components */
  36. #include "pagel.c"        /* page by single output line */
  37.  
  38. /* extern int _AX_, _CX_, _DX_, Cflags; */
  39.  
  40. /* Symbolic constants */
  41. #define FIRST    0x4e00
  42. #define NEXT     0x4f00
  43. #define FILE     6
  44. #define DIR      16
  45. #define FALSE    0
  46. #define TRUE     ! FALSE
  47. #define MAXLINES 24
  48. #define MAXARRAY 256
  49.  
  50. /* Function prototypes */
  51. char *get_dir ( char *drive, char *dir );       /* Builds full search dir  */
  52. char *get_name ( char *fname, char *ext );      /* Builds full search spec */
  53. void depth_search( char *dir, char *name );     /* Recursively searches    */
  54. int search( char *fname, int flag, int type );  /* Search for filespec     */
  55. void set_dta( char *ptr );                      /* Sets DTA                */
  56. void display( char *dir, char *dta, int flag ); /* Prints file info        */
  57. void print( char *string );                     /* Prints a line           */
  58. void quit( int error );                         /* Handles errors          */
  59.  
  60. int fcount, PAGE;                /* line page count */
  61.  
  62. main( int argc, char *argv[] )
  63.  
  64. {
  65.     char drive[MAXARRAY];   /* Drive to search              */
  66.     char dir[MAXARRAY];     /* Directory to start from      */
  67.     char fname[MAXARRAY];   /* File name to search for      */
  68.     char ext[6];            /* File extension to search for */
  69.     int i;                  /* Loop control variable        */
  70.  
  71.     LINECOUNT = 1;
  72.     PAGE = FALSE;
  73.  
  74.     /* Parse to see if there are any switches */
  75.     while ((i = getopt (argc, argv, "P?", NULL)) != EOF)
  76.         {
  77.         switch (i)
  78.             {
  79.             case 'P':
  80.                 PAGE = TRUE;
  81.                 break;
  82.             default:
  83.                 usage ();
  84.             }
  85.         };
  86.  
  87.     /* Check for no arguments on the command line */
  88.     if( argc == 1 )
  89.         quit( 1 );
  90.  
  91.     printf ("Fast File Find: ");
  92.     for (i = optind; i < argc; i++)
  93.         printf ("%s ", argv[i]);
  94.  
  95.     /* Search for files */
  96.     for( i = optind; i < argc; i++)
  97.         {
  98.         /* Split apart pathspec */
  99.         strupr (argv[i]);
  100.         fnsplit (argv[i], drive, dir, fname, ext);
  101.  
  102.         /* Search for file */
  103.         depth_search (get_dir (drive, dir), get_name (fname, ext));
  104.         }
  105.  
  106.     /* Return to DOS */
  107.     quit( 0 );
  108. }
  109.  
  110. char *get_dir( char *drive, char *dir )
  111. {
  112.     /* Test for empty drive spec */
  113.     if(!drive[0]) 
  114.         sprintf (drive, "%c%c\0", get_drive() + 'A', ':' );
  115.  
  116.     /* Test for empty directory */
  117.     if(!dir[0])
  118.         strcpy (dir, "\\");
  119.  
  120.     /* Remove trailing backslash from directory */
  121.     if (dir[strlen(dir)-1] == '\\')
  122.         dir [strlen(dir)-1] = '\0';
  123.  
  124.     /* Return full directory name */
  125.     strcat (drive, dir);
  126.  
  127.     return drive;
  128. }
  129.  
  130. char *get_name( char *fname, char *ext )
  131. {
  132.  
  133.     /* Test for empty extension */
  134.     if (!ext[0])
  135.         strcpy( ext, ".*" );
  136.  
  137.     /* Add wildcards to filespec and return result */
  138.     if (!fname[0])
  139.         strcpy (fname, "*");
  140.  
  141.     strcat (fname, ext);
  142.     return fname;
  143. }
  144.  
  145. void depth_search( char *dir, char *name )
  146. {
  147.     char filename[256];     /* File name to search for  */
  148.     char dirname[256];      /* Directory name to search */
  149.     char dta[43];           /* Disk transfer area       */
  150.     int flag;               /* Search type flag         */
  151.  
  152.     /* Create a filespec */
  153.     sprintf( filename, "%s\\%s", dir, name );
  154.  
  155.     /* Set the DTA to the local DTA buffer */
  156.     set_dta( dta );
  157.  
  158.     /* Search for the file */
  159.     flag = FIRST;
  160.     while( search( filename, flag, FILE ) ) {
  161.         display( dir, dta, flag );
  162.         flag = NEXT;
  163.     }
  164.  
  165.     /* Create a pathspec */
  166.     sprintf( filename, "%s\\*.", dir );
  167.  
  168.     /* Search for a subdirectory */
  169.     flag = FIRST;
  170.     while( search( filename, flag, DIR ) ) {
  171.         flag = NEXT;
  172.  
  173.         /* Specifically exclude "." and ".." fron search */
  174.         if( strcmp( ".", dta+30 ) && strcmp( "..", dta+30 ) ) {
  175.             sprintf( dirname, "%s\\%s", dir, dta+30 );
  176.             depth_search( dirname, name );
  177.         }
  178.  
  179.         /* Return to local DTA buffer for next directory */
  180.         set_dta( dta );
  181.     }
  182. }
  183.  
  184.  
  185. int search( char *fname, int flag, int type )
  186. {
  187.  
  188.     /* Set up calling registers */
  189.     _AX_ = flag;
  190.     _CX_ = type;
  191.     _DX_ = fname;
  192.     if (int86(0x21) & 0x01)
  193.         return FALSE;
  194.     else
  195.         return TRUE;
  196. }
  197.  
  198. void set_dta( char *ptr )
  199. {
  200.     /* Set up calling registers */
  201.     _AX_ = 0x1A00;
  202.     _DX_ = ptr;
  203.     int86 (0x21);
  204. }
  205.  
  206. void display( char *dir, char *dta, int flag )
  207. {
  208.     /* Test for new directory */
  209.     if( flag == FIRST ) {
  210.         /* Print directory name */
  211.         print( "" );
  212.         print( dir );
  213.     }
  214.  
  215.     /* Print file name and info */
  216.     print (dta+0x1E);
  217.  
  218.     /* Increment number of files found */
  219.     fcount++;
  220. }
  221.  
  222. void print( char *string )
  223. {
  224.     /* Paginate display */
  225.     if (PAGE == TRUE)
  226.         pagel (string);
  227.     else
  228.         printf ("%s\n", string);
  229. }
  230.  
  231. void quit( int error )
  232. {
  233.     switch( error )
  234.     {
  235.         case 1:
  236.             usage ();
  237.             break;
  238.         default:
  239.             /* Print total number of files found */
  240.             printf( "\n%d files found.\n", fcount );
  241.     }
  242.  
  243.     /* Exit to DOS with return code */
  244.     exit( error );
  245. }
  246.  
  247. void usage (void)
  248.  
  249.     {
  250.     printp ("FFF", "Fast File Find");
  251.     printc ("1991", "John Hall and RVS Utilities");
  252.     printu ("FFF", "[/P] [filespec..]");
  253.     printo ("P", "Paginate output");
  254.     exit (0);
  255.     }
  256.