home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / MKTEMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.7 KB  |  83 lines

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