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

/* Compiled with Lattice v4.0 */

void main(argc, argv)
int argc;
char **argv;
{
    char *filestring;
    char *commandstring;
    int error;
    int i;
    struct FILEINFO info;
    int attr=0;
    
    /* One could really look more at the arguments more here... */
    
    if (argc<3)
    {
        printf("Usage is %ls <filepattern> <command> <extra args>\n", argv[0]);
        exit(0);
    }
    
    /* Get filepattern and command to apply */
    
    filestring=strdup(argv[1]);
    commandstring=strdup(argv[2]);
    
    /* Make sure command gets all the supplied args.. */
    
    for(i=3;i<argc;i++)
    {
        strcat(commandstring, " ");
        strcat(commandstring, argv[i]);
    }
    
    strcat(commandstring, " ");
        
    if((error=dfind(&info, filestring, attr))!=0)
    {
        printf("\nNo file named %ls", filestring);
        exit(0);
    } 
    
    do 
    if(Execute(strcat(strdup(commandstring), info.fib_FileName),0,0)==0)
    {
        printf("Execute failed with command %ls\n and filename %ls\n",
        commandstring, info.fib_FileName);
        exit(0);
    }
    
    /* First strdup to create an untouched copy of commandstring, then */
    /* execute it with current filename. */
    
    while(!dnext(&info));  /* Nice test. */
    
    printf("\nDone!\n");
    
}
