Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4973 right-hear 1
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
2
#include 
3
#include 
4
#include 
5
#include 
6
#include 
7
#include 
8
#include 
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
}