/**************************************************************************/
/*                                                                        */
/*                                 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: b_tmpf.c,v 1.2 1997/09/25 17:09:35 ae59 Exp $ */
/*
$Log: b_tmpf.c,v $
 * Revision 1.2  1997/09/25  17:09:35  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:50  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_tmpf.c                              */
/*                                                              */
/*      Entry           : void b_tmpf(name,length)              */
/*                        char *name;                           */
/*                        a_intg length;                        */
/*                                                              */
/*      Arguments       : name = space for temporary filename   */
/*                        length = length of reserved space     */
/*                                                              */
/*      Description     : Generate a temporary filename         */
/*                        Characters 2 thru 6 of TEMP_BASE      */
/*                        are assumed to be digits.             */
/*                                                              */
/*      Date            : 1992-03-30                            */
/*      1992-03-30 : correct name generation: 1. break at if =b=*/
/*                   2. mv str... in front of do-> start at t0..*/
/*      1992-01-16 : use global o_text instead of constants     */
/*      1991-08-01 : TEMP_BASE characters 2-6 are incremented   */
/*      1991-02-01 : (int) cast before sizeof()                 */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern char *o_text[];
#endif

#ifdef LINT_ARGS
local void b_tmpf(char *name,a_intg length)
#else
local void b_tmpf(name,length)

char *name;
a_intg length;
#endif

        {
        int len,pos;
        FILE *fp;

        E_TPUSH("b_tmpf")

        if ((pos = strlen(o_text[39]))+strlen(o_text[40])>=length)
           {
           e_trap(I_O_BUFFER,2,E_TMSG,30);
           E_TPOPP("b_tmpf")
           return;
           }

        (void)strcpy(name,o_text[39]);
        (void)strcat(name,o_text[40]);
        do
           {
           /* test for existing file                            */
           if ((fp = fopen(name,"r"))!=NULL) fclose(fp);
           else break;

           /* generate next temporary filename                  */
           for (len=pos+5;len>pos;len--)
              {
              if (name[len]=='9') name[len] = '0';
              else
                 {
                 name[len]++;
                 break;
                 }
              }
           }
        while (fp!=NULL);

        E_TPOPP("b_tmpf")
        return;
        }
