#include <libraries/dosextens.h>
#include <stdio.h>

#ifndef AZTEC_C
#include <string.h>
#include <stdlib.h>
#include <proto/dos.h>
#include <proto/exec.h>
#else
#include <functions.h>
#endif

#define BELL 0x07 /* Lampeggio schermo */
#define MAXSUB 50 /* Massimo numero subslot */

BOOL trovato; /* Globale booleana di file trovato /*

/* Funzione di termine e rientro pulito */
void die( l )
LONG l;
{
 if ( !trovato ) printf( "Non l'ho trovato!\n" );
 exit( l );
}

void scan_directory( searchfile, path )
char * searchfile; char * path;
{
 struct FileInfoBlock *fb;
 BPTR dir;
 char *subdir [ MAXSUB ]; /* Puntatore area  Subdirectories */
 char pathname[ 130 ]; /* Puntatori subdirectories AmigaDOS*/
 UBYTE countdir, indexdir, i;
 char testname[ 32 ], *ch, *adddir();

 if ( trovato ) return;
 
 /* Ottiene il lock della directory */
 if ( ( dir = (BPTR) Lock ( path, ACCESS_READ ) ) == NULL )
   {
   printf ( "%cImpossibile lock %s\n", BELL, path );
   die( 4L );
   }
/* Examine() del lock e lettura  FileInfoBlock */
 fb = (struct FileInfoBlock *) \
            AllocMem( sizeof( struct FileInfoBlock ), 0 );

 if ( ! Examine ( dir, fb ) )
   {
   printf ( "%cImpossibile esaminare Lock \n", BELL );
   UnLock ( (BPTR) dir );
   FreeMem ( (char *) fb, sizeof( struct FileInfoBlock ) );
   die( 4L );
   }
   /* dir e fb a posto, scandiamo ricordando subdirectories */

   countdir = 0;
   while( ( ExNext( dir, fb ),IoErr() != ERROR_NO_MORE_ENTRIES )\
                               && ( countdir <= MAXSUB )\
                               && ( !trovato ) )
      {
      if ( fb -> fib_DirEntryType > 0 )
        { /* Abbiamo una subdirectory */
        if ( countdir < MAXSUB )
          subdir[ countdir ] = adddir( fb -> fib_FileName );
        else
          printf ( "%cTroppi subslot!\n", BELL );
        countdir++; 
        }
      else
         {
   /* Compara fb->filename con file cercato */
         for ( i = 0 ; i < 32 ; i++ )
           testname[ i ] = '\0';
         strncpy ( testname, fb->fib_FileName, 32 );
         testname[ 31 ] = '\0';
         for (i = 0, ch=testname; ( ch != '\0' ) && ( i<32 ); 
              i++, ch++ )
         *ch = toupper ( *ch );
         if ( strcmp ( testname, searchfile ) == 0 )
           {
           printf ( "%s\t%s\n", testname, path );
           trovato = TRUE;
           }
         }
      } /* fine while */

 UnLock ( (BPTR) dir );
 FreeMem ( (char *) fb, sizeof( struct FileInfoBlock ) );

/* Finito a questo livello, scendiamo di uno */

 indexdir = 0;
 while ( ( indexdir < countdir ) && ( !trovato ) )
   {
   strcpy ( pathname, path );

   /* Se non c'e' delimitatore di path, aggiungiamolo */

   if ( ( path[ strlen( path ) - 1 ] != ':' ) \
           && ( path[ strlen( path ) - 1 ] != '/' ) \
           && ( strlen( path ) > 0 ) )
     strcat( pathname, "/" );
     strcat( pathname, subdir[ indexdir ] );

     scan_directory( searchfile, pathname );
     /* E prendiamo lettera  lasciata da adddir() */
     FreeMem ( (char *) subdir[ indexdir ],
     strlen( subdir[ indexdir ] ) +1 );
     subdir[ indexdir ] = NULL;
     indexdir++;
   }
   return;
}

/* Alloca memoria e vi salva la stringa */

char *adddir (string)
char *string;
{
 char * nameadd;    /* Indirizzo nome della directory */

 nameadd = (char *) AllocMem ( strlen( string ) +1, 0 );
 if ( nameadd == NULL )
   {
   printf( "Non c'e' abbastanza memoria per le directory!\n" );
   die( 7L );
   }
 strcpy( nameadd, string );

 /* Ritorna indirizzo stringa allocata */
 return( nameadd );
}

void main( argc, argv )
int argc; char * argv[];
{
 BPTR dir;
 char path[ 130 ];   /* nome del volume */
 char devname[ 32 ], searchfile[ 32 ], *ch;
 UBYTE i;

 if ( ( argc > 3 ) || ( argc == 1 ) )
   printf ( "Uso: Find <nomefile> <nomedev>\n" );
 else
   {
   trovato = FALSE;
   if ( argc == 2 )
     devname[ 0 ] = '\0';
 else
     {
     for ( i = 0 ; i < 32 ; i++ )
     devname[ i ] = '\0';
     strncpy ( devname, argv[ 2 ], 31 );
     }
     strcpy ( searchfile, argv[ 1 ] );
  
     for ( ch = searchfile ; *ch != '\0'; ch++ )
       *ch = toupper( *ch );

     if ( ( dir = (BPTR) Lock ( devname,ACCESS_READ ) ) == NULL )
       {
       printf ( "Impossibile lock di %st\n", devname );
       die( 5L );
       }
     else
       strcpy ( path, devname );
     scan_directory( searchfile, path );
   }
 die( 0L );
}
