/* ***************************************************************************
 File	    : redirect_stdout.c
 Module     : redirect_stdout()
 Purpose    : make all printf's write to a file named on the command line
 Purpose    :   if the last two parameters are ">" and "filename" or
 Purpose    :   the last argument is ">filename".  Also handles append
 Purpose    :   mode ( ">> filename" or ">>filename")
 Library    : ??
 *--------------------------------------------------------------------------*
	This is free software protected under the DECUS and Free Software
	Foundation tenents.
 *--------------------------------------------------------------------------*
 Parameters   : int *argc - number of parameters passed to the program from
 Parameters   :	  the C rtl.  If redirection is found, this is decremented
 Parameters   :   by 2 or 1 depending on whether redirection is specified in
 Parameters   :   1 or 2 arguments.
 Parameters   : char *argv[] - parameter tokens passed to the program from
 Parameters   :	  the C rtl.

 Returns      : void
 Side effects : stdout goes to a named file.  The global pointer stdout can be
 Side effects :   modified.
 *--------------------------------------------------------------------------*
 Ver.Mod    : 1.0    Date: 19-feb-91          Author: rob riepel rob@ssvax1.dnet.loral.com
 Description:

*---------------------------------------------------------------------------*
 Ver.Mod    : 1.1    Date: 19-feb-91   Author: RTGregory
 Description: made into callable routine

 *--------------------------------------------------------------------------*/

#include <stdio.h>

void redirect_stdout( argc, argv)
  int *argc;
  char *argv[];

{
int fd, carg, i;

    carg = *argc;

    /*  check for redirection of output (>[ ]string)  */

    /*  note that we creat() the output file then redirect stdout in the  */
    /*  append mode.  this is so the file will have the correct VMS file  */
    /*  attributes (otherwise it would be STREAM_LF (yuck...)).           */

    /*******************************************************
    ** handles "> filename" or ">> filename" as last two arguments
    *******************************************************/
    if (argv[carg-2][0] == '>') 
    {
	/*******************************************************
	** if not append mode, create a new version of the file
	*******************************************************/
        if (argv[carg-2][1] != '>')
	{
            if ((fd=creat(argv[carg-1],0)) < 0 ||
		 close(fd))
	    {
                perror(argv[carg-1]);
                exit(1);
            }
	}

	/*******************************************************
	** open in append mode
	*******************************************************/
        if (freopen(argv[carg-1],"a",stdout) == NULL) {
            perror(argv[carg-1]);
            exit(1);
        }
        else
	{
	    carg -= 2;
	}
    }
    /*******************************************************
    ** handles ">filename" or ">>filename" as last argument
    *******************************************************/
    else if (argv[carg-1][0] == '>') 
    {
        i = 1;
        if (argv[carg-1][1] == '>')
	{
	  i = 2;
	}
        else
	{
	    /*******************************************************
	    ** if not append mode, create a new version of the file
	    *******************************************************/
            if ((fd=creat(&argv[carg-1][i],0)) < 0 ||
		 close(fd)) 
	    {
                perror(&argv[carg-1][i]);
                exit(1);
            }
        }
        
	/*******************************************************
	** open in append mode
	*******************************************************/
        if (freopen(&argv[carg-1][i],"a",stdout) == NULL) {
            perror(&argv[carg-1][i]);
            exit(1);
        }
        else
	{
	    carg--;
	}
    }

    *argc = carg;
} /* redirect_output */


#ifdef TESTING

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

{
	printf( "%d\n", argc);
	redirect_stdout( &argc, argv);
	printf( 
	  "send me to a file by adding \"> a.tmp\" to the command line\n");
	printf( "%d\n", argc);
}

#endif /* testing */
