#pragma extend

#define RMXRET int
#define RMXOBJ int
#include <rmx.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <udi.h>

char *ffirst( char * ), *fnext();

/****************************************************************************/
/* explain_ccode - outputs explanation of erroneous condition code          */
/****************************************************************************/

void explain_ccode( int ccode )
    {
    int status;
    static char buff[50];
        
    buff[0] = 0;
    rqcformatexception( buff, 50, ccode, 1, &status );

    if ( status != E_OK )
        fprintf( stderr, "%04X: E$UNKNOWN$EXCEPTION, ", ccode );
    else
        fprintf( stderr, "%s, ", cstr( buff, buff ) );
    }

/****************************************************************************/
/* binary_time - produces the iRMX binary time equivalent for the date and  */
/* (optional) time strings provided. iRMX binary time is a count of the     */
/* number of seconds since Midnight January 1, 1978.                        */
/****************************************************************************/

long binary_time( char *date_str, char *time_str )
    {
    int  month, day, year, hour, minute, second, index;
    long bin_time;
    long days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    if ( ( date_str[2] != '/' ) || ( date_str[5] != '/' ) )
        return( -1 );
    
    year = ( date_str[6] - '0' ) * 10 + ( date_str[7] - '0' );

    if ( ( year % 4 ) == 0 )
        ++days[1];

    if ( year < 78 )
        year += 100;

    month = ( date_str[0] - '0' ) * 10 + ( date_str[1] - '0' ) - 1;
    
    if ( month > 11 )
        return( -1 );
  
    day = ( date_str[3] - '0' ) * 10 + ( date_str[4] - '0' ) - 1;
    
    if ( day > days[month] )
        return( -1 );
  
    if ( time_str == NULL )
        hour = minute = second = 0;
    else
        {
        if ( ( time_str[2] != ':' ) || ( time_str[5] != ':' ) )
            return( -1 );
       
        hour = ( time_str[0] - '0' ) * 10 + ( time_str[1] - '0' );
        
        if ( hour >= 24 )
            return( -1 );
            
        minute = ( time_str[3] - '0' ) * 10 + ( time_str[4] - '0' );
        
        if ( minute >= 60 )
            return( -1 );
            
        second = ( time_str[6] - '0' ) * 10 + ( time_str[7] - '0' );

        if ( second >= 60 )
            return( -1 );
        }

    bin_time = 0;
  
    for ( index = 78; index < year; index++ )
        {
        bin_time += 31536000L;
        
        if ( ( index % 4 ) == 0 )
            bin_time += 86400L;
        }
        
    for ( index = 0; index < month; index++ )
        bin_time += days[index] * 86400L;

    bin_time += (day * 86400L) + (hour * 3600L) + (minute * 60L) + second;
    return( bin_time );
    }

/****************************************************************************/
/* MODIFIED MM/DD/YY [HH:MM:SS] <pathname> [<pathname>]*                    */
/****************************************************************************/

int main( int argc, char *argv[] )
    {
    long modify_time;
    int  status, start, index;
    char *filename, buffer[256];
    EXT_FILE_ATTRIBS attribs;

    if ( argc < 3 )
        {
        printf( "\n\nUSE: modified MM/DD/YY [HH:MM:SS] <pathname> [<pathname>]*\n\n" );
        exit( 0 );
        }
    
    if ( ( argv[2][2] != ':' ) || ( argv[2][5] != ':' ) )
        {
        modify_time = binary_time( argv[1], NULL );
        start = 2;
        }
    else
        {
        modify_time = binary_time( argv[1], argv[2] );
        start = 3;
        }
    
    if ( modify_time == -1 )
        {
        fprintf( stderr, "\n\nInvalid Date/Time String Specified\n\n" );
        exit( 0 );
        }
    
    for ( index = start; index < argc; index++ )
        {
        filename = ffirst( argv[index] );
        
        if ( filename == NULL )
            fprintf( stderr, "File %s does not exist!!!\n", argv[index] );
        else
            while ( filename != NULL )
                {
                rqsgetfilestatus( udistr( buffer, filename ), &attribs, &status );
                
                if ( status != E_OK )
                    {
                    explain_ccode( status );
                    fprintf( stderr, "while processing file %s\n", filename );
                    }
                else
                    if ( attribs.last_modify_time > modify_time )
                        printf( "%s\n", filename );
                        
                filename = fnext();
                }
        }
    
    exit( 0 );
    }

