Fix the calculation of the temporary file name length in
[metze/wireshark/wip.git] / util.c
1 /* util.c
2  * Utility routines
3  *
4  * $Id: util.c,v 1.20 1999/09/23 05:20:18 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31
32 #include <gtk/gtk.h>
33
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <errno.h>
39
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #ifdef HAVE_SYS_TYPES_H
49 #include <sys/types.h>
50 #endif
51
52 #ifdef HAVE_SYS_STAT_H
53 #include <sys/stat.h>
54 #endif
55
56 #ifdef NEED_SNPRINTF_H
57 # ifdef HAVE_STDARG_H
58 #  include <stdarg.h>
59 # else
60 #  include <varargs.h>
61 # endif
62 # include "snprintf.h"
63 #endif
64
65 #include "util.h"
66
67 #include "image/icon-excl.xpm"
68 #include "image/icon-ethereal.xpm"
69
70 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
71
72 const gchar *bm_key = "button mask";
73
74 /* Simple dialog function - Displays a dialog box with the supplied message
75  * text.
76  * 
77  * Args:
78  * type       : One of ESD_TYPE_*.
79  * btn_mask   : The address of a gint.  The value passed in determines if
80  *              the 'Cancel' button is displayed.  The button pressed by the 
81  *              user is passed back.
82  * msg_format : Sprintf-style format of the text displayed in the dialog.
83  * ...        : Argument list for msg_format
84  *
85  */
86  
87 #define ESD_MAX_MSG_LEN 2048
88 void
89 simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
90   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
91               *bbox, *ok_btn, *cancel_btn;
92   GdkPixmap   *pixmap;
93   GdkBitmap   *mask;
94   GtkStyle    *style;
95   GdkColormap *cmap;
96   va_list      ap;
97   gchar        message[ESD_MAX_MSG_LEN];
98   gchar      **icon;
99
100   /* Main window */
101   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
102   gtk_container_border_width(GTK_CONTAINER(win), 7);
103
104   switch (type) {
105   case ESD_TYPE_WARN :
106     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Warning");
107     icon = icon_excl_xpm;
108     break;
109   case ESD_TYPE_CRIT :
110     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Critical");
111     icon = icon_excl_xpm;
112     break;
113   case ESD_TYPE_INFO :
114   default :
115     icon = icon_ethereal_xpm;
116     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Information");
117     break;
118   }
119
120   gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
121
122   /* Container for our rows */
123   main_vb = gtk_vbox_new(FALSE, 5);
124   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
125   gtk_container_add(GTK_CONTAINER(win), main_vb);
126   gtk_widget_show(main_vb);
127
128   /* Top row: Icon and message text */
129   top_hb = gtk_hbox_new(FALSE, 10);
130   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
131   gtk_widget_show(top_hb);
132   
133   style = gtk_widget_get_style(win);
134   cmap  = gdk_colormap_get_system();
135   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
136     &style->bg[GTK_STATE_NORMAL], icon);
137   type_pm = gtk_pixmap_new(pixmap, mask);
138   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
139   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
140   gtk_widget_show(type_pm);
141
142   /* Load our vararg list into the message string */
143   va_start(ap, msg_format);
144   vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
145
146   msg_label = gtk_label_new(message);
147   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
148   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
149   gtk_widget_show(msg_label);
150   
151   /* Button row */
152   bbox = gtk_hbutton_box_new();
153   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
154   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
155   gtk_widget_show(bbox);
156
157   ok_btn = gtk_button_new_with_label ("OK");
158   gtk_signal_connect_object(GTK_OBJECT(ok_btn), "clicked",
159     GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT (win)); 
160   gtk_container_add(GTK_CONTAINER(bbox), ok_btn);
161   GTK_WIDGET_SET_FLAGS(ok_btn, GTK_CAN_DEFAULT);
162   gtk_widget_grab_default(ok_btn);
163   gtk_widget_show(ok_btn);
164
165   if (btn_mask && *btn_mask == ESD_BTN_CANCEL) {
166     cancel_btn = gtk_button_new_with_label("Cancel");
167     gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
168       GTK_SIGNAL_FUNC(simple_dialog_cancel_cb), (gpointer) win);
169     gtk_container_add(GTK_CONTAINER(bbox), cancel_btn);
170     GTK_WIDGET_SET_FLAGS(cancel_btn, GTK_CAN_DEFAULT);
171     gtk_widget_show(cancel_btn);
172   }
173
174   if (btn_mask)
175     *btn_mask = ESD_BTN_OK;
176
177   gtk_widget_show(win);
178 }
179
180 static void
181 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
182   gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
183   
184   if (btn_mask)
185     *btn_mask = ESD_BTN_CANCEL;
186   gtk_widget_destroy(GTK_WIDGET(win));
187 }
188
189 static char *
190 setup_tmpdir(char *dir)
191 {
192         int len = strlen(dir);
193         char *newdir;
194
195         /* Append slash if necessary */
196         if (dir[len - 1] == '/') {
197                 newdir = dir;
198         }
199         else {
200                 newdir = g_malloc(len + 2);
201                 strcpy(newdir, dir);
202                 strcat(newdir, "/");
203         }
204         return newdir;
205 }
206
207 static int
208 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
209 {
210         static const char suffix[] = "XXXXXXXXXX";
211         int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
212         mode_t old_umask;
213         int tmp_fd;
214
215         if (namebuflen < namelen) {
216                 errno = ENAMETOOLONG;
217                 return -1;
218         }
219         strcpy(namebuf, dir);
220         strcat(namebuf, pfx);
221         strcat(namebuf, suffix);
222
223         /* The Single UNIX Specification doesn't say that "mkstemp()"
224            creates the temporary file with mode rw-------, so we
225            won't assume that all UNIXes will do so; instead, we set
226            the umask to 0077 to take away all group and other
227            permissions, attempt to create the file, and then put
228            the umask back. */
229         old_umask = umask(0077);
230         tmp_fd = mkstemp(namebuf);
231         umask(old_umask);
232         return tmp_fd;
233 }
234
235 static char *tmpdir = NULL;
236 #ifdef WIN32
237 static char *temp = NULL;
238 #endif
239 static char *E_tmpdir;
240
241 #ifndef P_tmpdir
242 #define P_tmpdir "/var/tmp"
243 #endif
244
245 int
246 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
247 {
248         char *dir;
249         int fd;
250         static gboolean initialized;
251
252         if (!initialized) {
253                 if ((dir = getenv("TMPDIR")) != NULL)
254                         tmpdir = setup_tmpdir(dir);
255 #ifdef WIN32
256                 if ((dir = getenv("TEMP")) != NULL)
257                         temp = setup_tmpdir(dir);
258 #endif
259
260                 E_tmpdir = setup_tmpdir(P_tmpdir);
261                 initialized = TRUE;
262         }
263
264         if (tmpdir != NULL) {
265                 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
266                 if (fd != -1)
267                         return fd;
268         }
269
270 #ifdef WIN32
271         if (temp != NULL) {
272                 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
273                 if (fd != -1)
274                         return fd;
275         }
276 #endif
277
278         fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
279         if (fd != -1)
280                 return fd;
281
282         return try_tempfile(namebuf, namebuflen, "/tmp", pfx);
283 }