2 * tmpfile.c - functions to create and safely open temp files for the shell.
5 /* Copyright (C) 2000-2020 Free Software Foundation, Inc.
7 This file is part of GNU Bush, the Bourne Again SHell.
9 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
25 #include <bushtypes.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 /* Use ANSI-C rand() interface if random(3) is not available */
52 #define random() rand()
55 extern pid_t dollar_dollar_pid
;
57 static char *get_sys_tmpdir
PARAMS((void));
58 static char *get_tmpdir
PARAMS((int));
60 static char *sys_tmpdir
= (char *)NULL
;
62 static int tmpnamelen
= -1;
63 static unsigned long filenum
= 1L;
72 sys_tmpdir
= P_tmpdir
;
73 if (file_iswdir (sys_tmpdir
))
78 if (file_iswdir (sys_tmpdir
))
81 sys_tmpdir
= "/var/tmp";
82 if (file_iswdir (sys_tmpdir
))
85 sys_tmpdir
= "/usr/tmp";
86 if (file_iswdir (sys_tmpdir
))
89 sys_tmpdir
= DEFAULT_TMPDIR
;
100 tdir
= (flags
& MT_USETMPDIR
) ? get_string_value ("TMPDIR") : (char *)NULL
;
101 if (tdir
&& (file_iswdir (tdir
) == 0 || strlen (tdir
) > PATH_MAX
))
105 tdir
= get_sys_tmpdir ();
107 #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
108 if (tmpnamelen
== -1)
109 tmpnamelen
= pathconf (tdir
, _PC_NAME_MAX
);
122 static int seeded
= 0;
127 gettimeofday (&tv
, NULL
);
128 srandom (tv
.tv_sec
^ tv
.tv_usec
^ (getpid () << 16) ^ (uintptr_t)&d
);
135 sh_mktmpname (nameroot
, flags
)
139 char *filename
, *tdir
, *lroot
;
142 static int seeded
= 0;
144 filename
= (char *)xmalloc (PATH_MAX
+ 1);
145 tdir
= get_tmpdir (flags
);
146 tdlen
= strlen (tdir
);
148 lroot
= nameroot
? nameroot
: DEFAULT_NAMEROOT
;
150 flags
&= ~MT_TEMPLATE
;
152 if ((flags
& MT_TEMPLATE
) && strlen (nameroot
) > PATH_MAX
)
153 flags
&= ~MT_TEMPLATE
;
156 if (flags
& MT_TEMPLATE
)
157 strcpy (filename
, nameroot
);
159 sprintf (filename
, "%s/%s.XXXXXX", tdir
, lroot
);
160 if (mktemp (filename
) == 0)
165 #else /* !USE_MKTEMP */
169 filenum
= (filenum
<< 1) ^
170 (unsigned long) time ((time_t *)0) ^
171 (unsigned long) dollar_dollar_pid
^
172 (unsigned long) ((flags
& MT_USERANDOM
) ? random () : ntmpfiles
++);
173 sprintf (filename
, "%s/%s-%lu", tdir
, lroot
, filenum
);
174 if (tmpnamelen
> 0 && tmpnamelen
< 32)
175 filename
[tdlen
+ 1 + tmpnamelen
] = '\0';
177 r
= lstat (filename
, &sb
);
179 r
= stat (filename
, &sb
);
181 if (r
< 0 && errno
== ENOENT
)
184 #endif /* !USE_MKTEMP */
190 sh_mktmpfd (nameroot
, flags
, namep
)
195 char *filename
, *tdir
, *lroot
;
198 filename
= (char *)xmalloc (PATH_MAX
+ 1);
199 tdir
= get_tmpdir (flags
);
200 tdlen
= strlen (tdir
);
202 lroot
= nameroot
? nameroot
: DEFAULT_NAMEROOT
;
204 flags
&= ~MT_TEMPLATE
;
206 if ((flags
& MT_TEMPLATE
) && strlen (nameroot
) > PATH_MAX
)
207 flags
&= ~MT_TEMPLATE
;
210 if (flags
& MT_TEMPLATE
)
211 strcpy (filename
, nameroot
);
213 sprintf (filename
, "%s/%s.XXXXXX", tdir
, lroot
);
214 fd
= mkstemp (filename
);
215 if (fd
< 0 || namep
== 0)
223 #else /* !USE_MKSTEMP */
227 filenum
= (filenum
<< 1) ^
228 (unsigned long) time ((time_t *)0) ^
229 (unsigned long) dollar_dollar_pid
^
230 (unsigned long) ((flags
& MT_USERANDOM
) ? random () : ntmpfiles
++);
231 sprintf (filename
, "%s/%s-%lu", tdir
, lroot
, filenum
);
232 if (tmpnamelen
> 0 && tmpnamelen
< 32)
233 filename
[tdlen
+ 1 + tmpnamelen
] = '\0';
234 fd
= open (filename
, BASEOPENFLAGS
| ((flags
& MT_READWRITE
) ? O_RDWR
: O_WRONLY
), 0600);
236 while (fd
< 0 && errno
== EEXIST
);
244 #endif /* !USE_MKSTEMP */
248 sh_mktmpfp (nameroot
, flags
, namep
)
256 fd
= sh_mktmpfd (nameroot
, flags
, namep
);
258 return ((FILE *)NULL
);
259 fp
= fdopen (fd
, (flags
& MT_READWRITE
) ? "w+" : "w");
266 sh_mktmpdir (nameroot
, flags
)
270 char *filename
, *tdir
, *lroot
, *dirname
;
274 filename
= (char *)xmalloc (PATH_MAX
+ 1);
275 tdir
= get_tmpdir (flags
);
276 tdlen
= strlen (tdir
);
278 lroot
= nameroot
? nameroot
: DEFAULT_NAMEROOT
;
280 flags
&= ~MT_TEMPLATE
;
282 if ((flags
& MT_TEMPLATE
) && strlen (nameroot
) > PATH_MAX
)
283 flags
&= ~MT_TEMPLATE
;
285 if (flags
& MT_TEMPLATE
)
286 strcpy (filename
, nameroot
);
288 sprintf (filename
, "%s/%s.XXXXXX", tdir
, lroot
);
289 dirname
= mkdtemp (filename
);
296 #else /* !USE_MKDTEMP */
297 filename
= (char *)NULL
;
300 filename
= sh_mktmpname (nameroot
, flags
);
301 fd
= mkdir (filename
, 0700);
305 filename
= (char *)NULL
;
307 while (fd
< 0 && errno
== EEXIST
);
310 #endif /* !USE_MKDTEMP */