2 * tmpfile.c - functions to create and safely open temp files for the shell.
5 /* Copyright (C) 2000 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #include <bashtypes.h>
26 #include <posixstat.h>
27 #include <posixtime.h>
30 #if defined (HAVE_UNISTD_H)
45 #define BASEOPENFLAGS (O_CREAT | O_TRUNC | O_EXCL | O_BINARY)
47 #define DEFAULT_TMPDIR "." /* bogus default, should be changed */
48 #define DEFAULT_NAMEROOT "shtmp"
50 extern pid_t dollar_dollar_pid;
52 static char *get_sys_tmpdir __P((void));
53 static char *get_tmpdir __P((int));
55 static char *sys_tmpdir = (char *)NULL;
57 static int tmpnamelen = -1;
58 static unsigned long filenum = 1L;
67 sys_tmpdir = P_tmpdir;
68 if (file_iswdir (sys_tmpdir))
73 if (file_iswdir (sys_tmpdir))
76 sys_tmpdir = "/var/tmp";
77 if (file_iswdir (sys_tmpdir))
80 sys_tmpdir = "/usr/tmp";
81 if (file_iswdir (sys_tmpdir))
84 sys_tmpdir = DEFAULT_TMPDIR;
95 tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
96 if (tdir && (file_iswdir (tdir) == 0 || strlen (tdir) > PATH_MAX))
100 tdir = get_sys_tmpdir ();
102 #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
103 if (tmpnamelen == -1)
104 tmpnamelen = pathconf (tdir, _PC_NAME_MAX);
113 sh_mktmpname (nameroot, flags)
117 char *filename, *tdir, *lroot;
121 filename = (char *)xmalloc (PATH_MAX + 1);
122 tdir = get_tmpdir (flags);
123 tdlen = strlen (tdir);
125 lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
128 sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
129 if (mktemp (filename) == 0)
134 #else /* !USE_MKTEMP */
137 filenum = (filenum << 1) ^
138 (unsigned long) time ((time_t *)0) ^
139 (unsigned long) dollar_dollar_pid ^
140 (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++);
141 sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
142 if (tmpnamelen > 0 && tmpnamelen < 32)
143 filename[tdlen + 1 + tmpnamelen] = '\0';
145 r = lstat (filename, &sb);
147 r = stat (filename, &sb);
149 if (r < 0 && errno == ENOENT)
152 #endif /* !USE_MKTEMP */
158 sh_mktmpfd (nameroot, flags, namep)
163 char *filename, *tdir, *lroot;
166 filename = (char *)xmalloc (PATH_MAX + 1);
167 tdir = get_tmpdir (flags);
168 tdlen = strlen (tdir);
170 lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
173 sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
174 fd = mkstemp (filename);
175 if (fd < 0 || namep == 0)
183 #else /* !USE_MKSTEMP */
186 filenum = (filenum << 1) ^
187 (unsigned long) time ((time_t *)0) ^
188 (unsigned long) dollar_dollar_pid ^
189 (unsigned long) ((flags & MT_USERANDOM) ? random () : ntmpfiles++);
190 sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
191 if (tmpnamelen > 0 && tmpnamelen < 32)
192 filename[tdlen + 1 + tmpnamelen] = '\0';
193 fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600);
195 while (fd < 0 && errno == EEXIST);
203 #endif /* !USE_MKSTEMP */
207 sh_mktmpfp (nameroot, flags, namep)
215 fd = sh_mktmpfd (nameroot, flags, namep);
217 return ((FILE *)NULL);
218 fp = fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w");