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