From Chris Maynard:
[metze/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 /* create a tempfile with the given prefix (e.g. "ether")
104  * namebuf (and namebuflen) should be 128+1 bytes long (BTW: why?)
105  * returns the file descriptor of the new tempfile and
106  * the name of the new file in namebuf 
107  */
108 int
109 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
110 {
111         char *dir;
112         int fd;
113         static gboolean initialized;
114
115         if (!initialized) {
116                 if ((dir = getenv("TMPDIR")) != NULL)
117                         tmpdir = setup_tmpdir(dir);
118 #ifdef _WIN32
119                 if ((dir = getenv("TEMP")) != NULL)
120                         temp = setup_tmpdir(dir);
121 #endif
122
123                 E_tmpdir = setup_tmpdir(P_tmpdir);
124                 initialized = TRUE;
125         }
126
127         if (tmpdir != NULL) {
128                 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
129                 if (fd != -1)
130                         return fd;
131         }
132
133 #ifdef _WIN32
134         if (temp != NULL) {
135                 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
136                 if (fd != -1)
137                         return fd;
138         }
139 #endif
140
141         fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
142         if (fd != -1)
143                 return fd;
144
145         return try_tempfile(namebuf, namebuflen, G_DIR_SEPARATOR_S "tmp", pfx);
146 }