Small change to create_tempfile, initializing static vars.
[metze/wireshark/wip.git] / util.c
1 /* util.c
2  * Utility routines
3  *
4  * $Id: util.c,v 1.18 1999/08/18 15:29:06 gram 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 NEED_SNPRINTF_H
49 # ifdef HAVE_STDARG_H
50 #  include <stdarg.h>
51 # else
52 #  include <varargs.h>
53 # endif
54 # include "snprintf.h"
55 #endif
56
57 #include "util.h"
58
59 #include "image/icon-excl.xpm"
60 #include "image/icon-ethereal.xpm"
61
62 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
63
64 const gchar *bm_key = "button mask";
65
66 /* Simple dialog function - Displays a dialog box with the supplied message
67  * text.
68  * 
69  * Args:
70  * type       : One of ESD_TYPE_*.
71  * btn_mask   : The address of a gint.  The value passed in determines if
72  *              the 'Cancel' button is displayed.  The button pressed by the 
73  *              user is passed back.
74  * msg_format : Sprintf-style format of the text displayed in the dialog.
75  * ...        : Argument list for msg_format
76  *
77  */
78  
79 #define ESD_MAX_MSG_LEN 2048
80 void
81 simple_dialog(gint type, gint *btn_mask, gchar *msg_format, ...) {
82   GtkWidget   *win, *main_vb, *top_hb, *type_pm, *msg_label,
83               *bbox, *ok_btn, *cancel_btn;
84   GdkPixmap   *pixmap;
85   GdkBitmap   *mask;
86   GtkStyle    *style;
87   GdkColormap *cmap;
88   va_list      ap;
89   gchar        message[ESD_MAX_MSG_LEN];
90   gchar      **icon;
91
92   /* Main window */
93   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
94   gtk_container_border_width(GTK_CONTAINER(win), 7);
95
96   switch (type) {
97   case ESD_TYPE_WARN :
98     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Warning");
99     icon = icon_excl_xpm;
100     break;
101   case ESD_TYPE_CRIT :
102     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Critical");
103     icon = icon_excl_xpm;
104     break;
105   case ESD_TYPE_INFO :
106   default :
107     icon = icon_ethereal_xpm;
108     gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Information");
109     break;
110   }
111
112   gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
113
114   /* Container for our rows */
115   main_vb = gtk_vbox_new(FALSE, 5);
116   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
117   gtk_container_add(GTK_CONTAINER(win), main_vb);
118   gtk_widget_show(main_vb);
119
120   /* Top row: Icon and message text */
121   top_hb = gtk_hbox_new(FALSE, 10);
122   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
123   gtk_widget_show(top_hb);
124   
125   style = gtk_widget_get_style(win);
126   cmap  = gdk_colormap_get_system();
127   pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, cmap,  &mask,
128     &style->bg[GTK_STATE_NORMAL], icon);
129   type_pm = gtk_pixmap_new(pixmap, mask);
130   gtk_misc_set_alignment (GTK_MISC (type_pm), 0.5, 0.0);
131   gtk_container_add(GTK_CONTAINER(top_hb), type_pm);
132   gtk_widget_show(type_pm);
133
134   /* Load our vararg list into the message string */
135   va_start(ap, msg_format);
136   vsnprintf(message, ESD_MAX_MSG_LEN, msg_format, ap);
137
138   msg_label = gtk_label_new(message);
139   gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_FILL);
140   gtk_container_add(GTK_CONTAINER(top_hb), msg_label);
141   gtk_widget_show(msg_label);
142   
143   /* Button row */
144   bbox = gtk_hbutton_box_new();
145   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
146   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
147   gtk_widget_show(bbox);
148
149   ok_btn = gtk_button_new_with_label ("OK");
150   gtk_signal_connect_object(GTK_OBJECT(ok_btn), "clicked",
151     GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT (win)); 
152   gtk_container_add(GTK_CONTAINER(bbox), ok_btn);
153   GTK_WIDGET_SET_FLAGS(ok_btn, GTK_CAN_DEFAULT);
154   gtk_widget_grab_default(ok_btn);
155   gtk_widget_show(ok_btn);
156
157   if (btn_mask && *btn_mask == ESD_BTN_CANCEL) {
158     cancel_btn = gtk_button_new_with_label("Cancel");
159     gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
160       GTK_SIGNAL_FUNC(simple_dialog_cancel_cb), (gpointer) win);
161     gtk_container_add(GTK_CONTAINER(bbox), cancel_btn);
162     GTK_WIDGET_SET_FLAGS(cancel_btn, GTK_CAN_DEFAULT);
163     gtk_widget_show(cancel_btn);
164   }
165
166   if (btn_mask)
167     *btn_mask = ESD_BTN_OK;
168
169   gtk_widget_show(win);
170 }
171
172 static void
173 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
174   gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
175   
176   if (btn_mask)
177     *btn_mask = ESD_BTN_CANCEL;
178   gtk_widget_destroy(GTK_WIDGET(win));
179 }
180
181 static char *
182 setup_tmpdir(char *dir)
183 {
184         int len = strlen(dir);
185         char *newdir;
186
187         /* Append slash if necessary */
188         if (dir[len - 1] == '/') {
189                 newdir = dir;
190         }
191         else {
192                 newdir = g_malloc(len + 2);
193                 strcpy(newdir, dir);
194                 strcat(newdir, "/");
195         }
196         return newdir;
197 }
198
199 static int
200 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
201 {
202         static const char suffix[] = "XXXXXXXXXX";
203         int namelen = strlen(namebuf) + strlen(pfx) + sizeof suffix;
204
205         if (namebuflen < namelen) {
206                 errno = ENAMETOOLONG;
207                 return -1;
208         }
209         strcpy(namebuf, dir);
210         strcat(namebuf, pfx);
211         strcat(namebuf, suffix);
212         return mkstemp(namebuf);
213 }
214
215 static char *tmpdir = NULL;
216 #ifdef WIN32
217 static char *temp = NULL;
218 #endif
219 static char *E_tmpdir;
220
221 #ifndef P_tmpdir
222 #define P_tmpdir "/var/tmp"
223 #endif
224
225 int
226 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
227 {
228         char *dir;
229         int fd;
230         static gboolean initialized;
231
232         if (!initialized) {
233                 if ((dir = getenv("TMPDIR")) != NULL)
234                         tmpdir = setup_tmpdir(dir);
235 #ifdef WIN32
236                 if ((dir = getenv("TEMP")) != NULL)
237                         temp = setup_tmpdir(dir);
238 #endif
239
240                 E_tmpdir = setup_tmpdir(P_tmpdir);
241                 initialized = TRUE;
242         }
243
244         if (tmpdir != NULL) {
245                 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
246                 if (fd != -1)
247                         return fd;
248         }
249
250 #ifdef WIN32
251         if (temp != NULL) {
252                 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
253                 if (fd != -1)
254                         return fd;
255         }
256 #endif
257
258         fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
259         if (fd != -1)
260                 return fd;
261
262         return try_tempfile(namebuf, namebuflen, "/tmp", pfx);
263 }