From Didier Gautheron:
[obnox/wireshark/wip.git] / tempfile.c
1 /* tempfile.c
2  * Routines to create temporary files
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <errno.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #ifdef HAVE_WINDOWS_H
41 #include <windows.h>
42 #endif
43
44 #include "tempfile.h"
45 #include "mkstemp.h"
46 #include <wsutil/file_util.h>
47
48 static const char *
49 setup_tmpdir(const char *dir)
50 {
51         size_t len = strlen(dir);
52         char *newdir;
53
54         /* Append path separator if necessary */
55         if (len != 0 && dir[len - 1] == G_DIR_SEPARATOR) {
56                 return dir;
57         }
58         else {
59                 newdir = g_strdup_printf("%s%s", dir, G_DIR_SEPARATOR_S);
60                 return newdir;
61         }
62 }
63
64 static int
65 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
66 {
67         static const char suffix[] = "XXXXXXXXXX";
68         int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
69         int old_umask;
70         int tmp_fd;
71
72         g_snprintf(namebuf, namebuflen, "%s%s%s", dir, pfx, suffix);
73         if (namebuflen < namelen) {
74                 /* Stick with the truncated name, so that if this error is
75                    reported with the file name, you at least get
76                    something. */
77                 errno = ENAMETOOLONG;
78                 return -1;
79         }
80
81         /* The Single UNIX Specification doesn't say that "mkstemp()"
82            creates the temporary file with mode rw-------, so we
83            won't assume that all UNIXes will do so; instead, we set
84            the umask to 0077 to take away all group and other
85            permissions, attempt to create the file, and then put
86            the umask back. */
87         old_umask = umask(0077);
88         tmp_fd = mkstemp(namebuf);
89         umask(old_umask);
90         return tmp_fd;
91 }
92
93 static const char *tmpdir = NULL;
94 #ifdef _WIN32
95 static const char *temp = NULL;
96 #endif
97 static const char *E_tmpdir;
98
99 #ifndef P_tmpdir
100 #define P_tmpdir "/var/tmp"
101 #endif
102
103 int
104 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
105 {
106         char *dir;
107         int fd;
108         static gboolean initialized;
109
110         if (!initialized) {
111                 if ((dir = getenv("TMPDIR")) != NULL)
112                         tmpdir = setup_tmpdir(dir);
113 #ifdef _WIN32
114                 if ((dir = getenv("TEMP")) != NULL)
115                         temp = setup_tmpdir(dir);
116 #endif
117
118                 E_tmpdir = setup_tmpdir(P_tmpdir);
119                 initialized = TRUE;
120         }
121
122         if (tmpdir != NULL) {
123                 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
124                 if (fd != -1)
125                         return fd;
126         }
127
128 #ifdef _WIN32
129         if (temp != NULL) {
130                 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
131                 if (fd != -1)
132                         return fd;
133         }
134 #endif
135
136         fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
137         if (fd != -1)
138                 return fd;
139
140         return try_tempfile(namebuf, namebuflen, G_DIR_SEPARATOR_S "tmp", pfx);
141 }