4 * $Id: util.c,v 1.20 1999/09/23 05:20:18 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
48 #ifdef HAVE_SYS_TYPES_H
49 #include <sys/types.h>
52 #ifdef HAVE_SYS_STAT_H
56 #ifdef NEED_SNPRINTF_H
62 # include "snprintf.h"
67 #include "image/icon-excl.xpm"
68 #include "image/icon-ethereal.xpm"
70 static void simple_dialog_cancel_cb(GtkWidget *, gpointer);
72 const gchar *bm_key = "button mask";
74 /* Simple dialog function - Displays a dialog box with the supplied message
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
87 #define ESD_MAX_MSG_LEN 2048
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;
97 gchar message[ESD_MAX_MSG_LEN];
101 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
102 gtk_container_border_width(GTK_CONTAINER(win), 7);
106 gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Warning");
107 icon = icon_excl_xpm;
110 gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Critical");
111 icon = icon_excl_xpm;
115 icon = icon_ethereal_xpm;
116 gtk_window_set_title(GTK_WINDOW(win), "Ethereal: Information");
120 gtk_object_set_data(GTK_OBJECT(win), bm_key, btn_mask);
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);
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);
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);
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);
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);
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);
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);
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);
175 *btn_mask = ESD_BTN_OK;
177 gtk_widget_show(win);
181 simple_dialog_cancel_cb(GtkWidget *w, gpointer win) {
182 gint *btn_mask = (gint *) gtk_object_get_data(win, bm_key);
185 *btn_mask = ESD_BTN_CANCEL;
186 gtk_widget_destroy(GTK_WIDGET(win));
190 setup_tmpdir(char *dir)
192 int len = strlen(dir);
195 /* Append slash if necessary */
196 if (dir[len - 1] == '/') {
200 newdir = g_malloc(len + 2);
208 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
210 static const char suffix[] = "XXXXXXXXXX";
211 int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
215 if (namebuflen < namelen) {
216 errno = ENAMETOOLONG;
219 strcpy(namebuf, dir);
220 strcat(namebuf, pfx);
221 strcat(namebuf, suffix);
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
229 old_umask = umask(0077);
230 tmp_fd = mkstemp(namebuf);
235 static char *tmpdir = NULL;
237 static char *temp = NULL;
239 static char *E_tmpdir;
242 #define P_tmpdir "/var/tmp"
246 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
250 static gboolean initialized;
253 if ((dir = getenv("TMPDIR")) != NULL)
254 tmpdir = setup_tmpdir(dir);
256 if ((dir = getenv("TEMP")) != NULL)
257 temp = setup_tmpdir(dir);
260 E_tmpdir = setup_tmpdir(P_tmpdir);
264 if (tmpdir != NULL) {
265 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
272 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
278 fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
282 return try_tempfile(namebuf, namebuflen, "/tmp", pfx);