/* From the TOS GCC library by jrd */
/* modified to accept only template with trailing XXX's (really should reqire
 * that there be six trailing X's)
 */

#include <stddef.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include "lib.h"

#define TEN_MUL(X)	((((X) << 2) + (X)) << 1)

extern int __mint;

char * mktemp(pattern)
char * pattern;
{
  char * p, * q;
  long tempnum, nx;
  static int startat = 0;

  assert((pattern != NULL));

  /* scan back over X's */
  for(p = pattern; *p; p++) ;
  for(q = --p; *q == 'X'; --q) ;
  if((nx = p - q) == 0)  /* # of X's */
	return NULL;

  q++;

  /* if MiNT is active and there's room, put in the pid */
  /* we need 5 X's for this: up to 3 for the pid, and up to 2 for the
     extra number */
  if (__mint && nx > 4 && startat < 256) {
	(void) _itoa(getpid(), q, 10);
	while (*q) q++;
	(void) _itoa(startat++, q, 16);
	return pattern;
  }

  /* calc the #'s to try for X's, for 2 X's 10-99 and so on */
  for(tempnum = 1; --nx > 0; tempnum = TEN_MUL(tempnum)) ; /* [lower */
  nx = TEN_MUL(tempnum);			      /* upper) */

  tempnum += startat;  /* dont always start at [lower, start at lower+startat */
  if(tempnum >=nx )
  {
      tempnum -= startat;
      startat = 0;
  }
  else 
      startat++;

  for(; tempnum < nx; tempnum++)
  {
    (void) _ltoa(tempnum, q, 10); /* assumption: strrev reverses in place */
    if(access(pattern, F_OK))	/* using access takes care of unx2dos also */
	return pattern;
  }
  return NULL;
}
