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 it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 Bash is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
25 #include <bashtypes.h>
26 #include <posixstat.h>
27 #include <posixtime.h>
30 #if defined (HAVE_UNISTD_H)
43 #define BASEOPENFLAGS (O_CREAT | O_TRUNC | O_EXCL)
45 #define DEFAULT_TMPDIR "." /* bogus default, should be changed */
46 #define DEFAULT_NAMEROOT "shtmp"
48 extern pid_t dollar_dollar_pid;
50 static char *get_sys_tmpdir __P((void));
51 static char *get_tmpdir __P((int));
53 static char *sys_tmpdir = (char *)NULL;
55 static int tmpnamelen = -1;
56 static unsigned long filenum = 1L;
65 sys_tmpdir = P_tmpdir;
66 if (file_iswdir (sys_tmpdir))
71 if (file_iswdir (sys_tmpdir))
74 sys_tmpdir = "/var/tmp";
75 if (file_iswdir (sys_tmpdir))
78 sys_tmpdir = "/usr/tmp";
79 if (file_iswdir (sys_tmpdir))
82 sys_tmpdir = DEFAULT_TMPDIR;
93 tdir = (flags & MT_USETMPDIR) ? get_string_value ("TMPDIR") : (char *)NULL;
95 tdir = get_sys_tmpdir ();
97 #if defined (HAVE_PATHCONF) && defined (_PC_NAME_MAX)
99 tmpnamelen = pathconf (tdir, _PC_NAME_MAX);
108 sh_mktmpname (nameroot, flags)
112 char *filename, *tdir, *lroot;
116 filename = (char *)xmalloc (PATH_MAX + 1);
117 tdir = get_tmpdir (flags);
118 tdlen = strlen (tdir);
120 lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
123 sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
124 if (mktemp (filename) == 0)
129 #else /* !USE_MKTEMP */
132 filenum = (filenum << 1) ^
133 (unsigned long) time ((time_t *)0) ^
134 (unsigned long) dollar_dollar_pid ^
135 (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
136 sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
137 if (tmpnamelen > 0 && tmpnamelen < 32)
138 filename[tdlen + 1 + tmpnamelen] = '\0';
140 r = lstat (filename, &sb);
142 r = stat (filename, &sb);
144 if (r < 0 && errno == ENOENT)
147 #endif /* !USE_MKTEMP */
153 sh_mktmpfd (nameroot, flags, namep)
158 char *filename, *tdir, *lroot;
161 filename = (char *)xmalloc (PATH_MAX + 1);
162 tdir = get_tmpdir (flags);
163 tdlen = strlen (tdir);
165 lroot = nameroot ? nameroot : DEFAULT_NAMEROOT;
168 sprintf (filename, "%s/%s.XXXXXX", tdir, lroot);
169 fd = mkstemp (filename);
170 if (fd < 0 || namep == 0)
178 #else /* !USE_MKSTEMP */
181 filenum = (filenum << 1) ^
182 (unsigned long) time ((time_t *)0) ^
183 (unsigned long) dollar_dollar_pid ^
184 (unsigned long) ((flags & MT_USERANDOM) ? get_random_number () : ntmpfiles++);
185 sprintf (filename, "%s/%s-%lu", tdir, lroot, filenum);
186 if (tmpnamelen > 0 && tmpnamelen < 32)
187 filename[tdlen + 1 + tmpnamelen] = '\0';
188 fd = open (filename, BASEOPENFLAGS | ((flags & MT_READWRITE) ? O_RDWR : O_WRONLY), 0600);
190 while (fd < 0 && errno == EEXIST);
198 #endif /* !USE_MKSTEMP */
202 sh_mktmpfp (nameroot, flags, namep)
210 fd = sh_mktmpfd (nameroot, flags, namep);
212 return ((FILE *)NULL);
213 fp = fdopen (fd, (flags & MT_READWRITE) ? "w+" : "w");