Make some more string conversions available to plugins
[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 #include "file_util.h"
41
42 #ifdef HAVE_WINDOWS_H
43 #include <windows.h>
44 #endif
45
46 #include "tempfile.h"
47 #include "mkstemp.h"
48
49 static const char *
50 setup_tmpdir(const char *dir)
51 {
52         size_t len = strlen(dir);
53         char *newdir;
54
55         /* Append path separator if necessary */
56         if (len != 0 && dir[len - 1] == G_DIR_SEPARATOR) {
57                 return dir;
58         }
59         else {
60                 newdir = g_malloc(len + 2);
61                 strcpy(newdir, dir);
62                 strcat(newdir, G_DIR_SEPARATOR_S);
63                 return newdir;
64         }
65 }
66
67 static int
68 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
69 {
70         static const char suffix[] = "XXXXXXXXXX";
71         int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
72         int old_umask;
73         int tmp_fd;
74
75         if (namebuflen < namelen) {
76                 /* Stick in a truncated name, so that if this error is
77                    reported with the file name, you at least get
78                    something. */
79                 g_snprintf(namebuf, namebuflen, "%s%s%s", dir, pfx, suffix);
80                 errno = ENAMETOOLONG;
81                 return -1;
82         }
83         strcpy(namebuf, dir);
84         strcat(namebuf, pfx);
85         strcat(namebuf, suffix);
86
87         /* The Single UNIX Specification doesn't say that "mkstemp()"
88            creates the temporary file with mode rw-------, so we
89            won't assume that all UNIXes will do so; instead, we set
90            the umask to 0077 to take away all group and other
91            permissions, attempt to create the file, and then put
92            the umask back. */
93         old_umask = umask(0077);
94         tmp_fd = mkstemp(namebuf);
95         umask(old_umask);
96         return tmp_fd;
97 }
98
99 static const char *tmpdir = NULL;
100 #ifdef _WIN32
101 static const char *temp = NULL;
102 #endif
103 static const char *E_tmpdir;
104
105 #ifndef P_tmpdir
106 #define P_tmpdir "/var/tmp"
107 #endif
108
109 int
110 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
111 {
112         char *dir;
113         int fd;
114         static gboolean initialized;
115
116         if (!initialized) {
117                 if ((dir = getenv("TMPDIR")) != NULL)
118                         tmpdir = setup_tmpdir(dir);
119 #ifdef _WIN32
120                 if ((dir = getenv("TEMP")) != NULL)
121                         temp = setup_tmpdir(dir);
122 #endif
123
124                 E_tmpdir = setup_tmpdir(P_tmpdir);
125                 initialized = TRUE;
126         }
127
128         if (tmpdir != NULL) {
129                 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
130                 if (fd != -1)
131                         return fd;
132         }
133
134 #ifdef _WIN32
135         if (temp != NULL) {
136                 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
137                 if (fd != -1)
138                         return fd;
139         }
140 #endif
141
142         fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
143         if (fd != -1)
144                 return fd;
145
146         return try_tempfile(namebuf, namebuflen, G_DIR_SEPARATOR_S "tmp", pfx);
147 }