/*
** The following is a suggested skeleton for utilities which expect
** to handle a variable number of files with wildcards handled in
** input files and redirection of output files.  This type of application
** is very common and is difficult to handle...thus this example.
**
** The command line syntax for the resulting program is:
**  **name** [-o outputpath] file file file...
**    where the -o parameter is optional and indicates the path to
**    the directory where output files are to be placed.
**
** The skeleton handles wild card file names in the following fashion:
** First, if a "-o" option is detected, the skeleton builds an output
** file name mask consisting of the path given by the user after the
** option.  Then it reads file names from the command line and uses
** the "wildexp" function to expand them into a table of existing
** filenames which match.  For each filename in the table, the skeleton
** opens a file.  If a "-o" option was given by the user, it concatenates
** the input filename to the path obtained above, otherwise it
** concatenates ".f" to the end of the input filename.  These actions
** are necessary to ensure that the input file and the output file
** have different names.  The skeleton then opens the output file
** and calls "dowork".  If there are errors in opening either the
** input file or the output file, "dowork" is not called and an
** appropriate message is printed to stderr.
**
** Basically, to use this skeleton, first replace occurances of "**name**"
** with the name of the utility you are creating.  Then, write the
** "dowork" function.  The "dowork" function gets two arguments..."fin"
** and "fout".  For each file the user wants to process, the "main"
** function opens an input file (fin) and an output file (fout) and
** then calls "dowork"...which performs whatever processing there is
** to be done and then returns.
**
** Written by:  Rick Schaeffer
**              E. 13611 26th Ave.
**              Spokane, Wa.  99216
**              Compuserve ID: 70120,174
**              Bytenet ID: ricks.
*/

#include <stdio.h>
extern int Enable_Abort;

char    *largv[128];

main(argc,argv)
int     argc;
char    *argv[];
{
    char    outpath[80];
    char    outname[80];
    char    *nameonly,*strrchr();
    char    *malloc();
    int     i,j,begarg;
    FILE    *fin,*fout;

    Enable_Abort = 1;
    outpath[0] = 0;
    if ((argc < 2) || (strcmp(argv[1],"?") == 0)) {
        printf("**name** -- Usage:\n");
        printf("  **name** [-o outpath] file file\n\n");
        printf("if -o option present, will write files to that path\n");
        printf("Otherwise concatenates '.f' to input file names\n\n");
        exit(1);
        }
    if (strcmp(argv[1],"-o") == 0) {
        strncpy(outpath,argv[2],78);
        begarg = 3;
        }
    else
        begarg = 1;
    for (i=begarg; i<argc; i++) {
        if (iswild(argv[i])) {
            wildexp(argv[i],largv,127);
            if (largv[0] == NULL)
                fprintf(stderr,"No matching files for ==> %s!\n",argv[i]);
            }
        else {
            largv[0] = malloc(strlen(argv[i])+1);
            strcpy(largv[0],argv[i]);
            largv[1] = NULL;
            }
        j=0;
        while (largv[j] != NULL) {
            if (outpath[0] == 0) {
                strncpy(outname,largv[j],77);
                strcat(outname,".f");
                }
            else {
                strcpy(outname,outpath);
                nameonly = strrchr(largv[j],'/');
                if (nameonly == NULL)
                    nameonly = strrchr(largv[j],':');
                if (nameonly == NULL)
                    nameonly = largv[j];
                else
                    nameonly++;
                if ((strlen(outname) + strlen(nameonly)) > 79) {
                    fprintf(stderr,"Pathname+Filename too long\n");
                    break;
                    }
                strcat(outname,nameonly);
                }
            if ((fin = fopen(largv[j],"r")) == NULL) {
                fprintf(stderr,"Couldn't open input file ==> %s\n",largv[j]);
                break;
                }
            if ((fout = fopen(outname,"w")) == NULL) {
                fprintf(stderr,"Couldn't open output file ==> %s\n",outname);
                fclose(fin);
                break;
                }
            fprintf(stderr,"%s ==> %s\n",largv[j],outname);
            dowork(fin,fout);
            fclose(fin);
            fclose(fout);
            j++;
            }
        }
}

dowork(*fin,*fout)
FILE    *fin,*fout;
{
}
