/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: f_srwi.c,v 1.2 1997/09/25 17:22:43 ae59 Exp $ */
/*
$Log: f_srwi.c,v $
 * Revision 1.2  1997/09/25  17:22:43  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:38  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : f_srwi.c                              */
/*                                                              */
/*      Entry           : void f_srwi(desc,device)              */
/*                        f_text *desc;                         */
/*                        s_trng device;                        */
/*                                                              */
/*      Arguments       : desc   - descriptor of device         */
/*                        device - name of device               */
/*                          ""   = standard output (text only)  */
/*                          file = filename used for fopen      */
/*                                                              */
/*      Description     : rewrite PASCAL device.                */
/*                                                              */
/*      Date            : 1991-09-05                            */
/*      1991-09-05 : eoln initialized with TRUE                 */
/*      1991-07-09 : put final '\n' to open output textfile     */
/*      1991-02-13 : open for binary access if required         */
/*      1990-12-04 : strncpy replaced by memcpy                 */
/*      1990-11-15 : E_TPUSH+E_TPOPP                            */
/*      1990-10-30 : filename replaced by device.ptr in e_trap  */
/*                   name argument removed from argument list   */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local void f_srwi(f_text *desc,s_trng device)
#else
local void f_srwi(desc,device)

f_text *desc;
s_trng device;
#endif
        {
        char *filename = NULL;

        E_TPUSH("f_srwi")

        /* put final newline character to textfile if opened for output */
        if (desc->asgd==TRUE && desc->fp!=NULL && desc->text==TRUE &&
            desc->outf==TRUE && desc->err==FALSE && desc->eoln==FALSE)
           f_putc((a_char)'\n',desc);

        /* close assigned device if not input/output            */
        if (desc->stdo==FALSE && desc->stdi==FALSE && desc->fp!=NULL)
           fclose(desc->fp);
        desc->fp = NULL;

        desc->eoln = desc->outf = TRUE;
        desc->infl = desc->err = FALSE;

        /* open standard output                         */
        if (device.clen==0)
           {
           if (desc->temp)
              {
              remove(desc->name);
              desc->temp = FALSE;
              }

           if (desc->text==TRUE)
              {
              desc->stdo = TRUE;
              desc->name[0] = '\0';
              }
           else
              {
              e_trap(I_O_ERROR,2,E_TMSG,33);
              E_TPOPP("f_srwi")
              return;
              }
           }

        /* filename is given                            */
        else
           {
           if (desc->temp)
              {
              remove(desc->name);
              desc->temp = FALSE;
              }

           if (device.clen>=f_fnsz-1)
              {
              e_trap(I_O_BUFFER,6,E_TMSG,29,E_TMSG,30,
                     E_TSTG+E_TEXT(8),device);
              E_TPOPP("f_srwi")
              return;
              }

           filename = &desc->name[0];
           memcpy(filename,device.ptr,device.clen);
           filename[device.clen] = '\0';

           desc->stdo = FALSE;
           }

        desc->eof = desc->asgd = TRUE;
        desc->stdi = FALSE;

        if (desc->stdo)
           desc->fp = stdout;
        else if ((desc->fp =
                  fopen(filename,((desc->text) ? "w" : "wb")))==NULL)
           {
           e_trap(I_O_ERROR,4,E_TMSG,32,E_TSTR+E_TEXT(8),filename);
           desc->err = TRUE;
           }

        if (device.tmp) s_free(&device);

        E_TPOPP("f_srwi")
        return;
        }
