home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / tmpnam.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  41 lines

  1. /* tmpnam.c : return a temporary file name */
  2. /* written by Eric R. Smith and placed in the public domain */
  3. /**
  4.  *  - retuned name can be passed outside via system(); other programs
  5.  *    may not dig '/' as a path separator
  6.  *  - somehow more frugal in a memory use
  7.  *    (mj - October 1990)
  8.  **/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. static char pattern[] = "\\__XXXXXX";
  14.  
  15. char *tmpnam(buf)
  16.     char *buf;
  17. {
  18.     char *tmpdir;
  19.     size_t tlen;
  20.     extern char *mktemp __PROTO((char *));
  21.  
  22.     if (((tmpdir = getenv("TEMP")) == NULL) && ((tmpdir = getenv("TMPDIR")) == NULL) &&
  23.         ((tmpdir = getenv("TMP")) == NULL) && ((tmpdir = getenv("TEMPDIR")) == NULL))
  24.         tmpdir = ".";
  25.  
  26.     tlen = strlen(tmpdir);
  27.  
  28.     if (!buf) {
  29.         size_t blen;
  30.         
  31.         blen = tlen + sizeof(pattern);
  32.         if (NULL == (buf = (char *) malloc(blen)))
  33.             return NULL;
  34.     }
  35.     strcpy(buf, tmpdir);
  36.     if (tmpdir[tlen-1] == '/' || tmpdir[tlen-1] == '\\')
  37.         --tlen;
  38.     strcpy(buf+tlen, pattern);
  39.     return(mktemp(buf));
  40. }
  41.