We are obliged to define HAVE_UNISTD_H in "config.h"; to avoid the

hideous problem on FreeBSD 3.[23] (and perhaps other BSDs) if
HAVE_UNISTD_H is defined before "zlib.h" is included, turn "file_seek()"
into a subroutine defined in a file that *undefines* HAVE_UNISTD_H
before including "zlib.h", so that the *only* call to "gzseek()" is made
from a file that does not have HAVE_UNISTD_H defined when it includes
"zlib.h".

Move "file_error()" to that file while you're at it, so it holds all the
wrappers that hide the presence or absence of zlib from routines to read
capture files.

Turn "file.h", which declared those wrapper functions as well as wrapper
macros, into "file_wrapper.h" - it belongs with the "file_wrapper.c"
file that defines the wrapper functions, not with "file.c" which handles
higher-layer file access functions.

Remove the comment in "configure.in" that explained why defining
HAVE_UNISTD_H was a bad idea, as we're not obliged to define it and work
around the problem.  (The comment in "file_wrapper.c" explains the
workaround.)

svn path=/trunk/; revision=1463
This commit is contained in:
Guy Harris 2000-01-13 07:09:20 +00:00
parent b558dc21ec
commit 7a36bede0b
18 changed files with 167 additions and 114 deletions

View File

@ -1,7 +1,7 @@
# Makefile.am
# Automake file for Wiretap
#
# $Id: Makefile.am,v 1.25 1999/12/12 22:40:07 gram Exp $
# $Id: Makefile.am,v 1.26 2000/01/13 07:09:15 guy Exp $
#
# Ethereal - Network traffic analyzer
# By Gerald Combs <gerald@zing.org>
@ -39,7 +39,8 @@ libwiretap_a_SOURCES = \
buffer.c \
buffer.h \
file.c \
file.h \
file_wrappers.c \
file_wrappers.h \
i4btrace.c \
i4btrace.h \
i4b_trace.h \

View File

@ -1,6 +1,6 @@
/* ascend.c
*
* $Id: ascend.c,v 1.10 2000/01/10 17:33:16 gram Exp $
* $Id: ascend.c,v 1.11 2000/01/13 07:09:15 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -27,7 +27,7 @@
#include "buffer.h"
#include "ascend.h"
#include "ascend-int.h"
#include "file.h"
#include "file_wrappers.h"
#include <sys/stat.h>

View File

@ -1,4 +1,4 @@
# $Id: configure.in,v 1.19 2000/01/10 17:33:17 gram Exp $
# $Id: configure.in,v 1.20 2000/01/13 07:09:15 guy Exp $
dnl
dnl Process this file with autoconf 2.13 or later to produce a
dnl configure script; 2.12 doesn't generate a "configure" script that
@ -45,47 +45,6 @@ if test "x$GLIB_CFLAGS" = x ; then
fi
dnl Checks for header files
dnl WARNING: do *NOT* check for "unistd.h" unless you work around the
dnl following problem:
dnl
dnl At least on FreeBSD 3.2, "/usr/include/zlib.h" includes
dnl "/usr/include/zconf.h", which, if HAVE_UNISTD_H is defined,
dnl #defines "z_off_t" to be "off_t", and if HAVE_UNISTD_H is
dnl not defines, #defines "z_off_t" to be "long" if it's not
dnl already #defined.
dnl
dnl In 4.4-Lite-derived systems such as FreeBSD, "off_t" is
dnl "long long int", not "long int", so the definition of "z_off_t" -
dnl and therefore the types of the arguments to routines such as
dnl "gzseek()", as declared, with prototypes, in "zlib.h" - depends
dnl on whether HAVE_UNISTD_H is defined prior to including "zlib.h"!
dnl
dnl It's not defined in the FreeBSD 3.2 "zlib", so if we include "zlib.h"
dnl after defining HAVE_UNISTD_H, we get a misdeclaration of "gzseek()",
dnl and, if we're building with "zlib" support, anything that seeks
dnl on a file may not work.
dnl
dnl Other BSDs may have the same problem, if they haven't done something
dnl such as defining HAVE_UNISTD_H in "zconf.h".
dnl
dnl If "config.h" defines HAVE_UNISTD_H - which it will, on all systems
dnl that have it, if we test for it here, and all 4.4-Lite-derived
dnl BSDs have it - then, given that "zlib.h" is included by "file.h",
dnl that means that unless we include "zlib.h" before we include
dnl "config.h", we get a misdeclaration of "gzseek()".
dnl
dnl Unfortunately, it's "config.h" that tells us whether we have "zlib"
dnl in the first place, so we don't know whether to include "zlib.h"
dnl until we include "config.h"....
dnl
dnl Fortunately, we don't use "unistd.h", so we don't need to check
dnl for it.
dnl
dnl If we ever *do* end up requiring HAVE_UNISTD_H, a fix might be to
dnl turn "file_seek()" into a subroutine, at least if HAVE_ZLIB is
dnl defined, have it take an "off_t" as its second argument, and
dnl put it into a file that doesn't require HAVE_UNISTD_H.
dnl
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h netinet/in.h unistd.h)

View File

@ -1,6 +1,6 @@
/* file.c
*
* $Id: file.c,v 1.44 2000/01/10 17:33:17 gram Exp $
* $Id: file.c,v 1.45 2000/01/13 07:09:15 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -36,7 +36,7 @@
#endif
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "lanalyzer.h"
#include "ngsniffer.h"
@ -448,41 +448,3 @@ gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
g_free(wdh);
return ret;
}
/*
* Routine to return a Wiretap error code (0 for no error, an errno
* for a file error, or a WTAP_ERR_ code for other errors) for an
* I/O stream.
*/
#ifdef HAVE_LIBZ
int
file_error(void *fh)
{
int errnum;
gzerror(fh, &errnum);
switch (errnum) {
case Z_OK: /* no error */
return 0;
case Z_STREAM_END: /* EOF - not an error */
return 0;
case Z_ERRNO: /* file I/O error */
return errno;
default:
return WTAP_ERR_ZLIB + errnum;
}
}
#else /* HAVE_LIBZ */
int
file_error(FILE *fh)
{
if (ferror(fh))
return errno;
else
return 0;
}
#endif /* HAVE_LIBZ */

131
wiretap/file_wrappers.c Normal file
View File

@ -0,0 +1,131 @@
/* file_wrappers.c
*
* $Id: file_wrappers.c,v 1.1 2000/01/13 07:09:16 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
*
* 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
/*
* OK, now this is tricky.
*
* At least on FreeBSD 3.2, "/usr/include/zlib.h" includes
* "/usr/include/zconf.h", which, if HAVE_UNISTD_H is defined,
* #defines "z_off_t" to be "off_t", and if HAVE_UNISTD_H is
* not defines, #defines "z_off_t" to be "long" if it's not
* already #defined.
*
* In 4.4-Lite-derived systems such as FreeBSD, "off_t" is
* "long long int", not "long int", so the definition of "z_off_t" -
* and therefore the types of the arguments to routines such as
* "gzseek()", as declared, with prototypes, in "zlib.h" - depends
* on whether HAVE_UNISTD_H is defined prior to including "zlib.h"!
*
* It's not defined in the FreeBSD 3.2 "zlib", so if we include "zlib.h"
* after defining HAVE_UNISTD_H, we get a misdeclaration of "gzseek()",
* and, if we're building with "zlib" support, anything that seeks
* on a file may not work.
*
* Other BSDs may have the same problem, if they haven't done something
* such as defining HAVE_UNISTD_H in "zconf.h".
*
* "config.h" defines HAVE_UNISTD_H, on all systems that have it, and all
* 4.4-Lite-derived BSDs have it. Therefore, given that "zlib.h" is included
* by "file_wrappers.h", that means that unless we include "zlib.h" before
* we include "config.h", we get a misdeclaration of "gzseek()".
*
* Unfortunately, it's "config.h" that tells us whether we have "zlib"
* in the first place, so we don't know whether to include "zlib.h"
* until we include "config.h"....
*
* So what we do is *undefine* HAVE_UNISTD_H before including "zlib.h",
* and make "file_seek()" a subroutine, so that the only call to "gzseek()"
* is in this file, which, by dint of the hackery described above,
* manages to correctly declare "gzseek()".
*
* DO NOT, UNDER ANY CIRCUMSTANCES, REMOVE THE FOLLOWING LINE, OR MOVE
* IT AFTER THE INCLUDE OF "zlib.h"! Doing so will cause any program
* using Wiretap to read capture files to fail miserably on a FreeBSD
* 3.2 or 3.3 system - and possibly other BSD systems - if zlib is
* installed. If you *must* include <unistd.h> here, do so *before*
* including "zlib.h", and before undefining HAVE_UNISTD_H.
*/
#undef HAVE_UNISTD_H
#ifdef HAVE_LIBZ
#include "zlib.h"
#endif
#include <errno.h>
#include <stdio.h>
#include "file_wrappers.h"
#ifdef HAVE_LIBZ
int
file_seek(void *stream, long offset, int whence)
{
return gzseek(stream, offset, whence);
}
#else /* HAVE_LIBZ */
int
file_seek(FILE *stream, long offset, int whence)
{
return fseek(stream, offset, whence);
}
#endif /* HAVE_LIBZ */
/*
* Routine to return a Wiretap error code (0 for no error, an errno
* for a file error, or a WTAP_ERR_ code for other errors) for an
* I/O stream.
*/
#ifdef HAVE_LIBZ
int
file_error(void *fh)
{
int errnum;
gzerror(fh, &errnum);
switch (errnum) {
case Z_OK: /* no error */
return 0;
case Z_STREAM_END: /* EOF - not an error */
return 0;
case Z_ERRNO: /* file I/O error */
return errno;
default:
return WTAP_ERR_ZLIB + errnum;
}
}
#else /* HAVE_LIBZ */
int
file_error(FILE *fh)
{
if (ferror(fh))
return errno;
else
return 0;
}
#endif /* HAVE_LIBZ */

View File

@ -1,6 +1,6 @@
/* file.h
/* file_wrappers.h
*
* $Id: file.h,v 1.5 1999/10/31 17:46:07 gram Exp $
* $Id: file_wrappers.h,v 1.1 2000/01/13 07:09:16 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -27,7 +27,7 @@
#ifdef HAVE_LIBZ
#define file_open gzopen
#define filed_open gzdopen
#define file_seek gzseek
extern int file_seek(void *stream, long offset, int whence);
#define file_read(buf, bsize, count, file) gzread((file),(buf),((count)*(bsize)))
#define file_write(buf, bsize, count, file) gzwrite((file),(buf),((count)*(bsize)))
#define file_close gzclose
@ -39,7 +39,7 @@ extern int file_error(void *fh);
#else /* No zLib */
#define file_open fopen
#define filed_open fdopen
#define file_seek fseek
extern int file_seek(FILE *stream, long offset, int whence);
#define file_read fread
#define file_write fwrite
#define file_close fclose

View File

@ -1,6 +1,6 @@
/* i4btrace.c
*
* $Id: i4btrace.c,v 1.2 1999/12/15 02:25:50 guy Exp $
* $Id: i4btrace.c,v 1.3 2000/01/13 07:09:16 guy Exp $
*
* Wiretap Library
* Copyright (c) 1999 by Bert Driehuis <driehuis@playbeing.org>
@ -28,7 +28,7 @@
#include <errno.h>
#include <time.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "i4b_trace.h"

View File

@ -1,6 +1,6 @@
/* iptrace.c
*
* $Id: iptrace.c,v 1.22 1999/11/27 01:55:44 guy Exp $
* $Id: iptrace.c,v 1.23 2000/01/13 07:09:16 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -28,7 +28,7 @@
#include <time.h>
#include <string.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "iptrace.h"

View File

@ -1,6 +1,6 @@
/* lanalyzer.c
*
* $Id: lanalyzer.c,v 1.18 1999/11/10 19:47:57 gram Exp $
* $Id: lanalyzer.c,v 1.19 2000/01/13 07:09:16 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -27,7 +27,7 @@
#include <errno.h>
#include <time.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "lanalyzer.h"

View File

@ -1,6 +1,6 @@
/* libpcap.c
*
* $Id: libpcap.c,v 1.29 2000/01/09 07:55:48 guy Exp $
* $Id: libpcap.c,v 1.30 2000/01/13 07:09:17 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -26,7 +26,7 @@
#include <stdlib.h>
#include <errno.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "libpcap.h"

View File

@ -1,6 +1,6 @@
/* netmon.c
*
* $Id: netmon.c,v 1.22 1999/12/15 01:34:17 guy Exp $
* $Id: netmon.c,v 1.23 2000/01/13 07:09:17 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -27,7 +27,7 @@
#include <time.h>
#include <string.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "netmon.h"

View File

@ -1,6 +1,6 @@
/* nettl.c
*
* $Id: nettl.c,v 1.2 1999/10/31 19:23:37 guy Exp $
* $Id: nettl.c,v 1.3 2000/01/13 07:09:18 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -28,7 +28,7 @@
#include <errno.h>
#include <time.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "nettl.h"

View File

@ -1,6 +1,6 @@
/* netxray.c
*
* $Id: netxray.c,v 1.20 1999/12/15 01:34:16 guy Exp $
* $Id: netxray.c,v 1.21 2000/01/13 07:09:18 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -29,7 +29,7 @@
#include <time.h>
#include <string.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "netxray.h"
#include "buffer.h"

View File

@ -1,6 +1,6 @@
/* ngsniffer.c
*
* $Id: ngsniffer.c,v 1.33 1999/12/14 12:41:13 oabad Exp $
* $Id: ngsniffer.c,v 1.34 2000/01/13 07:09:18 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -64,7 +64,7 @@
#include <time.h>
#include <string.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "ngsniffer.h"

View File

@ -1,6 +1,6 @@
/* radcom.c
*
* $Id: radcom.c,v 1.15 1999/11/18 21:48:52 guy Exp $
* $Id: radcom.c,v 1.16 2000/01/13 07:09:19 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -28,7 +28,7 @@
#include <errno.h>
#include <time.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "radcom.h"

View File

@ -1,6 +1,6 @@
/* snoop.c
*
* $Id: snoop.c,v 1.22 1999/12/04 11:19:04 guy Exp $
* $Id: snoop.c,v 1.23 2000/01/13 07:09:19 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -25,7 +25,7 @@
#endif
#include <errno.h>
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "snoop.h"
#ifdef HAVE_NETINET_IN_H

View File

@ -1,6 +1,6 @@
/* toshiba.c
*
* $Id: toshiba.c,v 1.6 2000/01/01 04:01:41 gram Exp $
* $Id: toshiba.c,v 1.7 2000/01/13 07:09:19 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
@ -26,7 +26,7 @@
#include "wtap.h"
#include "buffer.h"
#include "toshiba.h"
#include "file.h"
#include "file_wrappers.h"
#include <stdio.h>
#include <stdlib.h>

View File

@ -1,6 +1,6 @@
/* wtap.c
*
* $Id: wtap.c,v 1.35 1999/12/12 22:53:25 guy Exp $
* $Id: wtap.c,v 1.36 2000/01/13 07:09:20 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -27,7 +27,7 @@
#include "config.h"
#endif
#include "wtap.h"
#include "file.h"
#include "file_wrappers.h"
#include "buffer.h"
#include "ascend.h"
#include "toshiba.h"