From:	HENRY::IN%"carl%cithex.caltech.edu%kl.sri.COM%relay.cs.net@RCA.COM" 28-APR-1987 23:40
To:	unc!mcguffey@mcnc.org, info-vax@CITHEX.CALTECH.EDU
Subj:	Re: redirection of stdio, et al.

 > Does anyone know of any tools that allow command line redirection of
 > stdio under VMS. Something similar to the way unix and msdos does it is
 > preferable to some of the methods that have previously been proposed.

The following is C code to do at least more or less what you want.  First
a short rationale for using the technique I did, rather than something else:
The C run time library does things like setting up the array of io blocks
and parsing the command line for you; while I could have written code to
do this, and then linked this code with everything I wrote, I decided to 
use the default startup routines, and then to do any I/O redirection later.
The end result is a routine called ioinit that is used as follows:
main(nargs, args)
int nargs;
char **args;
{	...
	ioinit(&nargs, args);
	...
}
It redirects any or all of stdin, stdout, or stderr, using the same syntax
as under UNIX* (with the exception of "here documents");
/******************************************************************************/
/* IOINIT.C -- I/O redirection subroutine.  Redirects stdin (< or <<);
 *	stdout (> or >>); or stderr ( 2>, 2>>)
 * Usage:
 *	main(nargs, args)
 *	{	...
 *		ioinit(&nargs, args);
 *		...
 *	}
 */

#include <errno.h>
#include <stdio.h>

typedef char *STR;

ioinit(nargs, args)
int *nargs;
STR *args;
{   int argnum;

    for (argnum = 1; argnum < *nargs; ++argnum)
    {	if (strncmp(args[argnum], "<", 1) == 0)
	{   args[argnum] += 1;
	    *nargs = u_reopen(*nargs, args, argnum, "r", stdin);
	    argnum--;
	} else if (strncmp(args[argnum], ">>", 2) == 0)
	{   args[argnum] += 2;
	    *nargs = u_reopen(*nargs, args, argnum, "a", stdout);
	    argnum--;
	} else if (strncmp(args[argnum], ">", 1) == 0)
	{   args[argnum] += 1;
	    *nargs = u_reopen(*nargs, args, argnum, "w", stdout);
	    argnum--;
	} else if (strncmp(args[argnum], "2>>", 3) == 0)
	{   args[argnum] += 3;
	    *nargs = u_reopen(*nargs, args, argnum, "a", stderr);
	    argnum--;
	} else if (strncmp(args[argnum], "2>", 2) == 0)
	{    args[argnum] += 2;
	    *nargs = u_reopen(*nargs, args, argnum, "w", stderr);
	    argnum--;
	}
    }
}

u_reopen(nargs, args, argnum, acmod, chan)
int nargs, argnum;
STR *args, acmod;
FILE *chan;
{   char *file;
    int offset, i, errornum;

    if (args[argnum][0] != '\0')
    {	offset = 1;
	file = args[argnum];
    } else if ((argnum + 1) < nargs)
    {	offset = 2;
	file = args[argnum+1];
    } else
    {	fprintf(stderr, "Illegal redirection on command line.\n");
	exit(1);
    } for (i = argnum; i < nargs - offset; ++i)
	args[i] = args[i + offset];
    if (((*acmod == ' ' || strcmp(acmod,"a+") == 0) && freopen(file, acmod,
	chan ,"rfm=stm") != chan) || freopen(file, acmod, chan) != chan)
    {	if (errno == EVMSERR)
	{	errornum = vaxc$errno;
		fprintf(stderr, "Failure opening redirected stream.\n");
		exit(errornum);
	} else
	{	perror("Failure opening redirected stream.");
		exit(1);
	}
    } return(nargs - offset);
}
/******************************************************************************/
Using this as a starting point, you should be able to tailor it to your
needs.
