home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / mktemp.c < prev    next >
C/C++ Source or Header  |  1992-09-17  |  2KB  |  69 lines

  1. /* From the TOS GCC library by jrd */
  2. /* modified to accept only template with trailing XXX's (really should reqire
  3.  * that there be six trailing X's)
  4.  */
  5.  
  6. #include <stddef.h>
  7. #include <support.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include "lib.h"
  13.  
  14. extern int __mint;        /* 0 for TOS, MiNT version number otherwise */
  15.  
  16. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  17.  
  18. char * mktemp(pattern)
  19. char * pattern;
  20. {
  21.   char * p, * q;
  22.   long tempnum, nx;
  23.   static int startat = 0;
  24.   int save_errno;
  25.  
  26.   assert((pattern != NULL));
  27.  
  28.   /* scan back over X's */
  29.   for(p = pattern; *p; p++) ;
  30.   for(q = --p; *q == 'X'; --q) ;
  31.   if((nx = p - q) == 0)  /* # of X's */
  32.     return NULL;
  33.   q++;
  34.  
  35.   /* if MiNT is active and there's room, put in the pid */
  36.   /* we need 5 X's for this: up to 3 for the pid, and up to 2 for the
  37.      extra number */
  38.   if (__mint && nx > 4 && startat < 256) {
  39.     (void) _itoa(getpid(), q, 10);
  40.     while (*q) q++;
  41.     (void) _itoa(startat++, q, 16);
  42.     return pattern;
  43.   }
  44.  
  45.   /* calc the #'s to try for X's, for 2 X's 10-99 and so on */
  46.   for(tempnum = 1; --nx > 0; tempnum = TEN_MUL(tempnum)) ; /* [lower */
  47.   nx = TEN_MUL(tempnum);                  /* upper) */
  48.   tempnum += startat;  /* dont always start at [lower, start at lower+startat */
  49.   if(tempnum >=nx )
  50.   {
  51.       tempnum -= startat;
  52.       startat = 0;
  53.   }
  54.   else 
  55.       startat++;
  56.   save_errno = errno;
  57.   for(; tempnum < nx; tempnum++)
  58.   {
  59.     (void) _ltoa(tempnum, q, 10); /* assumption: strrev reverses in place */
  60.     if(access(pattern, F_OK))    /* using access takes care of unx2dos also */
  61.     {
  62.     errno = save_errno;
  63.     return pattern;
  64.     }
  65.   }
  66.   errno = save_errno;
  67.   return NULL;
  68. }
  69.