/*{{{  about this file*/
/*
   mini MKTEMP for ORIGAMI (ST-port)
   (C) 1991 M. Schwingen
   This does just enough to work. Improvements are welcome.
   Temp. files are created in the $TMP or $TEMP directory. If those
   environment variables do not exist, the current directory is used.
   The passed string (pattern) is not used to build the filename - only
   the space is used !
   Use this where you want to - but only at your own risk!
*/
/*}}}  */

/*{{{  includes*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tos.h>
/*}}}  */

/*{{{  void mktemp(char *str)*/
void mktemp(char *str)
{
  static int tmpcnt = 0;
  char *p1;
  int stat;

  /* if env. variable 'TMP' or 'TEMP' exists: create temp file there */
  p1 = getenv("TMP");
  if (p1 == 0)
    p1 = getenv("TEMP");
  if (p1 != 0) /* we found something */
  {
    strcpy(str,p1);
    p1 = str;
    while(*p1)
      p1++;
    if(p1[-1] != '\\') /* if no trailing '\': append one */
    {
      *p1++ = '\\';
      *p1 = 0;
    }
  }
  else
  {
    *str = 0;  /* use current path */
    p1 = str;
  }

  do
  {
    sprintf(p1,"%08d.tmp",tmpcnt++); /* add temp file name */
    stat = Fsfirst(str,0x03);        /* ==0 -> file exists */
  } while (stat == 0);
}
/*}}}  */
