Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <libc/bss.h>
  9.  
  10. static char *tmp_dir;
  11. static int tmp_len;
  12. static int tmp_bss_count = -1;
  13.  
  14. static void
  15. try(const char *var)
  16. {
  17.   static char buf[L_tmpnam];
  18.  
  19.   char *t = getenv(var);
  20.   if (t == 0)
  21.     return;
  22.  
  23.   tmp_len = strlen(t);
  24.   strcpy(buf, t);
  25.   if (buf[tmp_len - 1] != '/' && buf[tmp_len - 1] != '\\')
  26.     buf[tmp_len++] = '/', buf[tmp_len] = 0;
  27.  
  28.   if (access(buf, D_OK))
  29.     return;
  30.  
  31.   tmp_dir = buf;
  32. }
  33.  
  34. char *
  35. tmpnam(char *s)
  36. {
  37.   static char static_buf[L_tmpnam];
  38.   static char tmpcount[] = "dj000000";
  39.   int i;
  40.  
  41.   if (tmp_bss_count != __bss_count)
  42.   {
  43.     tmp_bss_count = __bss_count;
  44.  
  45.     if (tmp_dir == 0) try("TMPDIR");
  46.     if (tmp_dir == 0) try("TEMP");
  47.     if (tmp_dir == 0) try("TMP");
  48.     if (tmp_dir == 0)
  49.     {
  50.       static char def[] = "c:/";
  51.       tmp_dir = def;
  52.       tmp_len = 3;
  53.     }
  54.   }
  55.  
  56.   if (!s)
  57.     s = static_buf;
  58.   strcpy(s, tmp_dir);
  59.  
  60.   do {
  61.     /* increment the "count" starting at the first digit (backwards order) */
  62.     for (i=2; tmpcount[i] == '9' && i < 8; tmpcount[i] = '0', i++);
  63.     if (i < 8)
  64.       tmpcount[i]++;
  65.  
  66.     strcpy(s+tmp_len, tmpcount);
  67.  
  68.   } while (access(s, F_OK)==0); /* until file doesn't exist */
  69.  
  70.   return s;
  71. }
  72.