diff options
author | Guy Harris <guy@alum.mit.edu> | 1999-08-18 02:59:05 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 1999-08-18 02:59:05 +0000 |
commit | fca713e47f71b641d9823bf2af97ac4ac40b723b (patch) | |
tree | 80d8e8644ef4c999ad227e5172af3b116540225d /util.c | |
parent | ac4f87218d7bf56558225bc0f78a5a0af25687e6 (diff) | |
download | wireshark-fca713e47f71b641d9823bf2af97ac4ac40b723b.tar.gz wireshark-fca713e47f71b641d9823bf2af97ac4ac40b723b.tar.bz2 wireshark-fca713e47f71b641d9823bf2af97ac4ac40b723b.zip |
Make a "create_tempfile()" routine that constructs the template to be
used by "mkstemp()" into a buffer supplied as an argument, trying
several directories for the tempfile, in the same fashion that the BSD
(and probably other) "tempnam()" routines do.
Have that routine cope with temporary-file directory names that don't
end with "/", as "P_tmpdir" doesn't necessarily end with "/" (and
doesn't, in GNU "libc" 2.x, at least on Linux); thanks to Gilbert
Ramirez for catching this one, and supplying the code to cope with that.
Have the code that creates the temporary file for the "Follow TCP
Stream" text use it.
svn path=/trunk/; revision=507
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 88 |
1 files changed, 87 insertions, 1 deletions
@@ -1,7 +1,7 @@ /* util.c * Utility routines * - * $Id: util.c,v 1.16 1999/07/09 04:18:36 gram Exp $ + * $Id: util.c,v 1.17 1999/08/18 02:59:05 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -32,7 +32,10 @@ #include <gtk/gtk.h> #include <stdarg.h> +#include <stdlib.h> +#include <string.h> #include <stdio.h> +#include <errno.h> #ifdef HAVE_FCNTL_H #include <fcntl.h> @@ -174,3 +177,86 @@ simple_dialog_cancel_cb(GtkWidget *w, gpointer win) { *btn_mask = ESD_BTN_CANCEL; gtk_widget_destroy(GTK_WIDGET(win)); } + +static char * +setup_tmpdir(char *dir) +{ + int len = strlen(dir); + char *newdir; + + /* Append slash if necessary */ + if (dir[len - 1] == '/') { + newdir = dir; + } + else { + newdir = g_malloc(len + 2); + strcpy(newdir, dir); + strcat(newdir, "/"); + } + return newdir; +} + +static int +try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx) +{ + static const char suffix[] = "XXXXXXXXXX"; + int namelen = strlen(namebuf) + strlen(pfx) + sizeof suffix; + + if (namebuflen < namelen) { + errno = ENAMETOOLONG; + return -1; + } + strcpy(namebuf, dir); + strcat(namebuf, pfx); + strcat(namebuf, suffix); + return mkstemp(namebuf); +} + +static char *tmpdir; +#ifdef WIN32 +static char *temp; +#endif +static char *E_tmpdir; + +int +create_tempfile(char *namebuf, int namebuflen, const char *pfx) +{ + char *dir; + int fd; + static gboolean initialized; + + if (!initialized) { + if ((dir = getenv("TMPDIR")) != NULL) + tmpdir = setup_tmpdir(dir); +#ifdef WIN32 + if ((dir = getenv("TEMP")) != NULL) + temp = setup_tmpdir(dir); +#endif +#ifdef P_tmpdir + E_tmpdir = setup_tmpdir(P_tmpdir); +#else + E_tmpdir = setup_tmpdir("/var/tmp/"); +#endif + initialized = TRUE; + } + + if (tmpdir != NULL) { + fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx); + if (fd != -1) + return fd; + } + +#ifdef WIN32 + if (temp != NULL) { + fd = try_tempfile(namebuf, namebuflen, temp, pfx); + if (fd != -1) + return fd; + } +#endif + + fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx); + if (fd != -1) + return fd; + + return try_tempfile(namebuf, namebuflen, "/tmp", pfx); +} |