2 * Routines to create temporary files
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
49 #include <process.h> /* For getpid() */
53 #include <wsutil/file_util.h>
56 #define __set_errno(x) errno=(x)
60 /* Generate a unique temporary file name from TEMPLATE.
61 The last six characters of TEMPLATE must be "XXXXXX";
62 they are replaced with a string that makes the filename unique.
63 Returns a file descriptor open on the file for reading and writing. */
68 static const char letters[]
69 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
73 len = strlen (template);
74 if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
80 if (g_snprintf (&template[len - 5], 6, "%.5u",
81 (unsigned int) getpid () % 100000) != 5)
82 /* Inconceivable lossage. */
85 for (i = 0; i < sizeof (letters); ++i)
89 template[len - 6] = letters[i];
91 fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600);
96 /* We return the null string if we can't find a unique file name. */
102 #endif /* HAVE_MKSTEMP */
105 /* Generate a unique temporary directory name from TEMPLATE.
106 The last six characters of TEMPLATE must be "XXXXXX";
107 they are replaced with a string that makes the filename unique.
108 Returns 0 on success or -1 on error (from mkdir(2)). */
113 static const char letters[]
114 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
118 len = strlen (template);
119 if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
121 __set_errno (EINVAL);
125 if (g_snprintf (&template[len - 5], 6, "%.5u",
126 (unsigned int) getpid () % 100000) != 5)
127 /* Inconceivable lossage. */
130 for (i = 0; i < sizeof (letters); ++i)
134 template[len - 6] = letters[i];
136 ret = ws_mkdir(template, 0700);
141 /* We return the null string if we can't find a unique file name. */
147 #endif /* HAVE_MKDTEMP */
150 #define INITIAL_PATH_SIZE 128
151 #define TMP_FILE_SUFFIX "XXXXXXXXXX"
154 * Create a tempfile with the given prefix (e.g. "wireshark").
156 * @param namebuf If not NULL, receives the full path of the temp file.
157 * Should NOT be freed.
158 * @param pfx A prefix for the temporary file.
159 * @return The file descriptor of the new tempfile, from mkstemp().
162 create_tempfile(char **namebuf, const char *pfx)
164 static char *tf_path[3];
165 static int tf_path_len[3];
174 * Allocate the buffer if it's not already allocated.
176 if (tf_path[idx] == NULL) {
177 tf_path_len[idx] = INITIAL_PATH_SIZE;
178 tf_path[idx] = (char *)g_malloc(tf_path_len[idx]);
182 * We can't use get_tempfile_path here because we're called from dumpcap.c.
184 tmp_dir = g_get_tmp_dir();
186 while (g_snprintf(tf_path[idx], tf_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > tf_path_len[idx]) {
187 tf_path_len[idx] *= 2;
188 tf_path[idx] = (char *)g_realloc(tf_path[idx], tf_path_len[idx]);
192 *namebuf = tf_path[idx];
194 /* The Single UNIX Specification doesn't say that "mkstemp()"
195 creates the temporary file with mode rw-------, so we
196 won't assume that all UNIXes will do so; instead, we set
197 the umask to 0077 to take away all group and other
198 permissions, attempt to create the file, and then put
200 old_umask = umask(0077);
201 fd = mkstemp(tf_path[idx]);
207 * Create a directory with the given prefix (e.g. "wireshark"). The path
208 * is created using g_get_tmp_dir and mkdtemp.
211 * @param pfx A prefix for the temporary directory.
212 * @return The temporary directory path on success, or NULL on failure.
216 create_tempdir(char **namebuf, const char *pfx)
218 static char *td_path[3];
219 static int td_path_len[3];
226 * Allocate the buffer if it's not already allocated.
228 if (td_path[idx] == NULL) {
229 td_path_len[idx] = INITIAL_PATH_SIZE;
230 td_path[idx] = (char *)g_malloc(td_path_len[idx]);
234 * We can't use get_tempfile_path here because we're called from dumpcap.c.
236 tmp_dir = g_get_tmp_dir();
238 while (g_snprintf(td_path[idx], td_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > td_path_len[idx]) {
239 td_path_len[idx] *= 2;
240 td_path[idx] = (char *)g_realloc(td_path[idx], td_path_len[idx]);
244 *namebuf = td_path[idx];
246 return mkdtemp(td_path[idx]);