1 /* Copyright (C) 1999, 2001-2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Extracted from sysdeps/posix/tempname.c. */
34 # define __set_errno(Val) errno = (Val)
39 # define P_tmpdir "/tmp"
43 #if STAT_MACROS_BROKEN
46 #if !defined S_ISDIR && defined S_IFDIR
47 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
49 #if !S_IRUSR && S_IREAD
50 # define S_IRUSR S_IREAD
53 # define S_IRUSR 00400
55 #if !S_IWUSR && S_IWRITE
56 # define S_IWUSR S_IWRITE
59 # define S_IWUSR 00200
61 #if !S_IXUSR && S_IEXEC
62 # define S_IXUSR S_IEXEC
65 # define S_IXUSR 00100
69 # define struct_stat64 struct stat64
71 # define struct_stat64 struct stat
72 # define __xstat64(version, path, buf) stat (path, buf)
75 #if ! (HAVE___SECURE_GETENV || _LIBC)
76 # define __secure_getenv getenv
80 /* Return nonzero if DIR is an existent directory. */
82 direxists (const char *dir
)
85 return __xstat64 (_STAT_VER
, dir
, &buf
) == 0 && S_ISDIR (buf
.st_mode
);
88 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
89 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
90 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
91 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
92 doesn't exist, none of the searched dirs exists, or there's not
93 enough space in TMPL. */
95 path_search (char *tmpl
, size_t tmpl_len
, const char *dir
, const char *pfx
,
115 d
= __secure_getenv ("TMPDIR");
116 if (d
!= NULL
&& direxists (d
))
118 else if (dir
!= NULL
&& direxists (dir
))
125 if (direxists (P_tmpdir
))
127 else if (strcmp (P_tmpdir
, "/tmp") != 0 && direxists ("/tmp"))
131 __set_errno (ENOENT
);
137 while (dlen
> 1 && dir
[dlen
- 1] == '/')
138 dlen
--; /* remove trailing slashes */
140 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
141 if (tmpl_len
< dlen
+ 1 + plen
+ 6 + 1)
143 __set_errno (EINVAL
);
147 sprintf (tmpl
, "%.*s/%.*sXXXXXX", (int) dlen
, dir
, (int) plen
, pfx
);