blob: a87c254fbd77f9a6c70eda9c0ac389591b1e3b17 [file] [log] [blame]
Jari Aalto28ef6c32001-04-06 19:14:31 +00001/*
2 * tmpfile.c - functions to create and safely open temp files for the shell.
3 */
4
5/* Copyright (C) 2000 Free Software Foundation, Inc.
6
7 This file is part of GNU Bash, the Bourne Again SHell.
8
Jari Aalto31859422009-01-12 13:36:28 +00009 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.
Jari Aalto28ef6c32001-04-06 19:14:31 +000013
Jari Aalto31859422009-01-12 13:36:28 +000014 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.
Jari Aalto28ef6c32001-04-06 19:14:31 +000018
19 You should have received a copy of the GNU General Public License
Jari Aalto31859422009-01-12 13:36:28 +000020 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21*/
Jari Aalto28ef6c32001-04-06 19:14:31 +000022
23#include <config.h>
24
25#include <bashtypes.h>
26#include <posixstat.h>
Jari Aalto7117c2d2002-07-17 14:10:11 +000027#include <posixtime.h>
Jari Aalto28ef6c32001-04-06 19:14:31 +000028#include <filecntl.h>
29
30#if defined (HAVE_UNISTD_H)
31# include <unistd.h>
32#endif
33
34#include <stdio.h>
35#include <errno.h>
36
37#include <shell.h>
38
39#ifndef errno
40extern int errno;
41#endif
42
43#define BASEOPENFLAGS (O_CREAT | O_TRUNC | O_EXCL)
44
45#define DEFAULT_TMPDIR "." /* bogus default, should be changed */
46#define DEFAULT_NAMEROOT "shtmp"
47
Jari Aaltof73dda02001-11-13 17:56:06 +000048extern pid_t dollar_dollar_pid;
Jari Aalto28ef6c32001-04-06 19:14:31 +000049
Jari Aalto06285672006-10-10 14:15:34 +000050static char *get_sys_tmpdir __P((void));
51static char *get_tmpdir __P((int));
52
Jari Aalto28ef6c32001-04-06 19:14:31 +000053static char *sys_tmpdir = (char *)NULL;
54static int ntmpfiles;
55static int tmpnamelen = -1;
56static unsigned long filenum = 1L;
57
58static char *
59get_sys_tmpdir ()
60{
Jari Aalto28ef6c32001-04-06 19:14:31 +000061 if (sys_tmpdir)
62 return sys_tmpdir;
63
Jari Aaltof73dda02001-11-13 17:56:06 +000064#ifdef P_tmpdir
65 sys_tmpdir = P_tmpdir;
Jari Aaltob80f6442004-07-27 13:29:18 +000066 if (file_iswdir (sys_tmpdir))
Jari Aaltof73dda02001-11-13 17:56:06 +000067 return sys_tmpdir;
68#endif
69
Jari Aalto28ef6c32001-04-06 19:14:31 +000070 sys_tmpdir = "/tmp";
Jari Aaltob80f6442004-07-27 13:29:18 +000071 if (file_iswdir (sys_tmpdir))
Jari Aalto28ef6c32001-04-06 19:14:31 +000072 return sys_tmpdir;
73
74 sys_tmpdir = "/var/tmp";
Jari Aaltob80f6442004-07-27 13:29:18 +000075 if (file_iswdir (sys_tmpdir))
Jari Aalto28ef6c32001-04-06 19:14:31 +000076 return sys_tmpdir;
77
78 sys_tmpdir = "/usr/tmp";
Jari Aaltob80f6442004-07-27 13:29:18 +000079 if (file_iswdir (sys_tmpdir))
Jari Aalto28ef6c32001-04-06 19:14:31 +000080 return sys_tmpdir;
81
Jari Aalto28ef6c32001-04-06 19:14:31 +000082 sys_tmpdir = DEFAULT_TMPDIR;
Jari Aalto28ef6c32001-04-06 19:14:31 +000083
84 return sys_tmpdir;
85}
86
87static char *
88get_tmpdir (flags)
89 int flags;
90{
91 char *tdir;
92
93 tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
Jari Aalto31859422009-01-12 13:36:28 +000094 if (tdir && (file_iswdir (tdir) == 0 || strlen (tdir) > PATH_MAX))
95 tdir = 0;
96
Jari Aalto28ef6c32001-04-06 19:14:31 +000097 if (tdir == 0)
98 tdir = get_sys_tmpdir ();
99
100#if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
101 if (tmpnamelen == -1)
102 tmpnamelen = pathconf (tdir, _PC_NAME_MAX);
103#else
104 tmpnamelen = 0;
105#endif
106
107 return tdir;
108}
109
110char *
111sh_mktmpname (nameroot, flags)
112 char *nameroot;
113 int flags;
114{
Jari Aaltof73dda02001-11-13 17:56:06 +0000115 char *filename, *tdir, *lroot;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000116 struct stat sb;
117 int r, tdlen;
118
Jari Aaltof73dda02001-11-13 17:56:06 +0000119 filename = (char *)xmalloc (PATH_MAX + 1);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000120 tdir = get_tmpdir (flags);
121 tdlen = strlen (tdir);
122
Jari Aaltof73dda02001-11-13 17:56:06 +0000123 lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000124
Jari Aaltof73dda02001-11-13 17:56:06 +0000125#ifdef USE_MKTEMP
126 sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
127 if (mktemp (filename) == 0)
128 {
129 free (filename);
130 filename = NULL;
131 }
132#else /* !USE_MKTEMP */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000133 while (1)
134 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000135 filenum = (filenum << 1) ^
136 (unsigned long) time ((time_t *)0) ^
137 (unsigned long) dollar_dollar_pid ^
138 (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
139 sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000140 if (tmpnamelen > 0 && tmpnamelen < 32)
141 filename[tdlen + 1 + tmpnamelen] = '\0';
Jari Aaltof73dda02001-11-13 17:56:06 +0000142# ifdef HAVE_LSTAT
Jari Aalto28ef6c32001-04-06 19:14:31 +0000143 r = lstat (filename, &sb);
Jari Aaltof73dda02001-11-13 17:56:06 +0000144# else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000145 r = stat (filename, &sb);
Jari Aaltof73dda02001-11-13 17:56:06 +0000146# endif
Jari Aalto28ef6c32001-04-06 19:14:31 +0000147 if (r < 0 && errno == ENOENT)
148 break;
149 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000150#endif /* !USE_MKTEMP */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000151
152 return filename;
153}
154
155int
156sh_mktmpfd (nameroot, flags, namep)
157 char *nameroot;
158 int flags;
159 char **namep;
160{
Jari Aaltof73dda02001-11-13 17:56:06 +0000161 char *filename, *tdir, *lroot;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000162 int fd, tdlen;
163
Jari Aaltof73dda02001-11-13 17:56:06 +0000164 filename = (char *)xmalloc (PATH_MAX + 1);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000165 tdir = get_tmpdir (flags);
166 tdlen = strlen (tdir);
167
Jari Aaltof73dda02001-11-13 17:56:06 +0000168 lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000169
Jari Aaltof73dda02001-11-13 17:56:06 +0000170#ifdef USE_MKSTEMP
171 sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
172 fd = mkstemp (filename);
173 if (fd < 0 || namep == 0)
174 {
175 free (filename);
176 filename = NULL;
177 }
178 if (namep)
179 *namep = filename;
180 return fd;
181#else /* !USE_MKSTEMP */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000182 do
183 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000184 filenum = (filenum << 1) ^
185 (unsigned long) time ((time_t *)0) ^
186 (unsigned long) dollar_dollar_pid ^
187 (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
188 sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000189 if (tmpnamelen > 0 && tmpnamelen < 32)
190 filename[tdlen + 1 + tmpnamelen] = '\0';
191 fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600);
192 }
193 while (fd < 0 && errno == EEXIST);
194
195 if (namep)
196 *namep = filename;
197 else
198 free (filename);
199
200 return fd;
Jari Aaltof73dda02001-11-13 17:56:06 +0000201#endif /* !USE_MKSTEMP */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000202}
203
204FILE *
205sh_mktmpfp (nameroot, flags, namep)
206 char *nameroot;
207 int flags;
208 char **namep;
209{
210 int fd;
Jari Aaltof73dda02001-11-13 17:56:06 +0000211 FILE *fp;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000212
213 fd = sh_mktmpfd (nameroot, flags, namep);
Jari Aaltof73dda02001-11-13 17:56:06 +0000214 if (fd < 0)
215 return ((FILE *)NULL);
216 fp = fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w");
217 if (fp == 0)
218 close (fd);
219 return fp;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000220}