/**************************************************************************/
/*                                                                        */
/*                                 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_free.c,v 1.2 1997/09/25 17:19:18 ae59 Exp $ */
/*
$Log: f_free.c,v $
 * Revision 1.2  1997/09/25  17:19:18  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:26  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : f_free.c                              */
/*                                                              */
/*      Entry           : void f_free(desc)                     */
/*                        f_text *desc;                         */
/*                                                              */
/*      Arguments       : desc   - file descriptor              */
/*                                                              */
/*      Description     : Close file and remove if temporary    */
/*                                                              */
/*      Date            : 1991-09-18                            */
/*      1991-09-18 : remove file descriptor from linked list    */
/*      1991-09-02 : explicitly reset fp-component to NULL      */
/*      1991-07-09 : put final '\n' to open output textfile     */
/****************************************************************/

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

#ifdef LINT_ARGS
local void f_free(f_text *desc)
#else
local void f_free(desc)

f_text *desc;
#endif

        {
        register a_VOID ptr;

        /* 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);

        if (desc->stdi==FALSE && desc->stdo==FALSE && desc->fp!=NULL)
           {
           fclose(desc->fp);
           if (desc->temp) remove(desc->name);
           }
        desc->temp = FALSE;
        desc->fp = NULL;

        /* remove file descriptor from linked list */
        if (f_ftop==(a_VOID)desc) f_ftop = desc->next;
        else
           {
           ptr = f_ftop;
           while (((f_text *)ptr)->next!=NULL)
              {
              if (((f_text *)ptr)->next==(a_VOID)desc)
                 {
                 ((f_text *)ptr)->next = desc->next;
                 break;
                 }
              else
                 ptr = ((f_text *)ptr)->next;
              }
           }

        return;
        }

