6ae7c4284182e84d6cf5cd6032a453f2cfd4f4e5
[gnulib.git] / lib / tmpdir.c
1 /* Copyright (C) 1999, 2001-2002, 2006, 2009-2012 Free Software Foundation,
2    Inc.
3    This file is part of the GNU C Library.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Extracted from sysdeps/posix/tempname.c.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "tmpdir.h"
24
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <errno.h>
30 #ifndef __set_errno
31 # define __set_errno(Val) errno = (Val)
32 #endif
33
34 #include <stdio.h>
35 #ifndef P_tmpdir
36 # ifdef _P_tmpdir /* native Windows */
37 #  define P_tmpdir _P_tmpdir
38 # else
39 #  define P_tmpdir "/tmp"
40 # endif
41 #endif
42
43 #include <sys/stat.h>
44
45 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
46 # define WIN32_LEAN_AND_MEAN  /* avoid including junk */
47 # include <windows.h>
48 #endif
49
50 #include "pathmax.h"
51
52 #if _LIBC
53 # define struct_stat64 struct stat64
54 #else
55 # define struct_stat64 struct stat
56 # define __xstat64(version, path, buf) stat (path, buf)
57 #endif
58
59 #if ! (HAVE___SECURE_GETENV || _LIBC)
60 # define __secure_getenv getenv
61 #endif
62
63 /* Pathname support.
64    ISSLASH(C)           tests whether C is a directory separator character.
65  */
66 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
67   /* Native Windows, Cygwin, OS/2, DOS */
68 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
69 #else
70   /* Unix */
71 # define ISSLASH(C) ((C) == '/')
72 #endif
73
74
75 /* Return nonzero if DIR is an existent directory.  */
76 static bool
77 direxists (const char *dir)
78 {
79   struct_stat64 buf;
80   return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
81 }
82
83 /* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is
84    non-null and exists, uses it; otherwise uses the first of $TMPDIR,
85    P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable
86    for use with mk[s]temp.  Will fail (-1) if DIR is non-null and
87    doesn't exist, none of the searched dirs exists, or there's not
88    enough space in TMPL. */
89 int
90 path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
91              bool try_tmpdir)
92 {
93   const char *d;
94   size_t dlen, plen;
95
96   if (!pfx || !pfx[0])
97     {
98       pfx = "file";
99       plen = 4;
100     }
101   else
102     {
103       plen = strlen (pfx);
104       if (plen > 5)
105         plen = 5;
106     }
107
108   if (try_tmpdir)
109     {
110       d = __secure_getenv ("TMPDIR");
111       if (d != NULL && direxists (d))
112         dir = d;
113       else if (dir != NULL && direxists (dir))
114         /* nothing */ ;
115       else
116         dir = NULL;
117     }
118   if (dir == NULL)
119     {
120 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
121       char dirbuf[PATH_MAX];
122       DWORD retval;
123
124       /* Find Windows temporary file directory.
125          We try this before P_tmpdir because Windows defines P_tmpdir to "\\"
126          and will therefore try to put all temporary files in the root
127          directory (unless $TMPDIR is set).  */
128       retval = GetTempPath (PATH_MAX, dirbuf);
129       if (retval > 0 && retval < PATH_MAX && direxists (dirbuf))
130         dir = dirbuf;
131       else
132 #endif
133       if (direxists (P_tmpdir))
134         dir = P_tmpdir;
135       else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
136         dir = "/tmp";
137       else
138         {
139           __set_errno (ENOENT);
140           return -1;
141         }
142     }
143
144   dlen = strlen (dir);
145   while (dlen >= 1 && ISSLASH (dir[dlen - 1]))
146     dlen--;                     /* remove trailing slashes */
147
148   /* check we have room for "${dir}/${pfx}XXXXXX\0" */
149   if (tmpl_len < dlen + 1 + plen + 6 + 1)
150     {
151       __set_errno (EINVAL);
152       return -1;
153     }
154
155   sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
156   return 0;
157 }