New module 'tmpdir'.
[gnulib.git] / lib / tmpdir.c
1 /* Copyright (C) 1999, 2001-2002, 2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 51 Franklin Street,
17    Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Extracted from sysdeps/posix/tempname.c.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 /* Specification.  */
26 #include "tmpdir.h"
27
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <errno.h>
33 #ifndef __set_errno
34 # define __set_errno(Val) errno = (Val)
35 #endif
36
37 #include <stdio.h>
38 #ifndef P_tmpdir
39 # define P_tmpdir "/tmp"
40 #endif
41
42 #include <sys/stat.h>
43 #if !defined S_ISDIR && defined S_IFDIR
44 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
45 #endif
46 #if !S_IRUSR && S_IREAD
47 # define S_IRUSR S_IREAD
48 #endif
49 #if !S_IRUSR
50 # define S_IRUSR 00400
51 #endif
52 #if !S_IWUSR && S_IWRITE
53 # define S_IWUSR S_IWRITE
54 #endif
55 #if !S_IWUSR
56 # define S_IWUSR 00200
57 #endif
58 #if !S_IXUSR && S_IEXEC
59 # define S_IXUSR S_IEXEC
60 #endif
61 #if !S_IXUSR
62 # define S_IXUSR 00100
63 #endif
64
65 #if _LIBC
66 # define struct_stat64 struct stat64
67 #else
68 # define struct_stat64 struct stat
69 # define __xstat64(version, path, buf) stat (path, buf)
70 #endif
71
72 #if ! (HAVE___SECURE_GETENV || _LIBC)
73 # define __secure_getenv getenv
74 #endif
75
76 /* Pathname support.
77    ISSLASH(C)           tests whether C is a directory separator character.
78  */
79 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
80   /* Win32, Cygwin, OS/2, DOS */
81 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
82 #else
83   /* Unix */
84 # define ISSLASH(C) ((C) == '/')
85 #endif
86
87
88 /* Return nonzero if DIR is an existent directory.  */
89 static bool
90 direxists (const char *dir)
91 {
92   struct_stat64 buf;
93   return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
94 }
95
96 /* Path search algorithm, for tmpnam, tmpfile, etc.  If DIR is
97    non-null and exists, uses it; otherwise uses the first of $TMPDIR,
98    P_tmpdir, /tmp that exists.  Copies into TMPL a template suitable
99    for use with mk[s]temp.  Will fail (-1) if DIR is non-null and
100    doesn't exist, none of the searched dirs exists, or there's not
101    enough space in TMPL. */
102 int
103 path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
104              bool try_tmpdir)
105 {
106   const char *d;
107   size_t dlen, plen;
108
109   if (!pfx || !pfx[0])
110     {
111       pfx = "file";
112       plen = 4;
113     }
114   else
115     {
116       plen = strlen (pfx);
117       if (plen > 5)
118         plen = 5;
119     }
120
121   if (try_tmpdir)
122     {
123       d = __secure_getenv ("TMPDIR");
124       if (d != NULL && direxists (d))
125         dir = d;
126       else if (dir != NULL && direxists (dir))
127         /* nothing */ ;
128       else
129         dir = NULL;
130     }
131   if (dir == NULL)
132     {
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 }