home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / MKTEMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.4 KB  |  85 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - mktemp.c
  3.  *
  4.  * function(s)
  5.  *        mktemp - makes a unique file name
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #include <dir.h>
  20. #include <string.h>
  21. #include <io.h>
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name        mktemp - makes a unique file name
  26.  
  27. Usage        char *mktemp(char *template);
  28.  
  29. Prototype in    dir.h
  30.  
  31. Description    mktemp replaces template by a unique file name
  32.         and returns the address of template.
  33.  
  34.         The template should be a null-terminated string
  35.         with six trailing X's. These X's are replaced with a unique
  36.         collection of letters plus a dot, so that there are two letters,
  37.         a dot, and three suffix letters in the new file name.
  38.  
  39.         Starting with AA.AAA, the new file name is assigned by looking
  40.         up the names on the disk and avoiding pre-existing names of the
  41.         same format.
  42.  
  43. Return value    If template is well-formed, mktemp returns the
  44.         address of the template string. Otherwise, it does not create
  45.         or open the file.
  46.  
  47. *---------------------------------------------------------------------*/
  48. char *mktemp(char *temp)
  49. {
  50.     register char    *cp;
  51.     int        len;
  52.     int        i, j, k, l, m;
  53.  
  54.     len = strlen(temp);
  55.     if (len < 6)
  56.         return(0);
  57.     cp = temp + len - 6;
  58.     if (strcmp(cp, "XXXXXX") != 0)
  59.         return(0);
  60.     cp[2] = '.';
  61.     for (i = 'A'; i <= 'Z'; i++)
  62.     {
  63.         cp[0] = i;
  64.         for (j = 'A'; j <= 'Z'; j++)
  65.         {
  66.             cp[1] = j;
  67.             for (k = 'A'; k <= 'Z'; k++)
  68.             {
  69.                 cp[3] = k;
  70.                 for (l = 'A'; l <= 'Z'; l++)
  71.                 {
  72.                     cp[4] = l;
  73.                     for (m = 'A'; m <= 'Z'; m++)
  74.                     {
  75.                         cp[5] = m;
  76.                         if (access(temp, 0777) == -1)
  77.                             return(temp);
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     return(0);
  84. }
  85.