AArch64: Remove unused defines of CPU names
[glibc.git] / stdio-common / tmpdir.c
blobf189e8577842ca0d7a39f631ec90f45934949e2b
1 /* Copyright (C) 1999, 2001-2002, 2006, 2009-2024 Free Software Foundation,
2 Inc.
3 This file is part of the GNU C Library.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Extracted from sysdeps/posix/tempname.c. */
20 #include <config.h>
22 /* Specification. */
23 #include "tmpdir.h"
25 #include <stdlib.h>
26 #include <string.h>
28 #include <errno.h>
29 #ifndef __set_errno
30 # define __set_errno(Val) errno = (Val)
31 #endif
33 #include <stdio.h>
34 #ifndef P_tmpdir
35 # ifdef _P_tmpdir /* native Windows */
36 # define P_tmpdir _P_tmpdir
37 # else
38 # define P_tmpdir "/tmp"
39 # endif
40 #endif
42 #include <sys/stat.h>
44 #if defined _WIN32 && ! defined __CYGWIN__
45 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
46 # include <windows.h>
47 #endif
49 #if defined _WIN32 && ! defined __CYGWIN__
50 /* Don't assume that UNICODE is not defined. */
51 # undef GetTempPath
52 # define GetTempPath GetTempPathA
53 #endif
55 #if _LIBC
56 # define struct_stat64 struct __stat64_t64
57 #else
58 # include "pathmax.h"
59 # define struct_stat64 struct stat
60 # define __libc_secure_getenv secure_getenv
61 # define __stat64_time64(path, buf) stat (path, buf)
62 #endif
64 /* Pathname support.
65 ISSLASH(C) tests whether C is a directory separator character.
67 #if defined _WIN32 || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
68 /* Native Windows, Cygwin, OS/2, DOS */
69 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
70 #else
71 /* Unix */
72 # define ISSLASH(C) ((C) == '/')
73 #endif
76 /* Return nonzero if DIR is an existent directory. */
77 static bool
78 direxists (const char *dir)
80 struct_stat64 buf;
81 return __stat64_time64 (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
84 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
85 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
86 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
87 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
88 doesn't exist, none of the searched dirs exists, or there's not
89 enough space in TMPL. */
90 int
91 __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
92 bool try_tmpdir)
94 const char *d;
95 size_t dlen, plen;
96 bool add_slash;
98 if (!pfx || !pfx[0])
100 pfx = "file";
101 plen = 4;
103 else
105 plen = strlen (pfx);
106 if (plen > 5)
107 plen = 5;
110 if (try_tmpdir)
112 d = __libc_secure_getenv ("TMPDIR");
113 if (d != NULL && direxists (d))
114 dir = d;
115 else if (dir != NULL && direxists (dir))
116 /* nothing */ ;
117 else
118 dir = NULL;
120 if (dir == NULL)
122 #if defined _WIN32 && ! defined __CYGWIN__
123 char dirbuf[PATH_MAX];
124 DWORD retval;
126 /* Find Windows temporary file directory.
127 We try this before P_tmpdir because Windows defines P_tmpdir to "\\"
128 and will therefore try to put all temporary files in the root
129 directory (unless $TMPDIR is set). */
130 retval = GetTempPath (PATH_MAX, dirbuf);
131 if (retval > 0 && retval < PATH_MAX && direxists (dirbuf))
132 dir = dirbuf;
133 else
134 #endif
135 if (direxists (P_tmpdir))
136 dir = P_tmpdir;
137 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
138 dir = "/tmp";
139 else
141 __set_errno (ENOENT);
142 return -1;
146 dlen = strlen (dir);
147 #ifdef __VMS
148 add_slash = 0;
149 #else
150 add_slash = dlen != 0 && !ISSLASH (dir[dlen - 1]);
151 #endif
153 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
154 if (tmpl_len < dlen + add_slash + plen + 6 + 1)
156 __set_errno (EINVAL);
157 return -1;
160 memcpy (tmpl, dir, dlen);
161 sprintf (tmpl + dlen, &"/%.*sXXXXXX"[!add_slash], (int) plen, pfx);
162 return 0;