/**\file n_dir.c
*
*  Program for testing directory listing
*
*\author Castagnier Mickaël
*
*\version 1.0
*
*\date 04/01/2007
*
*/


#include <nilorea.h>

#define FA_REC 64

int find_file( char *pattern , int attrib );


int main( int argc , char *argv[] )
{

    if( argc < 2 )
        argv[1] = "*.*" ;

    printf( "Searching %s\n" , argv[ 1 ] );

    allegro_init();

    find_file( argv[1] , FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_LABEL | FA_DIREC | FA_ARCH | FA_REC );

    allegro_exit();

    return TRUE;

}END_OF_MAIN()

int find_file( char *pattern , int attrib )
{
    int done = 0;

    struct al_ffblk next;

    char tmpbuf[ 2048 ];
    char *ptr;

    fix_filename_slashes( pattern );

    done = al_findfirst( pattern , &next , attrib );

    while (!done)
    {
        printf("%s ", next.name );
        if( next.attrib & FA_RDONLY )
            printf( "FA_RDONLY " );
        if( next.attrib & FA_HIDDEN )
            printf( "FA_HIDDEN " );
        if( next.attrib & FA_SYSTEM )
            printf( "FA_SYSTEM " );
        if( next.attrib & FA_LABEL )
            printf( "FA_LABEL " );
        if( next.attrib & FA_ARCH )
            printf( "FA_ARCH " );

        if( next.attrib & FA_DIREC )
        {
            printf( "FA_DIREC " );

            if( attrib & FA_REC )
            {
                printf( "\nFA_SHOULD_RECURSIVE\n" );

                if( ( next.name[ 0 ] != '.' && next.name[ 1 ] != '0' ) || ( next.name[ 0 ] != '.' && next.name[ 1 ] != '.' && next.name[ 1 ] != '0' ) )
                {
                ptr = get_executable_name( pattern ) ;
                sprintf( tmpbuf , "%s\\%s" , next.name , ptr );

                printf("\ntmpbuf:%s\n" , tmpbuf );

                find_file( tmpbuf , attrib );
                }
            }
        }

        printf( "\n" );

        done = al_findnext( &next );
    }

    return TRUE;
}
