1 /* { dg-do compile } */
2 /* { dg-options "-O2" } */
3 
4 extern __SIZE_TYPE__ strlen (__const char *__s)
5      __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
6 extern char *strcpy (char *__restrict __dest, __const char *__restrict __src)
7      __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
8 extern char *getenv (__const char *__name) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
9 extern int access (__const char *__name, int __type) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
10 extern void * xmalloc (__SIZE_TYPE__) __attribute__ ((__malloc__));
11 
12 static __inline__ const char *
try(const char * dir,const char * base)13 try (const char *dir, const char *base)
14 {
15   if (base != 0)
16     return base;
17   if (dir != 0
18       && access (dir, 4 | 2 | 1) == 0)
19     return dir;
20   return 0;
21 }
22 
23 static const char tmp[] = { '/', 't', 'm', 'p', 0 };
24 static const char usrtmp[] =
25 { '/', 'u', 's', 'r', '/', 't', 'm', 'p', 0 };
26 static const char vartmp[] =
27 { '/', 'v', 'a', 'r', '/', 't', 'm', 'p', 0 };
28 
29 static char *memoized_tmpdir;
30 char *
choose_tmpdir(void)31 choose_tmpdir (void)
32 {
33   const char *base = 0;
34   char *tmpdir;
35   unsigned int len;
36 
37   if (memoized_tmpdir)
38     return memoized_tmpdir;
39 
40   base = try (getenv ("TMPDIR"), base);
41   base = try (getenv ("TMP"), base);
42   base = try (getenv ("TEMP"), base);
43 
44 
45   base = try ("/tmp", base);
46 
47 
48 
49   base = try (vartmp, base);
50   base = try (usrtmp, base);
51   base = try (tmp, base);
52 
53 
54   if (base == 0)
55     base = ".";
56 
57 
58 
59   len = strlen (base);
60   tmpdir = xmalloc (len + 2);
61   strcpy (tmpdir, base);
62   /* Alias analysis was associating read-only memory tags to pointers
63      that are not read-only.  We would then not issue any V_MAY_DEF in
64      this store.  */
65   tmpdir[len] = '/';
66   tmpdir[len+1] = '\0';
67 
68   memoized_tmpdir = tmpdir;
69   return tmpdir;
70 }
71