From 4d8d477018ac51a9536103e34368dc11a37e2ed4 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sun, 12 Feb 2006 21:52:18 +0000 Subject: Move create_tempfile() to tempfile.c out of util.c. This means dumpcap no longer needs util.c, so it no longer includes routines that use host_ip_af(), so it no longer needs to define its own host_ip_af(). That also means dumpcap.c no longer needs to include . svn path=/trunk/; revision=17278 --- util.c | 121 ----------------------------------------------------------------- 1 file changed, 121 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index c2ce7cc192..2737677140 100644 --- a/util.c +++ b/util.c @@ -37,30 +37,9 @@ #include #endif -#include "file_util.h" - #include #include -#if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6) -#include /* available since GLib 2.6 only! */ - -/* GLib2.6 or above, using new wrapper functions */ -#define eth_mkstemp g_mkstemp -#else -#define eth_mkstemp mkstemp -#endif - -/* - * This has to come after the include of , as the include of - * might cause to be included, and if we've - * already included as a result of including , - * we get a bunch of redefinitions. - */ -#ifdef HAVE_WINDOWS_H -#include -#endif - #include "util.h" /* @@ -103,106 +82,6 @@ get_args_as_string(int argc, char **argv, int optind) return argstring; } -static const char * -setup_tmpdir(const char *dir) -{ - size_t len = strlen(dir); - char *newdir; - - /* Append path separator if necessary */ - if (len != 0 && dir[len - 1] == G_DIR_SEPARATOR) { - return dir; - } - else { - newdir = g_malloc(len + 2); - strcpy(newdir, dir); - strcat(newdir, G_DIR_SEPARATOR_S); - return newdir; - } -} - -static int -try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx) -{ - static const char suffix[] = "XXXXXXXXXX"; - int namelen = strlen(dir) + strlen(pfx) + sizeof suffix; - int old_umask; - int tmp_fd; - - if (namebuflen < namelen) { - /* Stick in a truncated name, so that if this error is - reported with the file name, you at least get - something. */ - g_snprintf(namebuf, namebuflen, "%s%s%s", dir, pfx, suffix); - errno = ENAMETOOLONG; - return -1; - } - strcpy(namebuf, dir); - strcat(namebuf, pfx); - strcat(namebuf, suffix); - - /* The Single UNIX Specification doesn't say that "mkstemp()" - creates the temporary file with mode rw-------, so we - won't assume that all UNIXes will do so; instead, we set - the umask to 0077 to take away all group and other - permissions, attempt to create the file, and then put - the umask back. */ - old_umask = umask(0077); - tmp_fd = eth_mkstemp(namebuf); - umask(old_umask); - return tmp_fd; -} - -static const char *tmpdir = NULL; -#ifdef _WIN32 -static const char *temp = NULL; -#endif -static const char *E_tmpdir; - -#ifndef P_tmpdir -#define P_tmpdir "/var/tmp" -#endif - -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 - - E_tmpdir = setup_tmpdir(P_tmpdir); - 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, G_DIR_SEPARATOR_S "tmp", pfx); -} - /* Compute the difference between two seconds/microseconds time stamps. */ void compute_timestamp_diff(gint *diffsec, gint *diffusec, -- cgit v1.2.1