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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - mkname.c
  3.  *
  4.  * function(s)
  5.  *        __mkname - builds a file name of the form TMPXXXXX.$$$
  6.  *        __tmpnam - builds a unique file name
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #include <_printf.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <dos.h>
  22. #include <RtlData.h>
  23.  
  24. #if !defined( _RTLDLL )
  25. static char    _template[L_tmpnam];
  26. #endif
  27.  
  28. /*---------------------------------------------------------------------*
  29.  
  30. Name            __mkname - builds a file name of the form TMPXXXXX.$$$
  31.  
  32. Usage           char * pascal __mkname(char *s, char *prefix, unsigned num);
  33.  
  34. Prototype in    _stdio.h
  35.  
  36. Description     __mkname constructs a file name of the form pfxXXXXX.$$$.
  37.                 The prefix can be a string of up to 3 characters; if NULL,
  38.                 "TMP" is used.  The XXXXX is the ASCII decimal equivalent
  39.                 of the 'num' serial number.  The filename is stored
  40.                 at s, or in an internal static buffer if s is NULL.
  41.  
  42. Return value    A pointer to the constructed filename.
  43.  
  44. *---------------------------------------------------------------------*/
  45. char * pascal near __mkname(char *s, char *prefix, unsigned num)
  46. {
  47.         /* If no buffer provided, use internal template (static buffer)
  48.          */
  49.         if (s == NULL)
  50.                 s = _RTLInstanceData(_template);
  51.  
  52.         /* A temporary name is build as follows: pfxXXXXX.$$$
  53.          * where pfx is the prefix (up to three characters),
  54.          * and XXXXX is the ASCII decimal equivalent of num.
  55.          */
  56.         __utoa(num, _stpcpy(s, prefix == NULL ? "TMP" : prefix));
  57.         strcat(s, ".$$$");
  58.         return (s);
  59. }
  60.  
  61. /*---------------------------------------------------------------------*
  62.  
  63. Name            __tmpnam - builds a unique file name
  64.  
  65. Usage           char *__tmpnam(char *s,unsigned *numP);
  66.  
  67. Prototype in    stdio.h
  68.  
  69. Description     __tmpnam generates a temporary filename that does not
  70.                 conflict with any existing files.  The name is stored
  71.                 in the buffer pointed by s.  If s is null, the name
  72.                 is stored in an internal static buffer.  If s is not
  73.                 NULL, it must point to a buffer of at least L__tmpnam
  74.                 bytes (L__tmpnam is defined in stdio.h).  Up to TMP_MAX
  75.                 unique filenames can be generated before names will
  76.                 be reused.
  77.  
  78.                 The filename generated has the form TMPXXXXX.$$$,
  79.                 where XXXXX is the ASCII decimal equivalent of
  80.                 the file's "serial number".  NumP points to this
  81.                 serial number; this number can be passed to __mkname
  82.                 to reconstruct the filename.
  83.  
  84.                 The function returns when it generates a filename
  85.                 that does not exist or cannot be accessed.
  86.  
  87. Return value    If successful, a pointer to the generated name.
  88.                 If a unique filename cannot be generated, NULL is
  89.                 returned.
  90.  
  91. *---------------------------------------------------------------------*/
  92. char * pascal near __tmpnam(char *s, unsigned *numP)
  93. {
  94.         unsigned attr;
  95.  
  96.         do
  97.                 s = __mkname(s, (char *) NULL, *numP += (*numP == -1U) ? 2 : 1);
  98.         while (_dos_getfileattr(s, &attr) == 0);
  99.  
  100.         return (s);
  101. }
  102.