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 <sys/socket.h>.

svn path=/trunk/; revision=17278
This commit is contained in:
Guy Harris 2006-02-12 21:52:18 +00:00
parent 4d94f994b5
commit 4d8d477018
9 changed files with 220 additions and 169 deletions

View File

@ -145,7 +145,8 @@ ethereal_SOURCES = \
g711.c \
merge.c \
proto_hier_stats.c \
summary.c
summary.c \
tempfile.c
# corresponding headers
ethereal_INCLUDES = \
@ -177,7 +178,8 @@ tethereal_SOURCES = \
$(ETHEREAL_COMMON_SRC) \
$(TETHEREAL_TAP_SRC) \
capture_opts.c \
capture_loop.c \
capture_loop.c \
tempfile.c \
tethereal-tap-register.c \
tethereal.c
@ -208,17 +210,17 @@ randpkt_SOURCES = \
# dumpcap specifics
dumpcap_SOURCES = \
$(PLATFORM_SRC) \
capture-pcap-util.c \
capture_stop_conditions.c \
getopt.c \
clopts_common.c \
conditions.c \
ringbuffer.c \
util.c \
version_info.c \
capture_opts.c \
capture_loop.c \
dumpcap.c
capture-pcap-util.c \
capture_stop_conditions.c \
clopts_common.c \
conditions.c \
dumpcap.c \
getopt.c \
ringbuffer.c \
tempfile.c \
version_info.c
# this target needed for distribution only

View File

@ -81,7 +81,7 @@
#include "ringbuffer.h"
#include "simple_dialog.h"
#include "util.h"
#include "tempfile.h"
#include "log.h"
#include "file_util.h"

View File

@ -42,10 +42,6 @@
#include <netdb.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include "ringbuffer.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
@ -665,25 +661,3 @@ const char *netsnmp_get_version(void) { return ""; }
gboolean dfilter_compile(const gchar *text, dfilter_t **dfp) { (void)text; (void)dfp; return FALSE; }
void dfilter_free(dfilter_t *df) { (void)df; }
/*
* Find out whether a hostname resolves to an ip or ipv6 address
* Return "ip6" if it is IPv6, "ip" otherwise (including the case
* that we don't know)
*/
const char* host_ip_af(const char *host
#ifndef HAVE_GETHOSTBYNAME2
_U_
#endif
)
{
#ifdef HAVE_GETHOSTBYNAME2
struct hostent *h;
return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
#else
return "ip";
#endif
}

2
file.c
View File

@ -58,7 +58,7 @@
#include "print.h"
#include "file.h"
#include "fileset.h"
#include "util.h"
#include "tempfile.h"
#include "merge.h"
#include "alert_box.h"
#include "simple_dialog.h"

View File

@ -55,7 +55,7 @@
#include <epan/prefs.h>
#include <epan/addr_resolv.h>
#include <epan/charsets.h>
#include "util.h"
#include "tempfile.h"
#include "gui_utils.h"
#include <epan/epan_dissect.h>
#include <epan/filesystem.h>

161
tempfile.c Normal file
View File

@ -0,0 +1,161 @@
/* tempfile.c
* Routines to create temporary files
*
* $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "file_util.h"
#if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6)
#include <glib/gstdio.h> /* 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 <pcap.h>, as the include of
* <pcap.h> might cause <winsock2.h> to be included, and if we've
* already included <winsock.h> as a result of including <windows.h>,
* we get a bunch of redefinitions.
*/
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#include "tempfile.h"
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);
}

43
tempfile.h Normal file
View File

@ -0,0 +1,43 @@
/* tempfile.h
* Declarations of routines to create temporary files
*
* $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __TEMPFILE_H__
#define __TEMPFILE_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* create a tempfile with the given prefix (e.g. "ether")
* namebuf (and namebuflen) should be 128+1 bytes long (BTW: why?)
* returns the file descriptor of the new tempfile and
* the name of the new file in namebuf
*/
int create_tempfile(char *namebuf, int namebuflen, const char *pfx);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __TEMPFILE_H__ */

121
util.c
View File

@ -37,30 +37,9 @@
#include <unistd.h>
#endif
#include "file_util.h"
#include <epan/address.h>
#include <epan/addr_resolv.h>
#if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6)
#include <glib/gstdio.h> /* 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 <pcap.h>, as the include of
* <pcap.h> might cause <winsock2.h> to be included, and if we've
* already included <winsock.h> as a result of including <windows.h>,
* we get a bunch of redefinitions.
*/
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#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,

8
util.h
View File

@ -29,14 +29,6 @@
extern "C" {
#endif /* __cplusplus */
/* create a tempfile with the given prefix (e.g. "ether")
* namebuf (and namebuflen) should be 128+1 bytes long (BTW: why?)
* returns the file descriptor of the new tempfile and
* the name of the new file in namebuf
*/
int create_tempfile(char *namebuf, int namebuflen, const char *pfx);
/* Collect command-line arguments as a string consisting of the arguments,
* separated by spaces.
*/