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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - tempnam.c
  3.  *
  4.  * function(s)
  5.  *        tempnam   - builds a unique file name in any directory
  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 <stdio.h>
  18. #include <_stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <dir.h>
  22. #include <dos.h>
  23. #include <io.h>
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name            tempnam - builds a unique file name
  28.  
  29. Usage           char *tempnam(char *dir, char *prefix);
  30.  
  31. Prototype in    stdio.h
  32.  
  33. Description     The tempnam function creates a unique filename
  34.                 in arbitrary directories.  It attempts to use the following
  35.                 directories, in order, when creating the file:
  36.  
  37.                 1. The directory specified by the TMP environment variable.
  38.                 2. The dir argument.
  39.                 3. The P_tmpdir definition in stdio.h.
  40.                 4. The current working directory.
  41.  
  42.                 The prefix parameter specifies the first part
  43.                 of the filename; it cannot be longer than 5 characters.
  44.                 A unique filename is created by concatenating the directory,
  45.                 the prefix, and 6 unique characters.  Space for the
  46.                 resulting filename is allocated with malloc; the caller
  47.                 must free this filename by calling free().  The unique
  48.                 file is not actually created; tempnam only verifies that
  49.                 it does not current exist.
  50.  
  51. Return value    a pointer to the unique temporary file name, which the
  52.                 caller may pass to free() when no longer needed; or NULL if a
  53.                 no unique filename can be created.
  54.  
  55. *---------------------------------------------------------------------*/
  56.  
  57. char * _FARFUNC tempnam(char *dir, char *prefix)
  58. {
  59.     char *dirs[4];
  60.     int tries;
  61.  
  62.     /* Make sure the prefix is 5 characters or less and has no '.' in it.
  63.      */
  64.     if (strlen(prefix) > 5 || strchr(prefix, '.') != NULL)
  65.         return (NULL);
  66.  
  67.     /* Set up the four directories we'll try searching.
  68.      */
  69.     dirs[0] = getenv("TMP");            /* TMP enviroment variable */
  70.     dirs[1] = dir;                      /* dir parameter */
  71.     dirs[2] = P_tmpdir;                 /* stdio.h temp dir */
  72.     dirs[3] = "";                       /* current directory */
  73.  
  74.     /* Search the four directories.
  75.      */
  76.     for (tries = 0; tries < 4; tries++)
  77.     {
  78.         char *dir, *p, *buf;
  79.         unsigned err, attr, num;
  80.  
  81.         /* Allocate a buffer big enough for the complete filename.
  82.         /* Put the directory name in the buffer, then repeatedly use
  83.          * __mkname to append a weird name until we get one that
  84.          * gives a filename that doesn't exist.
  85.          */
  86.         if ((dir = dirs[tries]) == NULL)
  87.             continue;           /* skip NULL directory */
  88.         if ((buf = malloc(strlen(dir)+strlen(prefix)+8)) == NULL)
  89.             continue;           /* can't allocate space for dir\preXXXXXX */
  90.         p = _stpcpy(buf,dir);
  91.         if (p != buf && *(p-1) != '/' && *(p-1) != '\\' && *(p-1) != ':')
  92.             *p++ = '\\';        /* add trailing slash */
  93.  
  94.         for (num = 0; num != TMP_MAX; num++)
  95.         {
  96.             __mkname(p, prefix, num);
  97.             if ((err = _dos_getfileattr(buf, &attr)) != 0)
  98.             {
  99.                 if (err == 2)           /* file not found? */
  100.                     return (buf);       /* return unique name */
  101.                 else                    /* some other error */
  102.                     break;              /* give up on this directory */
  103.             }
  104.         }
  105.         free(buf);                      /* try next directory */
  106.     }
  107.     return (NULL);              /* all four directories failed */
  108. }
  109.